aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JComboBox Default Selection Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox Default Selection Problem" Watch "JComboBox Default Selection Problem" New topic
Author

JComboBox Default Selection Problem

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I am having trouble setting the default value in a JComboBox. When I do nothing, the default is the first index (0) in the list. However, when I use setSelectedItem, the value shows up as "..." rather than the item specified (that string is not in my list). Am I misunderstanding what setSelectItem does and perhaps using the wrong method?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.


luck, db
There are no new questions, but there may be new answers.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Thanks for the reply but I don't know how practical it would be to write an SSCCE that is sufficiently representative. However, here is the actual code I am using plus some additional behavior I have since found.

The values for the ComboBox are an ArraList from a SQL query so I need to change it to an array of strings. All this works fine, including the JComboBox until I try to setSelectedItem as the default. As I said previously, the box then shows the default value as "..." which is not one of the strings in the array. In addition, I cannot select any value in the JFrame when the JComboBox is displayed. That is, no matter what I select, the display remains "...". The value or item returned by the JComboBox is always 0 or the 0th item which is not the correct index for setSelectedItem. Hopefully that is sufficient to help you get me on the right track or at least suggest some debug technique.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

You can create a SSCCE with harrdcoded data. And you can use java.util.List#toArray(T[]) instead of rolling your own in 4 lines.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
sounds like you might have duplicate declarations of your comboBox
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Got it. It turned out the box was too small for one of the selections. I don't understand why that caused the behavior problem I had but when I expanded the size, it started working correctly. Sorry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JComboBox Default Selection Problem