How do I show a tooltip for a JComboBox item?

Sandip Chitale

For the item that is normally shown in the JComboBox simply calling the -


ToolTipManager. sharedInstance().registerComponent(comboBox);
comboBox.setToolTipText("item info");

should work.

For the items in the popup associated with the JComboBox you will have to install a custom ListCellRenderer using the following class and API -


class MyCellRenderer extends JLabel implements ListCellRenderer {
     public MyCellRenderer() {
         setOpaque(true);
     }
     public Component getListCellRendererComponent(
         JList list,
         Object value,
         int index,
         boolean isSelected,
         boolean cellHasFocus)
     {
         // should be done only once...
         ToolTipManager. sharedInstance().registerComponent(list);
         setText(value.toString());
         setBackground(isSelected ? Color.red : Color.white);
         setForeground(isSelected ? Color.white : Color.black);
         if (value != null) {
             setToolTipText("item info based on the value");
         }
         return this;
     }
 }
comboBox.setRenderer(new MyCellRenderer());

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact