Java Swing Tutorial - Java JComboBox .removeActionListener (ActionListener l)








Syntax

JComboBox.removeActionListener(ActionListener l) has the following syntax.

public void removeActionListener(ActionListener l)

Example

In the following code shows how to use JComboBox.removeActionListener(ActionListener l) method.

import java.awt.BorderLayout;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//from w  ww  .  j a va  2  s.  co  m
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Main {

  public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Command: " + actionEvent.getActionCommand());
        ItemSelectable is = (ItemSelectable)actionEvent.getSource();
        System.out.println(", Selected: " + selectedString(is));
      }
    };
    comboBox.addActionListener(actionListener);
    comboBox.removeActionListener(actionListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

  }
  static private String selectedString(ItemSelectable is) {
    Object selected[] = is.getSelectedObjects();
    return ((selected.length == 0) ? "null" : (String)selected[0]);
  }  
}