0

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.

4

1 回答 1

1

jComboBox 是可过滤和可编辑的。

如果组合框是可编辑的并且用户向组合框添加了新项目,您如何提供工具提示?

但是当 jComboBox 显示过滤的项目时,jToolTip 索引的顺序发生了变化。

不要将您的查找基于索引。相反,您需要基于项目(或项目的 toString() 值)进行查找。为此,您可以使用HashMap

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

然后在渲染器中你可以使用:

String tooltip = tooltips.get( value.toString() );
于 2015-03-10T14:59:18.817 回答