Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.15. Toggle buttons

5.15. Toggle buttons
Last updated: 25 January 2013.

This tutorial will show you how to use toggle buttons in Java.

A JToggleButton is a button which can be in one of two states: selected or deselected. Generally, a toggle button is associated with a listener to handle selection and deselection. The code below shows a basic use of a toggle button that simply prints a message into a label when its state changes:

import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;

public final class ToggleButtonExample extends JFrame {

   
public ToggleButtonExample() {
         
setTitle("ToggleButtonExample");
          setSize
(350, 100);
          setLocationRelativeTo
(null);

          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));

         
// The label ------------------------------------------------------------------------------------
         
final JLabel label = new JLabel();

          Dimension labelDimension =
new Dimension(150, 40);
          label.setPreferredSize
(labelDimension);
          label.setMinimumSize
(labelDimension);
          label.setMaximumSize
(labelDimension);
          label.setBorder
(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
          label.setHorizontalAlignment
(SwingConstants.CENTER);


         
// The toggle button -------------------------------------------------------------------------
         
JToggleButton toggleButton = new JToggleButton("ToggleButton", false);

          Dimension buttonDimension =
new Dimension(150, 50);
          toggleButton.setPreferredSize
(buttonDimension);
          toggleButton.setMinimumSize
(buttonDimension);
          toggleButton.setMaximumSize
(buttonDimension);

          toggleButton.addItemListener
(new ItemListener() {
               
public void itemStateChanged(ItemEvent e) {
                     
if(e.getStateChange() == ItemEvent.SELECTED)
                           
label.setText("Button selected");
                     
else
                           
label.setText("Button deselected");
               
}
          })
;


         
// Add the button and the label to the content pane
         
contentPane.add(Box.createHorizontalGlue());
          contentPane.add
(toggleButton);
          contentPane.add
(Box.createHorizontalGlue());
          contentPane.add
(label);
          contentPane.add
(Box.createHorizontalGlue());

          add
(contentPane);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new ToggleButtonExample();
               
}
          })
;
   
}
}


Here is the result:


Toggle button


When the user clicks the toggle button, its state is displayed in the label (Button selected or deselected). As you can see, toggle buttons are quite simple to use. However, toggle buttons are rarely used as shown in this tutorial. In fact, the class JToggleButton is the base class that defines features used by the more commonly used subclasses JCheckBox and JRadioButton. You will learn how to use those subclasses in the next tutorials.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.15. Toggle buttons
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.16. Check boxes

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7