File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox oddity

 
Tim Manchester
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic