The Artima Developer Community
Sponsored Link

Java Answers Forum
drop down boxes

1 reply on 1 page. Most recent reply: May 13, 2002 3:38 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Fariha

Posts: 1
Nickname: fariha
Registered: May, 2002

drop down boxes Posted: May 11, 2002 4:55 PM
Reply to this message Reply
Advertisement
If you have two drop-down boxes, how do you get the results? please help


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: drop down boxes Posted: May 13, 2002 3:38 PM
Reply to this message Reply
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

Flat View: This topic has 1 reply on 1 page
Topic: Using jar Previous Topic   Next Topic Topic: no return value

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use