2

I created a combo box:

JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"<Select a Team>", "X", "Y", "Z"}));

When I invoke System.out.println(comboBox.getSelectedItem().toString()); in order to see the selected item, I only see <Select a Team> .

How can I print the value of the combo box each time I change it's value? (I tried to search for how to use a listener or callback functions but didn't understand how to implement it for my goals).

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • [Duplicate](http://stackoverflow.com/questions/58939/jcombobox-selection-change-listener) – elias Nov 09 '12 at 16:30

2 Answers2

1

This will add an ActionListener to your combobox:

comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println(comboBox.getSelectedItem().toString());
    }
});
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • Thank you. If I choose to implement it that way, I need to change the declaration of my comboBox to final in order to use it in an inner class. Is this the only way to do it? – Maroun Nov 09 '12 at 16:35
  • The other way is to declare comboBox as instance variable. – Dan D. Nov 09 '12 at 16:36
0

Try with this code . I have commented out the lines may like difficult to you.

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

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener{
  JComboBox combo = new JComboBox();

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    combo.addItem("A");
    combo.addItem("H");
    combo.addItem("P");
    combo.setEditable(true);
    System.out.println("#items=" + combo.getItemCount());

    //this line is telling the combox that call this class's 
    //actionPerformed method whenver any interesting thing happens
    combo.addActionListener(this);

    getContentPane().add(combo);
    pack();
    setVisible(true);
  }

  //here is the method
  //it will be called every time by combo object whenver any interesting thing happens
  public void actionPerformed(ActionEvent e) {
    System.out.println("Selected index=" + combo.getSelectedIndex()
        + " Selected item=" + combo.getSelectedItem());
      }


  public static void main(String arg[]) {
    new Main();
  }
}

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

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener{
  JComboBox combo = new JComboBox();

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    combo.addItem("A");
    combo.addItem("H");
    combo.addItem("P");
    combo.setEditable(true);
    System.out.println("#items=" + combo.getItemCount());

    //this line is telling the combox that call this class's 
    //actionPerformed method whenver any interesting thing happens
    combo.addActionListener(this);

    getContentPane().add(combo);
    pack();
    setVisible(true);
  }

  //here is the method
  //it will be called every time by combo object whenver any interesting thing happens
  public void actionPerformed(ActionEvent e) {
    System.out.println("Selected index=" + combo.getSelectedIndex()
        + " Selected item=" + combo.getSelectedItem());
      }


  public static void main(String arg[]) {
    new Main();
  }
}
MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45