Thomas SMETS
Posts: 307
Nickname: tsmets
Registered: Apr, 2002
|
|
Re: drop down boxes
|
Posted: May 13, 2002 3:38 PM
|
|
package test.gui;
import org.apache.log4j.Category;
import org.apache.log4j.BasicConfigurator;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;
/**
* The purpose of this class is to show the usage of ItemStateChange.
*
* @see http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node115.html#SECTION000106000000000000000
*
* This code is free of any copyright. Just reference the author and
* his sources for the sake of ...
*
* @author <a href="mailto:tsmets@altern.org">Thomas SMETS, Brussels</a>
* @version 0.0.2
*/
public class ListenDropDown
extends JFrame
{
public static final int NBR_JCOMBO = 10;
Container c = null;
JComboBox combo[] = null;
Category log = Category.getInstance (ListenDropDown.class);
ListenDropDown ()
{
super ("ListenDropDown");
log.info ("Instanciating");
c = this.getContentPane ();
combo = new JComboBox [NBR_JCOMBO];
c.setLayout ( new GridLayout ( 1, NBR_JCOMBO+1) );
log.debug ("array instanciated adding the JComboBox");
for (int i = 0; i < NBR_JCOMBO; i++)
{
log.debug ("Adding the JComboBox nbr " + i);
combo[i]
= new JComboBox (new String[] { "1", "2", "3", "4", "5", "6", "7" });
c.add ( combo[i] );
combo[i].addItemListener (new CustomActionListener (i));
} // End of for-loop
setVisible (true);
pack ();
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
/**
*
*/
public static void main (String[] args)
{
BasicConfigurator.resetConfiguration ();
BasicConfigurator.configure ();
new ListenDropDown();
}
public class CustomActionListener
implements ActionListener,
ItemListener
{
int ComboNbr = 0;
Category log = null;
public CustomActionListener ()
{
log = Category.getInstance (CustomActionListener.class);
}
public CustomActionListener (int aComboNbr)
{
this ();
ComboNbr = aComboNbr;
}
public void actionPerformed (ActionEvent anActionEvent)
{
log.info ("Message from : " + ComboNbr + " : public void actionPerformed (ActionEvent " + anActionEvent + ")");
}
public void itemStateChanged( ItemEvent event )
{
if( event.getSource() == combo[ComboNbr]
&& event.getStateChange() == ItemEvent.SELECTED )
log.info ( "Message from : " + ComboNbr + " Change :"
+ combo[ComboNbr].getSelectedItem() );
}
}
}
Don't forget to change the lines :
combo<i> = new JComboBox (new String[] { "1", "2", "3", "4", "5", "6", "7" }); c.add ( combo<i> ); combo<i>.addItemListener (new CustomActionListener (i));
into : combo = new JComboBox (new String[] { "1", "2", "3", "4", "5", "6", "7" }); c.add ( combo ); combo.addItemListener (new CustomActionListener (i));
Thomas SMETS, SCJP2 - Brussels
|
|