aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Processing with two JComboBox's in one JPanel/JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Processing with two JComboBox Watch "Processing with two JComboBox New topic
Author

Processing with two JComboBox's in one JPanel/JFrame

Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
I have researched the Java Ranch forum for Swing/... and don't see
an entry quite like this. There was one other but it had no code.

My intent is to have two combo boxes - pickRstrInit and pickRstrName.

The first combo box selects the initial of last name.
This causes the data base to be read for all persons whose last name
begins with that initial. And load those names into the second combo box.

Then the second combo box is selected to cause the record of that name to
be updated.

In the code below, the first combo box is initialized with a letter array.
The second is initialized with a #.
Two switches are used to indicate which box has been selected.
chrPickSwt1 is set for the initial combo box and chrPickSwt2 for the name
combo box.

When the initial combo box has been selected, the code does what it should
in that it reads the data table and does pickRstrName.addItem which adds the
data record to the second box. At the end of the data base code, I put code
to delete the # record.

The problem is that the deletion statement pickRstrName.removeItemAt(0)
cause an Action Event "comboBoxChanged" which prematurely does processing
before a name is selected, causing a null pointer exception.

Is this the best way to do this? Or is there a method I am not
familiar with that is better? Any advice would be helpful.

===============Program Follows=========

================End=======================
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure what the problem is, and I can't run the code to see what's happening,
but, generally the best way to 'load' the second comboBox (after selection of item in first comboBox)
is to change the model, combo2.setModel(..)

if there's a problem in the second comboBox listener firing when a change is made to the contents of the comboBox,
remove the listener, make the change, add the listener back.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Will,
In future, while posting code, please use code tags.
I have edited your existing post.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
Maneesh,
Thanks for the edit and also the tip about Code Tags.
Will
Will Barbee
Ranch Hand

Joined: Oct 01, 2008
Posts: 41
LESSONS LEARNED:

Which combo box caused the action can be determined by testing them:



I also found it helpful to only addActionListener to the boxes when
they should be eligible for use. So, as the program progressed (it was a
menu) it would addActionListener and removeActionListener.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Processing with two JComboBox's in one JPanel/JFrame