Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A JToggleButton is a button with two states. Selected and unselected are the two statuses. This class is subclassed by the JRadioButton and JCheckBox classes. The toggle button toggles between being pressed and unpressed when the user presses it. JToggleButton is a button that allows you to choose from a list of options. Actions can be used to configure and control buttons to some extent. Beyond explicitly creating a button, using an Action with a button offers many advantages.
The workflow of JToggleButton is something like the below:
A JToggleButton is an extension of AbstractButton that can be used to depict toggleable ON/OFF buttons.
When you push JToggleButton for the first time, it stays pressed, and you can only release it when you press it again.
Each time a JToggleButton is pressed, an ActionEvent is generated.
An ItemEvent can be generated by a JToggleButton, which is used by components that support the selection concept. A JToggleButton is selected when it is pressed in. It is deselected when it is popped out.
It would help if you implemented the ItemListener interface to handle item events. When the status of an item changes, this interface defines the itemStateChanged() method.
The selected() method on the button that created the event can be used to determine the state of a toggle button.
Nested Classes
Modifier and Type
Class
Description
protected class
JToggleButton.AccessibleJToggleButton
For the JToggleButton class, this class implements accessibility support.
Static class
JToggleButton.ToggleButtonModel
ToggleButton is a model.
Constructors
JToggleButton(): Creates a toggle button with no text or image that is initially unselected.
JToggleButton(Action a): This method creates a toggle button with properties based on the Action.
JToggleButton(Icon icon): Creates a toggle button with the provided image but no text that is initially unselected.
JToggleButton(Icon icon, boolean selected): Creates a toggle button with the provided image and selection status, but no text.
JToggleButton(String text): Creates an unselected toggle button with the provided text.
JToggleButton(String text, boolean selected): Creates a toggle button with the supplied text and selection status.
JToggleButton(String text, Icon icon): This method creates an unselected toggle button with the provided text and image.
JToggleButton(String text, Icon icon, boolean selected): Creates a toggle button with the supplied text, image, and selection status.
getAccessibleContext(): Returns the AccessibleContext that this JToggleButton is associated with.
getUIClassID(): Returns the name of the l&f class that renders this component as a string.
paramString(): Returns a string representation of this JToggleButton.
updateUI(): Resets the value of the UI property to the current look and feel.
Example
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonExample extends JFrame implements ItemListener {
public static void main(String[] args) {
new JToggleButtonExample();
}
private JToggleButton button;
JToggleButtonExample() {
setTitle("JToggleButton with ItemListener Example");
setLayout(new FlowLayout());
setJToggleButton();
setAction();
setSize(200, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setJToggleButton() {
button = new JToggleButton("ON");
add(button);
}
private void setAction() {
button.addItemListener(this);
}
public void itemStateChanged(ItemEvent eve) {
if (button.isSelected())
button.setText("OFF");
else
button.setText("ON");
}
}
Frequently Asked Questions
What is the difference between a radio button and a JToggleButton?
The fundamental distinction between JRadioButton and JCheckBox is that JRadioButton is a group of buttons in which only one button may be selected at a time. In contrast, JCheckBox is a group of checkboxes in which several items can be selected simultaneously.
What's the distinction between a toggle and a switch?
Because toggle switches and toggle buttons both manage states, it's simple for designers to mix them up, but there's a big distinction. Toggle switches are used to change system states, while toggle buttons are used to change context.
What is JToggleButton?
A JToggleButton is a button with two states. Selected and unselected are the two statuses. This class is subclassed by the JRadioButton and JCheckBox classes. The toggle button toggles between being pressed and unpressed when the user presses it.
What are the different methods used in JtoggleButton?
There are four types of methods used:
getAccessibleContext()
getUIClassID()
paramString()
UpdateUI()
In Java Swing, what is JscrollPane?
A JscrollPane is used to make a component's view scrollable. We utilize a scroll pane to display a huge component or a component whose size can change dynamically when the screen size is constrained.
Conclusion
This article taught us about JToggleButton, its different constructors, and the methods used. Lastly, we understood different functionalities with the help of an example. That’s all from the article. I hope you all like it.