This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

JComboBox Default Selection Problem  RSS feed

 
Ranch Hand
Posts: 377
  • 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?
 
Bartender
Posts: 5167
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: 377
  • 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: 5167
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.
 
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: 377
  • 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.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
ScroogeXHTML 8.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/704747/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!