Java JToggleButton

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JToggleButton

JToggleButton is used to create toggle button, it is two-states button to switch on or off.

Nested Classes

Modifier and Type Class Description
protected class JToggleButton.AccessibleJToggleButton This class implements accessibility support for the JToggleButton class.
static class JToggleButton.ToggleButtonModel The ToggleButton model

Constructors

Constructor Description
JToggleButton() It creates an initially unselected toggle button without setting the text or image.
JToggleButton(Action a) It creates a toggle button where properties are taken from the Action supplied.
JToggleButton(Icon icon) It creates an initially unselected toggle button with the specified image but no text.
JToggleButton(Icon icon, boolean selected) It creates a toggle button with the specified image and selection state, but no text.
JToggleButton(String text) It creates an unselected toggle button with the specified text.
JToggleButton(String text, boolean selected) It creates a toggle button with the specified text and selection state.
JToggleButton(String text, Icon icon) It creates a toggle button that has the specified text and image, and that is initially unselected.
JToggleButton(String text, Icon icon, boolean selected) It creates a toggle button with the specified text, image, and selection state.

Methods

Modifier and Type Method Description
AccessibleContext getAccessibleContext() It gets the AccessibleContext associated with this JToggleButton.
String getUIClassID() It returns a string that specifies the name of the l&f class that renders this component.
protected String paramString() It returns a string representation of this JToggleButton.
void updateUI() It resets the UI property to a value from the current look and feel.

JToggleButton Example

  1. import java.awt.FlowLayout;  
  2. import java.awt.event.ItemEvent;  
  3. import java.awt.event.ItemListener;  
  4. import javax.swing.JFrame;  
  5. import javax.swing.JToggleButton;  
  6.   
  7. public class JToggleButtonExample extends JFrame implements ItemListener {  
  8.     public static void main(String[] args) {  
  9.         new JToggleButtonExample();  
  10.     }  
  11.     private JToggleButton button;  
  12.     JToggleButtonExample() {  
  13.         setTitle("JToggleButton with ItemListener Example");  
  14.         setLayout(new FlowLayout());  
  15.         setJToggleButton();  
  16.         setAction();  
  17.         setSize(200200);  
  18.         setVisible(true);  
  19.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  20.     }  
  21.     private void setJToggleButton() {  
  22.         button = new JToggleButton("ON");  
  23.         add(button);  
  24.     }  
  25.     private void setAction() {  
  26.         button.addItemListener(this);  
  27.     }  
  28.     public void itemStateChanged(ItemEvent eve) {  
  29.         if (button.isSelected())  
  30.             button.setText("OFF");  
  31.         else  
  32.             button.setText("ON");  
  33.     }  
  34. }  

Output

Java JTogglebutton