Java JToggleButton with Example using NetBeans
Java JToggleButton
The implementation of a Java JToggleButton differs from the JButton in that the button can have two states: selected or not selected. If the Java JToggleButton was clicked or set to selected, the button gets a different background color so that you can see that it has been selected. If you click again, the appearance changes back to the initial state.
Java JToggleButton Constructors:
In addition to the standard constructor, the JToggleButton class has a few other constructors that we would like to explain in more detail in the following table.
Constructor | Description |
JToggleButton(Action a) | Here, a Java JToggleButton is directly assigned an action that is to be triggered by clicking the button. We will go into more detail about actions later. |
JToggleButton(Icon icon) | This constructor creates a java JToggleButton with an Icon. This usage is very common in toolbars ( JToolBar ). |
JToggleButton(String text) | This constructor is used to set the text of the Java JToggleButton as a  label. |
JToggleButton(String text, Icon icon) | With the help of this constructor, both a text and an icon can be assigned to the Java JToggleButton. The text appears behind the icon. |
JToggleButton(Icon icon, boolean selected) | This constructor creates a java JToggleButton with an Icon. In addition, the status of the button is specified. If the value true is passed as the second parameter, the button has the status “pressed”. |
JToggleButton(String text, boolean selected) | Here a JToggleButton is created with a text as a label. In addition, the status of the button is specified. If the value true is passed as the second parameter, the button has the status “pressed”. |
JToggleButton(String text, Icon icon, boolean selected) | Here the previous constructor is extended by an icon. |
The status of the JToggleButton can be queried using the isSelected function, which returns a boolean value (true for selected and false for unselected).
Example: how to use Java JToggleButton using NetBeans:Â
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.mycompany.guiexamples; import javax.swing.*; /** * * @author Fawadkhan */ public class JToggleButtonExample { public static void main(String[] args) { JFrame myFrame = new JFrame("JToggleButton Testing Example"); myFrame.setSize(400, 350); myFrame.setLayout(null); // here i Create a JToggleButton JToggleButton toggleButton = new JToggleButton("Press me", false); // this statement is used to Set the position and size of a jtogglebutton toggleButton.setBounds(130, 150, 120, 40); //here i am adding the button to the frame myFrame.add(toggleButton); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setLocationRelativeTo(null); myFrame.setVisible(true); } } |
I Â have created a Java JToggleButton using the sixth constructor from the table above. Since I set the status to false via the second parameter, the button is displayed as unselected when the program starts. If you click on the button, the status changes to “selected”.
Screenshot of the unselected button:
Screenshot of the selected button:
As you can see, the button’s background color changes depending on the state.
Example: How to change Container Color using Java JToggleButton:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.mycompany.guiexamples; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; /** * * @author Fawadkhan */ public class JToggleButtonExample extends JFrame implements ItemListener { private Color Default; Container c; public static void main(String[] args) { new JToggleButtonExample(); } private JToggleButton button; JToggleButtonExample() { setTitle("JToggleButton with ItemListener Example"); setLayout(null); setJToggleButton(); setAction(); setSize(400, 400); button.setBounds(100, 150, 100, 60); button.setFont(new Font("Calibri", Font.BOLD, 30)); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c=getContentPane(); Default=c.getBackground(); //button.setBounds(60, 150, 388, 118); } 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"); c.setBackground(Color.red); } else { button.setText("ON"); c.setBackground(Default); } } } |
Output: