简体   繁体   中英

Set tooltip for filterable and editable jComboBox items

I have a JComboBox that is filterable and editable. I would like to set a tooltip for each item in the JComboBox - I figured I should use JToolTip for this.

I tried to use the answer from this link: Java Swing: Mouseover text on JComboBox items? .

But when the JComboBox shows filtered items, the order of the JToolTip index is changed. In this case I don't know how to set the right JToolTip text for each JComboBox item.

I would greatly appreciate it if you kindly give me some advice concerning to this problem.

jComboBox is filterable and editable.

How do you provide a tooltip if the combo box is editable and the user adds a new item to the combo box?

But when jComboBox shows filtered items, the order of jToolTip index is changed.

Don't base your lookup on index. Instead you need to base the lookup on the item (or toString() value of the item). For this you can use a HashMap

HashMap<String, String> tooltips = new HashMap<String, String>();
tooltips.put("A", "tooltip for item A");
tooltips.put("B", "tooltip for item B");

Then in the renderer you can use:

String tooltip = tooltips.get( value.toString() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM