Updated March 17, 2023
Introduction to JToggleButton
JToggleButton is simply a two-state button selected and de-selected. It is a Swing component. You must have heard about the checkbox and radio button. It is exactly the same. In fact, JRadioButton and JCheckBox are subclasses of the JToggleButton class. Usage of JToggleButton is, to register the choice of the user between two choices.
For example, We can have multiple JToggleButtons specifying the language name on each button and the user will select/de-select (default de-selected) each button to specify that he/she knows/doesn’t know the respective language. JToggleButton is in the deselected state by default.
Declaration of JToggleButton
public class JToggleButton
extends AbstractButton
implements Accessible
Where,
- AbstractButton defines common functionalities of Buttons and MenuItems.
- Accessible is the main interface for the accessibility package. It contains a method “getAccessibleContext” which returns the AccessibleContext associated with this button.
Constructors of JToggleButton
Below are the different constructors of JToggleButton:
- JToggleButton(): This constructor creates an unselected toggle button with no text or image.
- JToggleButton(Action a): This constructor creates a toggle button having properties from action supplied.
- JToggleButton(String text): This constructor creates an unselected toggle button with the specified text and no image.
- JToggleButton(Icon icon): This constructor creates an unselected toggle button with no text and specified image.
- JToggleButton(String text, Icon icon): This constructor creates an unselected toggle button with the specified text and the specified image.
- JToggleButton(String text, boolean selected): This constructor creates a toggle button with no image, specified text, and the specified selection state.
- JToggleButton(Icon icon, boolean selected): This constructor creates a toggle button with no text, specified image, and specified selection state.
- JToggleButton(String text, Icon icon, boolean selected): This is the constructor where all three properties can be provided – selection state, text and image.
Methods
Below are the different methods used:
- public void setText(String text): This method sets the text which appears on the button.
- public void setSelected(boolean b): This method sets the state of the button. If b is true, the button will appear selected and if b is false, the button will appear de-selected.
- public void setEnabled(boolean b): This method is used to disable the button. If b is false, the button will be grayed out and will no more be clickable.
- public void setIcon(Icon defaultIcon): This method sets the icon for this button. This icon will be used as a “pressed” and “disabled” icon if no “pressed” icon is set explicitly.
- public void setPressedIcon(Icon pressedIcon): This method sets the “pressed” icon for this button. This icon will be shown at the moment when the user presses the button.
- public AccessibleContext getAccessibleContext(): This method returns the accessible context associated with this button.
- protected String paramString(): This method returns string representation of this button. This method is for debugging purposes. Return value may be empty but not null.
- public String getUIClassID(): This method returns the name of look and feel (l&f) class of this button.
- public void updateUI(): Resets the look and feel of this button.
- public void addActionListener(ActionListener l): This method attached an action listener to this button so that any action performed on this button can be catched at the backend.
- public void addItemListener(ItemListener l): This method attaches an item listener to this button so that the selection and deselection of this button can be handled at the backend.
Program to Implement JToggleButton
Following is a program to implement jtogglebutton:
Code:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
public class JToggleButtonDemo {
public static void main(String[] args) {
JFrame app = new JFrame();
JToggleButton englishLanguage = new JToggleButton("English", true);
JToggleButton hindiLanguage = new JToggleButton();
JToggleButton marathiLanguage = new JToggleButton();
JToggleButton tamilLanguage = new JToggleButton();
app.setSize(400, 400);
app.setLayout(new GridLayout(6, 1));
app.setVisible(true);
englishLanguage.setText("English");
englishLanguage.setBackground(new Color(255, 66, 0));
hindiLanguage.setText("Hindi");
hindiLanguage.setBackground(new Color(255, 66, 0));
marathiLanguage.setText("Marathi");
marathiLanguage.setBackground(new Color(255, 66, 0));
tamilLanguage.setText("Tamil");
tamilLanguage.setBackground(new Color(255, 66, 0));
app.add(englishLanguage);
app.add(hindiLanguage);
app.add(marathiLanguage);
app.add(tamilLanguage);
JLabel action = new JLabel();
app.add(action);
JLabel status = new JLabel("User knows : English");
app.add(status);
ItemListener listener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
StringBuilder statusString = new StringBuilder("User knows :");
if (englishLanguage.isSelected()) {
statusString.append(" English");
}
if (hindiLanguage.isSelected()) {
statusString.append(" Hindi");
}
if (marathiLanguage.isSelected()) {
statusString.append(" Marathi");
}
if (tamilLanguage.isSelected()) {
statusString.append(" Tamil");
}
if (englishLanguage.isSelected() == false && hindiLanguage.isSelected() == false
&& marathiLanguage.isSelected() == false && tamilLanguage.isSelected() == false) {
status.setText("User doesn't know any language !!");
} else {
status.setText(statusString.toString());
}
}
};
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JToggleButton temp = (JToggleButton) e.getSource();
if (temp.isSelected()) {
action.setText("Current action : " + temp.getActionCommand() + " is selected");
} else {
action.setText("Current action : " + temp.getActionCommand() + " is de-selected");
}
}
};
englishLanguage.addItemListener(listener);
englishLanguage.addActionListener(actionListener);
hindiLanguage.addItemListener(listener);
hindiLanguage.addActionListener(actionListener);
marathiLanguage.addItemListener(listener);
marathiLanguage.addActionListener(actionListener);
tamilLanguage.addItemListener(listener);
tamilLanguage.addActionListener(actionListener);
}
}
Explanation:
- This program is very simple and self-explanatory. It collects the user information about which all languages he/she knows.
- First, we have created a frame of size 400 by 400 pixels and having one column and five rows.
- Created four toggle buttons for four different languages – English, Hindi, Marathi and Tamil and added to the container frame.
- We have set the background color of these buttons to red (until deselected).
- Then added a label in the fifth row in the frame to show the status of the application.
- Initially, a button for the English language is selected using the constructor of JToggleButton which means the user knows English language by default and all other buttons are deselected.
- To specify the known language, the user will have to click on (select) the specific button. Users can click again on selected button to de-select it.
- When a button is selected or de-selected, an event is fired and cached in an ItemListener and status is changed accordingly.
- Also, to check the current action, an ActionListener is created and attached to each button so that when a language is selected or deselected, performed action is shown to the user.
- Used can select or de-select one or more languages and see the status accordingly.
Output:
1. This is our application in which the English language is selected by default.
2. User has selected “Marathi” language and status and action are changed accordingly.
3. User has de-selected “English” language and status and action are changed accordingly.
4. User has de-selected “Marathi” language and status is changed accordingly.
Conclusion
It is an important component in Swing which makes it easy to specify a choice between two choices or answers like yes or no as depicted in the example. We can also use checkboxes or radio buttons which are subclasses of JToggleButton.
Recommended Articles
This is a guide to JToggleButton. Here we discuss the constructors and methods of JToggleButton along with its program. You may also look at the following articles to learn more –