We help IT Professionals succeed at work.

Which methods gives the selected item in a combobox and which combobox was selected

Vanavah Edwards
on
Below is the Java code I have written.  I cannot seem to get the selected items print to screen or determine which combobox (cb1 or cb2) was chosen.  Please help me.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class thehandler extends JFrame {

  private JComboBox cb1, cb2;

  public static void main(String[] args) {
    new thehandler();
  }

  public thehandler() {
	 JFrame f = new JFrame("I'm Listening");
    f.setSize(200, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();

    ClickListener cl = new ClickListener();
    String[] jcbt1 = {"1111", "2222", "3333"};
    String[] jcbt2 = {"aaaa", "bbbb", "cccc"};
    JComboBox cb1 = new JComboBox(jcbt1);
    JComboBox cb2 = new JComboBox(jcbt2);
    cb1.addActionListener(cl);
    cb2.addActionListener(cl);

    p.add(cb1);
    p.add(cb2);
    f.add(p);
    f.setVisible(true);
  }

  private class ClickListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
//		System.out.println(e.getSource());
      if (e.getSource()== "1111") {
          System.out.println("You've seleccted - 1111");
    }
  }
}
}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class thehandler extends JFrame {

  private JComboBox cb1, cb2;

  public static void main(String[] args) {
    new thehandler();
  }

  public thehandler() {
	 JFrame f = new JFrame("I'm Listening");
    f.setSize(200, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();

    ClickListener cl = new ClickListener();
    String[] jcbt1 = {"1111", "2222", "3333"};
    String[] jcbt2 = {"aaaa", "bbbb", "cccc"};
    cb1 = new JComboBox(jcbt1);
     cb2 = new JComboBox(jcbt2);
    cb1.addActionListener(cl);
    cb2.addActionListener(cl);

    p.add(cb1);
    p.add(cb2);
    f.add(p);
    f.setVisible(true);
  }

  private class ClickListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
	//System.out.println(e.getSource());
      if (e.getSource().equals(cb1)) {
          System.out.println("You've seleccted - 1111");
    }
  }
}
}

Open in new window

Awarded 2011
Awarded 2011

Commented:
(1) You need to have comboboxes as instance variables
(2) you need to check if(e.getSource().equals(cb1))  like that
Awarded 2011
Awarded 2011

Commented:
the source of the event is not a string "111" but an object cb1 - so you need to compare e.getSource() with the instance of cmobobox obeject
Awarded 2011
Awarded 2011

Commented:
General impertant rule :

in Java you can use ==  for comparison only of primitives - int, long, double
or when comparing objects, strings to null if(string == null) - thas OK

if you compare for equality instances of objects (including strings) you always (with the rraest specvial case exceptions)
use method equals() , not ==

== is very bad, beuase sometimes it still may work, but in some other time it will not work;
So the rule - never use it for object comparisons
Awarded 2011
Awarded 2011

Commented:


In your original code , although you declared cb1 and cb2 as instance variables:
 private JComboBox cb1, cb2;

in fact whne in the cionstructor you declare them again:
JComboBox cb1 = new JComboBox(jcbt1);
    JComboBox cb2 = new JComboBox(jcbt2);

so you create in gfact local variables cb1 and cb2 which point to your
checkbox, and your instance cb1 and cb2 remain null;s

so I changed this to

cb1 = new JComboBox(jcbt1);
    cb2 = new JComboBox(jcbt2);

Now the actual instance variables are used and they are accesible in
another method, wherease local variables which would be
declared in the constructor would have been accesible only in the cionstructor
so equals(cb1) in the action method could not have wrked





Author

Commented:
I am sorry it so much time to respond.  I  was away.  I tried your suggestion and made the changes and it worked.  However, the final question remains unaswered, how do I determine which element in the combobox was selected.
Awarded 2011
Awarded 2011
Commented:
like aalwys with comboboxes
cb1.getSelectedItem()

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class thehandler extends JFrame {

  private JComboBox cb1, cb2;

  public static void main(String[] args) {
    new thehandler();
  }

  public thehandler() {
	 JFrame f = new JFrame("I'm Listening");
    f.setSize(200, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();

    ClickListener cl = new ClickListener();
    String[] jcbt1 = {"1111", "2222", "3333"};
    String[] jcbt2 = {"aaaa", "bbbb", "cccc"};
    cb1 = new JComboBox(jcbt1);
     cb2 = new JComboBox(jcbt2);
    cb1.addActionListener(cl);
    cb2.addActionListener(cl);

    p.add(cb1);
    p.add(cb2);
    f.add(p);
    f.setVisible(true);
  }

  private class ClickListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
	//System.out.println(e.getSource());
      if (e.getSource().equals(cb1)) {
          System.out.println("You've seleccted - cb1 combobox with selected item" + cb1.getSelectedItem());
    }
  }
}
}

Open in new window


Outpurt:

You've seleccted - cb1 combobox with selected item3333

Open in new window

Author

Commented:
Thank you very much.  This solves this problem.  I will now award your points.  I will post now another problem in which I want to call the the actionlisteners and actionperformers from a separate class where my comboboxs is in another class

Author

Commented:
He was prompt, fast and spot on and assisted me with my code