• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Arrow navigation not working on JComboBox with null element

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

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

Thanks,
Mae
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, first addItem should read:

cb.addItem(null);

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks for looking into this. Will modify as you suggest.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic