aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to persuade a JComboBox to change its selected item Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to persuade a JComboBox to change its selected item" Watch "How to persuade a JComboBox to change its selected item" New topic
Author

How to persuade a JComboBox to change its selected item

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
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

Joined: Oct 13, 2005
Posts: 39388
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to persuade a JComboBox to change its selected item