Implement JToggleButton in Java



In this article, we will learn about the implementation of a JToggleButton in Java. The JToggleButton is a basic Swing component in Java that is used to represent a two-state button, i.e., selected or unselected. It is therefore the best component to use when adding on/off or mode selection functionality to Java applications.

JToggleButton

A JToggleButton is an extension of AbstractButton, and it can be used to represent buttons that can be toggled ON and OFF. When JToggleButton is pressed for the first time, it remains pressed and it can be released only when it is pressed for the second time. A JToggleButton generates an ActionEvent each time it is pressed.

Initialization of an "On" JToggleButton:

JToggleButton jtb = new JToggleButton("ON");

A JToggleButton can also generate an ItemEvent, this event is used by those components that support the concept of selection. When a JToggleButton is pressed in, it is selected. When it is popped out, it is deselected.

Initialization of an "OFF" JToggleButton:

JToggleButton jtb = new JToggleButton("OFF");

To handle item events, you must implement the ItemListener interface. This interface defines the itemStateChanged( ) method that is invoked when the state of an item changes. A toggle button's state is by calling the isSelected( ) method on the button that generated the event.

Primary JToggleButton Customizations

The following are the primary JToggleButton customizations in Java:

Using Icons for Different States

We can provide alternative icons for the selected and unselected states of a JToggleButton to provide better visual feedback.

The following are the key methods for icon management:

setIcon(Icon icon)

  • Sets the default (unselected) icon.
  • If no explicitly set pressed icon is provided, the same icon can also be used as the "pressed" or "disabled" icon.

Method declaration

public void setIcon(Icon defaultIcon)

setSelectedIcon(Icon icon)

  • Sets the icon when the button is selected.

Method declaration

public void setSelectedIcon(Icon selectedIcon)

setPressedIcon(Icon icon)

  • Sets the icon when the button is pressed.

Method declaration

public void setPressedIcon(Icon pressedIcon)

setRolloverIcon(Icon icon)

  • Sets the icon when the mouse hovers over the button.

Method declaration

public void setRolloverIcon(Icon rolloverIcon)

Syntax

The following is the syntax for icon setup in JToggleButton:

JToggleButton toggleButton = new JToggleButton();

// Set icon for the unselected state
toggleButton.setIcon(unselectedIcon);

// Set icon for the selected state
toggleButton.setSelectedIcon(selectedIcon);

// Optionally, set icons for pressed and rollover states
toggleButton.setPressedIcon(pressedIcon);
toggleButton.setRolloverIcon(rolloverIcon);

Creating a Toggle Button Group

If we want only one toggle button to be selected at a time (like radio buttons), you can group them using ButtonGroup.

The following are some key points to remember while creating a toggle button group:

  • Only one button in a ButtonGroup can be selected at a time.
  • The ButtonGroup itself is not a visual component; it only manages selection logic.
  • We still need to manually add the buttons to a container (e.g., JPanel).

Syntax

The following is the syntax for creating a toggle button group:

// Create button group for mutual exclusion
ButtonGroup group = new ButtonGroup();

JToggleButton option1 = new JToggleButton("Option 1");
JToggleButton option2 = new JToggleButton("Option 2");
JToggleButton option3 = new JToggleButton("Option 3");

group.add(option1);
group.add(option2);
group.add(option3);

Customizing Appearance

We can change the look of JToggleButton by modifying its background colors, foreground colors, font, border and mouse hover effects.

The following are the primary ways to customize the appearance:

  • setBackground(Color): For setting the color of the background
  • setForeground(Color): Sets the color of the text
  • setFont(Font): Changes the font
  • setBorder(Border): Sets a border for the user
  • setFocusPainted(boolean): Disables the focus highlight

Syntax

The following is the syntax to customize the appearance of JToggleButton:

JToggleButton toggleButton = new JToggleButton("Custom Button");

// Change colors
toggleButton.setBackground(Color.BLUE);
toggleButton.setForeground(Color.WHITE);

// Change font
toggleButton.setFont(new Font("Arial", Font.BOLD, 16));

// Change border
toggleButton.setBorder(BorderFactory.createLineBorder(Color.RED, 2));

// Disable focus painting (removes dotted border when clicked)
toggleButton.setFocusPainted(false);

Example of JToggleButton

Below is an example for the implementation of a JToggleButton in Java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JToggleButtonTest extends JFrame implements ItemListener {
   private JToggleButton jtb;
   JToggleButtonTest() {
      setTitle("JToggleButton Test");
      setLayout(new FlowLayout());
      setJToggleButton();
      setAction();
      setSize(450, 300);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }
   private void setJToggleButton() {
      jtb = new JToggleButton("ON");
      add(jtb);
   }
   private void setAction() {
      jtb.addItemListener(this);
   }
   public void itemStateChanged(ItemEvent eve) {
      if(jtb.isSelected())
         jtb.setText("OFF");
      else
         jtb.setText("ON");
   }
}
public class MainApp {
   public static void main(String[] args) {
      new JToggleButtonTest();
   }
}

Output

Below is an "ON" JToggleButton snippet:

Below is an "OFF" JToggleButton snippet:

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-18T17:50:47+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements