Listening to JToggleButton Events with an ActionListener : JToggleButton « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » JToggleButton 
14.8.3.Listening to JToggleButton Events with an ActionListenerPrevious/Next
Listening to JToggleButton Events with an ActionListener
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class JToggleButtonAction {
  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 = (AbstractButtonactionEvent.getSource();
        boolean selected = abstractButton.getModel().isSelected();
        System.out.println("Action - selected=" + selected + "\n");
      }
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300125);
    frame.setVisible(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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.