Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to associate Item Listenrs with 2 JCombo Boxes

 
Mike Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would i go about associating Itemlisteners with 2 JComboBoxes and add the JComboBoxes to a JPanel with a border layout.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you either add an itemListener to each of the comboboxes, or, if the code
is to be essentially the same in each listener, you create an itemListener
class, and use getSource() to ID the combobox.

The common way though, when first starting in gui's, is to have your gui class
implement ItemListener, provide the method itemStateChanged(..), and then its
comboBox.addItemListener(this) added to both comboboxes, again, in itemStateChanged(..)
you use getSource() to ID which combobox
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are using a BorderLayout you can locate components in 5 different positions: NORTH, SOUTH, EAST, WEST, or CENTER

You can do it somewhat like this within a JFrame class:



As for the ItemListener, well you could implement it inline with annonymous classes or you can declare an inner class that implements it and then create an instance of it and associated with your combos. Somewhat like this:



...and you associate an implemention of this with your combos. Somewhat like this:



I hope this helps!

[ May 07, 2006: Message edited by: Edwin Dalorzo ]
[ May 07, 2006: Message edited by: Edwin Dalorzo ]
 
Beware the other head of science - it bites! Nibble on this message:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!