The event sequence for JToggleButton: ActionListener, ItemListener and ChangeListener : JToggleButton « Swing « Java Tutorial






The event sequence for JToggleButton: ActionListener, ItemListener and ChangeListener
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JToggleButtonEventListenerSequence {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting Toggle");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Toggle Button");
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
        boolean selected = abstractButton.getModel().isSelected();
        System.out.println("Action - selected=" + selected + "\n");
      }
    };
    // Define ChangeListener
    ChangeListener changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent changeEvent) {
        AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
        ButtonModel buttonModel = abstractButton.getModel();
        boolean armed = buttonModel.isArmed();
        boolean pressed = buttonModel.isPressed();
        boolean selected = buttonModel.isSelected();
        System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
      }
    };
    // Define ItemListener
    ItemListener itemListener = new ItemListener() {
      public void itemStateChanged(ItemEvent itemEvent) {
        int state = itemEvent.getStateChange();
        if (state == ItemEvent.SELECTED) {
          System.out.println("Selected");
        } else {
          System.out.println("Deselected");
        }
      }
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
  }
}
Changed: true/false/false
Changed: true/true/false
Changed: true/true/true
Selected
Changed: true/false/true
Action - selected=true

Changed: false/false/true








14.8.JToggleButton
14.8.1.Creating JToggleButton Components
14.8.2.JToggleButton is a button that has two states. Pressed and not pressed.
14.8.3.Listening to JToggleButton Events with an ActionListenerListening to JToggleButton Events with an ActionListener
14.8.4.Listening to JToggleButton Events with an ItemListenerListening to JToggleButton Events with an ItemListener
14.8.5.Listening to JToggleButton Events with a ChangeListenerListening to JToggleButton Events with a ChangeListener
14.8.6.The event sequence for JToggleButton: ActionListener, ItemListener and ChangeListenerThe event sequence for JToggleButton: ActionListener, ItemListener and ChangeListener
14.8.7.Customizing JToggleButton Look and Feel
14.8.8.This class represents the toggle buttons used in toolbars.