This is a Java Program to Show the Functioning of a Toggle Button
We have to write a program in Java such that it shows the functioning of a toggle button.
Toggle Button : It is a two-state button that allows the user to switch the button either on or off, that is the status of the button can be switched between selected and deselected respectively.
For showing the function of Toggle Button, we can have the following different sets of input and output.
1. To Display the Toggle Button:
When the program is being executed, it is expected that a toggle button is created and displayed.
2. To Display the Selected Status of Toggle Button:
When the toggle button is selected, the expected output is "Status of Toggle Button : SELECTED"
3. To Display the Deselected Status of Toggle Button:
When the toggle button is deselected, the expected output is "Status of Toggle Button : DESELECTED"
1. Create a frame and a label.
2. Create a Toggle Button and add ItemListener to it.
3. When any action is performed on the toggle button, display the appropriate message.
Here is source code of the Java Program to show the functioning of Toggle Button. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/* Java Program to show the functioning to Toggle Button*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JToggleButton;
class Toggle implements ItemListener
{
static JLabel text;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame = new JFrame("Toggle Button");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
frame.setLayout(null);
//Create an object
Toggle obj = new Toggle();
//Create a Toggle Button
JToggleButton button = new JToggleButton("Toggle Button");
button.setBounds(150,100,200,100);
button.addItemListener(obj);
frame.add(button);
//Create a label
text = new JLabel();
text.setBounds(150,250,300,100);
text.setText("Click on the Toggle Button");
frame.add(text);
//Display the frame
frame.setVisible(true);
}
//Function to display the status of toggle button
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
text.setText("Status of Toggle Button : SELECTED");
else
text.setText("Status of Toggle Button : DESELECTED");
}
}
1. To create a toggle button use JToggleButton class.
2. Add ItemListener to the toggle button.
3. Use method getStateChange to get the state change of toggle button.
Here’s the run time test case to show the functioning of toggle button.
Test case 1 – To View the Toggle Button.
Test case 2 – To View the Selected status of Toggle Button.
Test case 3 – To View the Deselected status of Toggle Button.
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Check Java Books