I wonder if you could create and post a small compilable and runnable program that demonstrates for us your problem, that we can run on our own computers and would allow us to alter your code and perhaps better help you reach a solution. This type of small program is often referred to as an SSCCE
Best of luck on finding a solution, and again, welcome!
Below is program demonstrating my problem. When the combobox receives focus, the arrow navigation does not work. I would like to maintain the null first element rather than have an empty string as the code is already widely used.
What is the point of this? Why do you need an empty or null object?
The null object is to allow the user to not set a value to the field that the combobox represents. Is it possible to browse the items on a combobox and not make a selection? Or am I using the wrong UI component?
I just figured this one out a couple weeks ago after not finding a solution online. The problem is in JComboBox's getSelectedIndex method. There's a check there that if the selected value is null, it is hardcoded to return -1 as the index (rather than 0 if it were the first item in your combo box). The UI uses the selected index to return the next value. If it returns -1, there is no next value by its logic so it ignores the down arrow until a "valid" value is selected.
The solution I came up with was to subclass JComboBox (which I had already done) and override that method to check if null is a value in the model. And if so, to use that index. Otherwise, just return -1 as usual. Simple and it worked.
Thanks Sean! I think I will go with your solution. It does save me from making an overhaul in an application that has already come to expect null when the first selection item is selected. Much appreciated.
As a side note, I wonder why null is not considered a valid selection item. This is more a bug than a feature.
My issue now is how to get the null item selected with a return and not a mouse action. When selecting "test1" or a non-null item, you can press return. For the null item, you have to do an Esc.
For my next trick, I'll need the help of a tiny ad ...