Java Program to Show the Functioning of a Toggle Button

This is a Java Program to Show the Functioning of a Toggle Button

Problem Description

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.

Expected Input and Output

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"
Problem Solution

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.

Program/Source Code

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.

  1. /* Java Program to show the functioning to Toggle Button*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.JToggleButton;
  6. class Toggle implements ItemListener
  7. {
  8.     static JLabel text;
  9.     //Driver function
  10.     public static void main(String args[])
  11.     {
  12. 	//Create a frame
  13. 	JFrame frame = new JFrame("Toggle Button");
  14. 	frame.setSize(500,500);
  15. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. 	frame.getContentPane().setBackground(Color.white);
  17. 	frame.setLayout(null);
  18. 	//Create an object
  19. 	Toggle obj = new Toggle();
  20. 	//Create a Toggle Button
  21. 	JToggleButton button = new JToggleButton("Toggle Button");
  22. 	button.setBounds(150,100,200,100);
  23. 	button.addItemListener(obj);
  24. 	frame.add(button);
  25. 	//Create a label
  26. 	text = new JLabel();
  27. 	text.setBounds(150,250,300,100);
  28. 	text.setText("Click on the Toggle Button");
  29. 	frame.add(text);
  30. 	//Display the frame
  31. 	frame.setVisible(true);
  32.     }
  33.     //Function to display the status of toggle button
  34.     public void itemStateChanged(ItemEvent e)
  35.     {
  36. 	if(e.getStateChange()==ItemEvent.SELECTED)
  37. 	    text.setText("Status of Toggle Button : SELECTED");
  38. 	else
  39. 	    text.setText("Status of Toggle Button : DESELECTED");	
  40.     }
  41. }
Program Explanation

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.

Runtime Test Cases

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]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.