• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox problem...  RSS feed

 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
i'm having a problem with getting the first item in the JComboBox() being selected. i have to select another item before i'm able to select the first item and trigger it. i'm using 1.6 :



i never used JComboBox() before, so any help would be appreciated. thanks in advance.

 
VarunKumar Mallisetty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are facing the problem because of adding ActionListener to JComboBox. Use a ItemListener for the JComboBox. Have a look at java tutorial's JComboBox section for usage and more details. http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"VarunKumar",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i added the following; still exhibits the same behavior. also setSelectedIndex() will not work for first item as well. any ideas?

 
VarunKumar Mallisetty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A ItemEvent will be generated only when the selection changes. Since already the first element is being selected setSelectedIndex(0) will not raise an event. You may want to initially set the text for price label or add a first item like "Select a size" to JComboBox and don't set any initial value for price label.
Hope this helps.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try it like this

 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS mike && varun. that was the solution(s) i needed. i read in another forum that this is the correct behavior as of 1.6. now i can handle it from here. thanks again && take care.

ps. nice use of the ActionListener(), mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!