Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox Default Selection Problem

 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds like you might have duplicate declarations of your comboBox
 
Dennis Putnam
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic