Skip to main content

JToggleButton Component in Java


This is a tutorial in which we will going to create a program that has the JToggleButton Component using Java. The JToggleButton is a button which implements of a two-state button; an on or off button.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of jToggleButtonComponent.java.

2. Import the following packages:

  1. import java.awt.event.*; // used to access the ActionEvent and ActionListener class
  2. import javax.swing.*; // used to access the JFrame, JOptionPane, JToggleButton

3. Initialize your variable in your Main, variable frame for creating JFrame, and variable toggleBtn for JToggleButton.

  1. JFrame frame = new JFrame("JToggle Component");
  2. JToggleButton toggleBtn = new JToggleButton("Toggle Me!");

4. Now, we will add an ActionListener to the toggle button. This trigger to have an ActionEvent when clicking the toggle button. Have this code below:

  1. toggleBtn.addActionListener(new ActionListener() {
  2.  
  3. public void actionPerformed(ActionEvent e) {
  4. JToggleButton tBtn = (JToggleButton)e.getSource();
  5. if (tBtn.isSelected()) {
  6. JOptionPane.showMessageDialog(null, "Button is on!");
  7. } else {
  8. JOptionPane.showMessageDialog(null, "Button is off!");
  9. }
  10. }
  11. });

As you have seen the code above, we have created again a JToggleButton having a variable named of tBtn. We used the method getSource to get the action out of the JToggleButton. When clicking the JToggleButton to determine if it's on or off, we will use isSelected method. When we click first the toggle button, it will prompt to the user that the button is on using the JOptionPane. When clicking again the button, it will display that the button is off.

5. Now, add the JToggleButton variable to the frame using the default BorderLayout of north position of the getContentPane method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:

  1. frame.getContentPane().add(toggleBtn, "North");
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. frame.setSize(300, 125);
  4. frame.setVisible(true);


Output:

output


Here's the full code of this tutorial:

  1. import java.awt.event.*; // used to access the ActionEvent and ActionListener class
  2. import javax.swing.*; // used to access the JFrame, JOptionPane, JToggleButton
  3.  
  4.  
  5. public class jToggleButtonComponent {
  6. public static void main(String[] args) {
  7. JFrame frame = new JFrame("JToggle Component");
  8. JToggleButton toggleBtn = new JToggleButton("Toggle Me!");
  9.  
  10. toggleBtn.addActionListener(new ActionListener() {
  11.  
  12. public void actionPerformed(ActionEvent e) {
  13. JToggleButton tBtn = (JToggleButton)e.getSource();
  14. if (tBtn.isSelected()) {
  15. JOptionPane.showMessageDialog(null, "Button is on!");
  16. } else {
  17. JOptionPane.showMessageDialog(null, "Button is off!");
  18. }
  19. }
  20. });
  21.  
  22. frame.getContentPane().add(toggleBtn, "North");
  23. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. frame.setSize(300, 125);
  25. frame.setVisible(true);
  26.  
  27.  
  28. }
  29. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.