在JTable中为JComboBox的Items设置一个工具提示作为CellEditor [英] Setting a tooltip for a value from JComboBox's Items as CellEditor in JTable
问题描述
我想为自定义组合框中的值设置工具提示值。基本上,我的组合框包含一个人的首字母列表 - 当它们悬停在我希望它显示该人的全名时。我的组合框基本上是JTable的单元/列,它有自己的TableCellRenderer和DefaultCellEditor。该列表是一个JList(模型中的内部类) - iv尝试直接设置'setToolTipText'方法,但似乎不起作用。
I would like to set a tooltip value for the values in a custom combo box. Basically, my combobox contains a list of initials of people - when they hover over i want it to display the fullname of the person. My combo box is basically a cell/column of a JTable that has its own TableCellRenderer and DefaultCellEditor. The list is a JList (inner class within the model) - iv tried setting the 'setToolTipText' method directly but that doesnt seem to work.
以下是代码:
* JTable中的代码:*
*Code within the JTable: *
public void setupUserCombo(Container container){
TableColumn col = getColumnModel().getColumn(3);
Set<ComboUser> values = new LinkedHashSet<ComboUser>();
ComboUser comboUser = new ComboUser(new User("Test User"));
values.add(comboUser);
col.setCellEditor(new MyComboBoxEditor((ComboUser[])values.toArray(new ComboUser[0])));
col.setCellRenderer(new MyComboBoxRenderer((ComboUser[])values.toArray(new ComboUser[0])));
repaint();
}
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public MyComboBoxRenderer(ComboUser[] items) {
super(items);
repaint();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value != null){
System.out.println("Setting tooltip");
((ComboUser)value).setToolTipText("TESTING!!");
}
setSelectedItem(value);
return this;
}
}
public class MyComboBoxEditor extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
public MyComboBoxEditor(ComboUser[] items) {
super(new JComboBox(items));
}
}
*模型中的子类:*
*Subclass within the model: *
public class ComboUser extends JLabel{
private User user;
public ComboUser(User user){
if (user != null){
this.user = user;
} else {
this.user = new User("");
}
}
@Override
public String toString() {
return user.getInitials();
}
推荐答案
如果你想要工具提示编辑组合框,你必须在组合框的自定义渲染器中这样做。下面是一个简短的例子:
If you want tooltips in the editing combobox, you'll have to so in a custom renderer of that combobox. Below is a short example:
// some data
User[] users = new User[] {
new User("Clara Zetkin", "CZ"),
new User("Rosa Luxemburg", "RL"),
new User("Susan Sontag", "SS"),
};
// the renderer for use in the editor
ListCellRenderer comboRenderer = new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (value instanceof User) {
setToolTipText(((User) value).getName());
value = ((User) value).getInitials();
} else {
setToolTipText(null);
}
return super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
}
};
JComboBox editingCombo = new JComboBox(users);
editingCombo.setRenderer(comboRenderer);
JTable table = new JTable(3, 2);
// set up the editor for the first column
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(editingCombo));
// the renderer to use in the table
TableCellRenderer renderer = new DefaultTableCellRenderer() {
@Override
protected void setValue(Object value) {
if (value instanceof User) {
setToolTipText(((User) value).getInitials());
value = ((User) value).getName();
} else {
setToolTipText(null);
}
super.setValue(value);
}
};
table.getColumnModel().getColumn(0).setCellRenderer(renderer);
这篇关于在JTable中为JComboBox的Items设置一个工具提示作为CellEditor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!