Even I was confused why this problem occurred to me. I mean, I have another JComboBox cell editor used in another JTable and there was no problem with the selection highlight when I pressed either the up or down arrow key to navigate through the items.

i double checked line by line and compared my two classes to ensure that there is nothing different with the two of them. And still, the selection highlight problem persisted.

Luckily, there is a property that you can set to a JComboBox cell editor to highlight the selected item when you use the up and down arrow keys.

If you encounter this problem, avoid debugging your code and use this property right away. It took me some time to find out the solution but the fix is pretty quick.

You wonder why there may be a need to have a tooltip inside a JComboBox. What if your item is very long like the image shown below?

This where tooltips come in handy unless you plan to have your JComboBox display items in multi-line if they are very long but that won’t look good. A tooltip is the best solution and the way to go about this is to assign a renderer to the JComboBox that does this.

This class does the trick. You only need to instantiate this and assign it to the JComboBox by calling the setRenderer() method.

I recently posted articles on how to get the selected item chosen in a JComboBox. Using an ItemListener is painfully confusing if you do not know how it works since it fires two events. The best listener to use for a JComboBox to listen which item is selected is an ActionListener.

The same ActionListener code that you use for a JButton can be used for a JComboBox. See code below.

Related Posts Plugin for WordPress, Blogger...