JComboBox ActionListener


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: JComboBox ActionListener

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    11

    JComboBox ActionListener

    Hello again all,

    I'm still trying to work out my JComboBox and I'm having trouble with my ActionListener. I would like to have some JLabels change when I make a selection in my box.

    Code:
    public void admitPane(){
    		
       JLabel labelAdmit = new JLabel ("Admit");
       JPanel admitCard = new JPanel();
    			
       JLabel label1=new JLabel();
       JLabel label2=new JLabel();
       JLabel label3=new JLabel();
    			
       admitCard.add (labelAdmit);
       deck.add (admitCard, "Admit");
       JComboBox studentBox = new JComboBox(nameArray);
    		  
    
       admitCard.add (studentBox);
    			
       studentBox.addActionListener(this); 
    
       JRadioButton a1 = new JRadioButton ("accept", false);
       JRadioButton r1 = new JRadioButton ("reject", true);
       JRadioButton a2 = new JRadioButton ("accept", false);
       JRadioButton r2 = new JRadioButton ("reject", true);
       JRadioButton a3 = new JRadioButton ("accept", false);
       JRadioButton r3 = new JRadioButton ("reject", true);
    
       ...
    
    if (event.getSource() == studentBox)
    {
       studentIndex  =studentBox.getSelectedIndex(); 
       school1 =arrayOfStudents[studentIndex].getUniversity(0);
       school2 =arrayOfStudents[studentIndex].getUniversity(1);
       school3 =arrayOfStudents[studentIndex].getUniversity(2);
       label1.setText (school1);
       label2.setText(school2);
       label3.setText (school3);
    						 
       school1 =arrayOfStudents[studentIndex].getUniversity(0);
       school2 =arrayOfStudents[studentIndex].getUniversity(1);
       school3 =arrayOfStudents[studentIndex].getUniversity(2);
       label1.setText (school1);
       label2.setText(school2);
       label3.setText (school3);

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You are adding the panel to the combobox's actionlisteners, then you
    respond to selections (actions) in that combo by setting the texts in some JLabels.

    If that doesn't work you must have missed out on something.

    I'm am unable to see the error here ?

    I have tested it also, so I am 100 % sure:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     *
     */
    
    public class SetLabelText extends JFrame implements ActionListener {
      JPanel centerPanel = new JPanel();
      JLabel lbl1 = new JLabel();
      JLabel lbl2 = new JLabel();
      JLabel lbl3 = new JLabel();
      JComboBox combo = new JComboBox();
    
    
      private void fillCombo() {
        combo.addItem("Test1");
        combo.addItem("Test2");
        combo.addItem("Test3");
        combo.addActionListener(this);
      }
      public SetLabelText(String title) {
        super(title);
        jbInit();
        fillCombo();
      }
      public void actionPerformed(ActionEvent e) {
        if (e.getSource()==combo) {
          String s=(String)combo.getSelectedItem();
          lbl2.setText(s);
        }
      }
    
      public static void main(String[] args) {
        SetLabelText slt = new SetLabelText("Labels");
        slt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        slt.setBounds(20,20,400,200);
        slt.setVisible(true);
      }
      private void jbInit() {
        lbl1.setBorder(BorderFactory.createLineBorder(Color.black));
        lbl1.setText("lbl1");
        lbl2.setBorder(BorderFactory.createLineBorder(Color.black));
        lbl2.setText("lbl2");
        lbl3.setBorder(BorderFactory.createLineBorder(Color.black));
        lbl3.setText("lbl3");
        combo.setPreferredSize(new Dimension(120, 25));
        this.getContentPane().add(centerPanel, BorderLayout.CENTER);
        centerPanel.add(lbl1, null);
        centerPanel.add(lbl2, null);
        centerPanel.add(lbl3, null);
        centerPanel.add(combo, null);
      }
    
    }
    eschew obfuscation

  3. #3
    Join Date
    Oct 2005
    Posts
    11
    OK, then it must be in my implimentation (again)...

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.ItemListener;
    
    public class ApplicationCentre extends JApplet implements ActionListener
    {
       Student[] arrayOfStudents;
       Object[] jListArray;
       String[] nameArray = new String[100];
       private JPanel main, deck;
       private JLabel labelName, labelAvg, counter, label1, label2, label3;
       private JButton input, admit, display, submit, admitSubmit;
       private Container container;
       private CardLayout cardManager;
       private JTextField name, avg;
       private JList schools;
       private int studentIndex;
       private int count=0;
       private int i = 0;
       private int mark = 0;
       private JComboBox studentBox;
       private String nameOfStudent;
       private JRadioButton a1, a2, a3, r1, r2, r3;
       private String school1, school2, school3;
       private JTextArea displayText;
       private String textHolder=("");
       // End of variables declaration
     
       public void init ()
       {
          initComponents();
          setSize(400, 210);
       }  
    	 
       public void initComponents() 
       {
           container = getContentPane();
           container.setLayout( new BorderLayout() );        
          
           // Creating instances of each item  
           main = new JPanel();
           input = new JButton("Input");
           admit = new JButton("Admit");
           display = new JButton("Display");
           arrayOfStudents = new Student[100];
    		  
           school1 = ("");
           school2 = ("");
           school3 = ("");
    	  
           // End Creation 
    		
          //set up main menu		
           main.setLayout (new GridLayout (3,1));
           main.add(input);
           main.add (admit);
           main.add (display);
    	  
           //set up deck
           cardManager = new CardLayout();
           deck = new JPanel(cardManager);
    		 		  		  
           //Input Card
           inputPane();
    		  
           //Display Card
           JLabel labelDisplay = new JLabel ("Display");
           JPanel displayCard = new JPanel();
           displayCard.setLayout (new BoxLayout (displayCard, BoxLayout.PAGE_AXIS));
           JTextArea displayText = new JTextArea ("This is a JTextArea");
    		  
    		  
           displayText.setFont(new Font("Serif", Font.ITALIC, 12)); 
           //textArea.setLineWrap(true); 
           //textArea.setWrapStyleWord(true); 
           displayText.setEditable(false);
    
           displayCard.add (labelDisplay);
           deck.add (displayCard, "Display");	
    		  
           JScrollPane areaScrollPane = new JScrollPane(displayText); 
           areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
           displayCard.add(areaScrollPane);	
           //displayCard.add(displayText);
    		  
           //add listeners
           input.addActionListener (this);
           admit.addActionListener (this);
           display.addActionListener (this);
    		  
           //listener for input
           submit.addActionListener (this);
    
           //add components to screen	
            container.add( main, BorderLayout.WEST);
            container.add(deck, BorderLayout.CENTER);
    		  
    		  
        }//end applicationCentre	  
    	
           //INPUT PANE
           public void inputPane(){
           labelName = new JLabel ("Name");
           name = new JTextField (10);
           labelAvg = new JLabel ("Average");
           avg = new JTextField (3);
           submit = new JButton ("Submit");
           counter = new JLabel ();
    		 
           //school array
           String schoolArray[] = {"Toronto", "York", "Western", "Brock", "Guelph", "Waterloo", "McGill", "Concordia", "Laval", "MacMaster"};
           schools = new JList (schoolArray);
           schools.setVisibleRowCount(5);
           schools.setSelectionMode (ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    			
           JPanel inputCard = new JPanel();
           inputCard.setLayout (new BoxLayout (inputCard, BoxLayout.PAGE_AXIS));
            inputCard.add (labelName);
            inputCard.add (name);
            inputCard.add (labelAvg);
            inputCard.add (avg);
            inputCard.add (new JScrollPane (schools));
            inputCard.add (submit);
            inputCard.add (counter);
    		  
            deck.add (inputCard, "Input");
    	
            }//end input pane
    		
           //admit pane
            public void admitPane(){
    		
           JLabel labelAdmit = new JLabel ("Admit");
           JPanel admitCard = new JPanel();
           admitCard.setLayout (new BoxLayout (admitCard, BoxLayout.PAGE_AXIS));
           JButton admitSubmit = new JButton ("Submit");
    			
           JLabel label1=new JLabel();
           JLabel label2=new JLabel();
           JLabel label3=new JLabel();
    			
           admitCard.add (labelAdmit);
           deck.add (admitCard, "Admit");
           JComboBox studentBox = new JComboBox(nameArray);
    		  
           admitCard.add (studentBox);
    			
           studentBox.addActionListener(this);
           admitSubmit.addActionListener(this); 
           JRadioButton a1 = new JRadioButton ("accept", false);
           JRadioButton r1 = new JRadioButton ("reject", true);
           JRadioButton a2 = new JRadioButton ("accept", false);
           JRadioButton r2 = new JRadioButton ("reject", true);
           JRadioButton a3 = new JRadioButton ("accept", false);
           JRadioButton r3 = new JRadioButton ("reject", true);
    			
           ButtonGroup radioGroup1 = new ButtonGroup();
           radioGroup1.add(a1);
           radioGroup1.add(r1);
    
           ButtonGroup radioGroup2 = new ButtonGroup();
           radioGroup2.add(a2);
           radioGroup2.add(r2);
    			
           ButtonGroup radioGroup3 = new ButtonGroup();
           radioGroup3.add(a3);
           radioGroup3.add(r3);
    			
           admitCard.add (label1);
           admitCard.add (a1);
           admitCard.add (r1);
           admitCard.add (label2);
           admitCard.add (a2);
           admitCard.add (r2);	
           admitCard.add (label3);
           admitCard.add (a3);
           admitCard.add (r3);
    
           admitCard.add (admitSubmit);
    
           }//end admit pane
    			  
           //buttons!!
           public void actionPerformed (ActionEvent event)  {
    	  
           //show cards
           if (event.getSource() == input)
              cardManager.show(deck, "Input");
           else if (event.getSource ()== admit)
              {
               admitPane();
               cardManager.show (deck, "Admit");
              } //end admit if
    
           else if (event.getSource()==display){
               for ( int r = 0; r < count; r++ ) 
    	{ 
                     textHolder = (textHolder+arrayOfStudents[r]+"\n"); 
                    }
    		  
             //displayText.setText(textHolder); //Why is this line giving me so much grief???
            cardManager.show (deck, "Display");
           }
    
           //submit button
           if (event.getSource() == submit){
    	String s1=name.getText();
    	String s2=avg.getText();
    	mark=Integer.parseInt(s2);
    	Object [] o=schools.getSelectedValues();
    				
    	String choice1 = (String)o[0];
    	String choice2 = (String)o[1];
    	String choice3 = (String)o[2];
    	arrayOfStudents[count]=new Student(s1, mark, choice1, choice2, choice3);
    	nameArray[count]=new String(s1); 
    	name.setText("");
    	avg.setText("");
    	schools.clearSelection();
    	count ++;
    	counter.setText ("Student " + count + " out of 100");
    	}//end submit button
    				
           //jComboBox
           if (event.getSource() == studentBox)
           {
               //u1.setText(school1);
               studentIndex  =studentBox.getSelectedIndex(); 
               school1 =arrayOfStudents[studentIndex].getUniversity(0);
               school2 =arrayOfStudents[studentIndex].getUniversity(1);
               school3 =arrayOfStudents[studentIndex].getUniversity(2);
               label1.setText (school1);
               label2.setText(school2);
               label3.setText (school3);
    	
               //studentIndex  =studentBox.getSelectedIndex(); 
               school1 =arrayOfStudents[studentIndex].getUniversity(0);
               school2 =arrayOfStudents[studentIndex].getUniversity(1);
               school3 =arrayOfStudents[studentIndex].getUniversity(2);
               label1.setText (school1);
               label2.setText(school2);
               label3.setText (school3);
               }//end jcombobox
    				
         if (event.getSource()==admitSubmit)
          {
             //Student.setAcceptance();
          }//end admitSubmit
       }//end button handler
    
    }

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I cannot find anywhere in the code where the combobox is being added to any
    container, - so it's invisible.

    Once you have done that you must add one or more of the other objects to the
    combobox's list of actionlisteners.
    eschew obfuscation

  5. #5
    Join Date
    Oct 2005
    Posts
    11
    The comboBox is added above the buttonGroups.

    JComboBox studentBox = new JComboBox(nameArray);
    admitCard.add (studentBox);

    It shows when compiled. The only problem is that when a selection is made in the combobox, it does not change the JLabels as it should.

Similar Threads

  1. Display Value in JcomboBox
    By daina in forum Java
    Replies: 7
    Last Post: 10-10-2007, 08:21 PM
  2. Jcombobox value
    By daina in forum Java
    Replies: 3
    Last Post: 10-03-2005, 12:27 PM
  3. Replies: 1
    Last Post: 08-01-2002, 10:48 AM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  5. After click JComboBox, go back to first item
    By Lim Wing Hoe in forum Java
    Replies: 0
    Last Post: 12-30-2000, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center