aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Arrow navigation not working on JComboBox with null element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Arrow navigation not working on JComboBox with null element" Watch "Arrow navigation not working on JComboBox with null element" New topic
Author

Arrow navigation not working on JComboBox with null element

Mae Tan
Greenhorn

Joined: Sep 03, 2010
Posts: 8
Hi!

Is there a way to get arrow navigation to work on a JComboBox with a null first item?

Thanks,
Mae
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Hello Mae Tan, and welcome to JavaRanch!

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!

/Pete
Mae Tan
Greenhorn

Joined: Sep 03, 2010
Posts: 8
Hi Pete!

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.



Thanks,
Mae
Mae Tan
Greenhorn

Joined: Sep 03, 2010
Posts: 8
Sorry, first addItem should read:

cb.addItem(null);

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
I would like to maintain the null first element rather than have an empty string


What is the point of this? Why do you need an empty or null object?

Anyway, I doubt it can be done, since using the arrow keys will ultimately result in the comboBox setSelectedItem(...) method to be invoked. From the API for this method:

anObject - the list object to select; use null to clear the selection

So every time you try to select the null item the selection gets set back to -1.

Try changing your code to:



and you will see the selection cycle through "test1", "test2" and nothing.

as the code is already widely used.


Its better to have a proper design then to attempt to work around something.
Mae Tan
Greenhorn

Joined: Sep 03, 2010
Posts: 8
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?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
The null object is to allow the user to not set a value


Provide a more meaningfull item, like "None", "Choose"?

Is it possible to browse the items on a combobox and not make a selection?


Not when you use the keyboard.
Mae Tan
Greenhorn

Joined: Sep 03, 2010
Posts: 8
Ok, thanks for looking into this. Will modify as you suggest.
Sean Connolly
Greenhorn

Joined: Sep 16, 2010
Posts: 1
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.

Enjoy,
-Sean
Mae Tan
Greenhorn

Joined: Sep 03, 2010
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrow navigation not working on JComboBox with null element