wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JComboBox oddity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox oddity" Watch "JComboBox oddity" New topic
Author

JComboBox oddity

Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
I've noticed something really quite odd about the JComboBox class. For background, let me say that I'm populating a JComboBox (let's say the instance name is cbMyCombo) with an array of objects (let's call them dataObj class) that have data members: String name, int id and a toString() method that returns the name. This is working ducky. What irks me is the way the the JComboBox behaves in the container's actionPerfomed() method.
If I do the following:
========================================

========================================
I see that when I trigger an event on cbMyCombo, cbMyCombo.getSelectedItem() returns a dataObj object. In any other case (when any other control fires an event), cbMyCombo.getSelectedItem() returns a string! How can I ensure that I always get a dataObj returned from cbMyCombo.getSelectedItem()? This is really troubling. Thanks for looking at this issue.

Regards,

- DM_Tim
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I'm not sure I'm following you on this one.

Here is a quick and dirty example of a combo class:


How is this particular code exhibiting the problems you are having?
Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
I see one major difference between my code and yours. My JPanel class implements ActionListener and all of my controls use that action listener:

You on the other hand, create a unique action listener for each control:


So I'm guessing that the behavior has to do with the fact that a JPanel that implements actionPerformed() for all it's controls is the cause. Does that sound right to you?

Regards,

- DM_Tim
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113

So I'm guessing that the behavior has to do with the fact that a JPanel that implements actionPerformed() for all it's controls is the cause. Does that sound right to you?


Yeah, that sounds right to me too.

If it's not introducing a great deal of work, try doing it with an actionPerformed() method for each component.
Tim Manchester
Ranch Hand

Joined: Nov 03, 2004
Posts: 62
Ya, I did duplicate your process in my code. That did the trick. That was a great tip. What torques me is that I could find no mention of this behavior on the Sun site or any of the books I've perused. You have been incredibly helpful. Thanks!

Regards,

- Tim Manchester
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JComboBox oddity
 
Similar Threads
How the insertAtBack sets the next for head?
JComboBox - rookie question
small swings problem
combobox rendered in a Jtable
jtable problem please help