• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
Your mother was a hamster and your father was a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic