• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox oddity

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic