• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to persuade a JComboBox to change its selected item

 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it finds a ResultSet (rs), and populates the JComboBox with addresses; if I enter my own postcode I see several addresses from this street. And they change colour if I click them, but I can only seem to get it to return the first Address in the Vector. The System.out.printf call never seems to be executed.
I have tried changing to new JComboBox(addresses.toArray()) and myAddress = addresses.get(addressBox.getSelectedIndex()); and commented the two lines starting // out and commented them back in, but it resolutely selects the first address in the street.
It looks very like the example in my copy of Deitel and Deitel (6th edition page 544) and similar to the examples in the Java Tutorial, so I can't see what I have done wrong.
I even tried commenting out the ItemListener and replacing it with an addActionListener call, but no change.

What have I done wrong?
[ October 09, 2008: Message edited by: Campbell Ritchie ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> but I can only seem to get it to return the first Address in the Vector

possibly similar to this

http://www.coderanch.com/t/342430/GUI/java/Same-value-JComboBox

can you hard-code 5 or 6 address (lieu of rs.next()), and post a compilable program?
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always prefer ActionListener for JComboBox. Never failed me yet.

Well, that is if I use an uneditable JComboBox.
[ October 10, 2008: Message edited by: Rob Prime ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
can you hard-code 5 or 6 address (lieu of rs.next()), and post a compilable program?


Yes, but it will be a couple of hours before I post it. I tried with an ActionListener and got a test printout but it always read 0. I shall have a look at the link later.

Thank you
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much; when I saw Dunn and Prime were on the case I knew I was onto a winner.That is a simplified version of what I was running. It seems to be the setEditable(false) which helped, as Rob said. I have had a look at Michael's previous link and seen how that works.

Thank you very much, both of you

I am still having problems, however. This simple version reliably prints out 11 High St or 13 High St, but I am still having problems with my complicated version. I am going to try with a stand-alone version with its own database connection which might be easier to analyse.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic