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.
Here is the code:
*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:
see JTable tutorial, test code examples, for example