JToggleButton Component In Java | Free Source Code

JToggleButton Component in Java

jtoggle

In this tutorial, we will be going to make a program that has the JToggleButton Component using Java. The JToggleButton is a button that implements of a two-state button; an on or off button.

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:

import java.awt.event.*; // used to access the ActionEvent and ActionListener class
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.

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

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

toggleBtn.addActionListener(new ActionListener() {
 
   public void actionPerformed(ActionEvent e) {
      JToggleButton tBtn = (JToggleButton)e.getSource();
      if (tBtn.isSelected()) {
          JOptionPane.showMessageDialog(null, "Button is on!");
      } else {
          JOptionPane.showMessageDialog(null, "Button is off!");
      }
   }
});

As we can see in 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. To determine if JToggleButtonit’s on or off, we will use isSelected method. When we click first the toggle button, it will prompt the user that the button is on using the JOptionPane. When clicking the button again, 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. Finally, set the size, visibility, and the close operation of the frame. Have this code below:

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


Output:

jtoggle button in java


Here’s the full code of this tutorial:

import java.awt.event.*; // used to access the ActionEvent and ActionListener class
import javax.swing.*; // used to access the JFrame, JOptionPane, JToggleButton
 
 
public class jToggleButtonComponent {
   public static void main(String[] args) {
   	JFrame frame = new JFrame("JToggle Component");
      JToggleButton toggleBtn = new JToggleButton("Toggle Me!");
 
      toggleBtn.addActionListener(new ActionListener() {
 
         public void actionPerformed(ActionEvent e) {
            JToggleButton tBtn = (JToggleButton)e.getSource();
            if (tBtn.isSelected()) {
                JOptionPane.showMessageDialog(null, "Button is on!");
            } else {
                JOptionPane.showMessageDialog(null, "Button is off!");
            }
         }
      });
 
    frame.getContentPane().add(toggleBtn, "North");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 125);
    frame.setVisible(true);
 
 
   }
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.