event

CheckBox Listener example

In this tutorial we are going to see how you can use an ActionListener, a ChangeListener and an ItemListener with the JCheckBox component to construct a CheckBox listener. This is very useful because it gives a generic way to construct you own custom listeners based on the build in ones, in your own components.

Basically all you have to do to create a CheckBox listener is:

  • Create a new ActionListener instance.
  • Override the actionPerformed method in order to customize the handling of a specific event. Using this you can monitor the ticking actions of the checkbox.
  • Create a new ChangeListener instance.
  • Override the stateChanged method. Using this you can monitor the state of the checkboxes e.g if its pressed, selected, armed etc.
  • Create a new ItemListener instance.
  • Override the itemStateChanged method in order to customize the handling of state changes in the checkboxes.
  • Create a new JCheckBox component and use addActionListener, addChangeListener, addItemListener to add the above event listeners to your checkbox component.

Let’s take a look at the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {

    private static String label1 = "Unckecked";
    private static String label2 = "Checked";

    public static void main(String args[]) {

  JFrame jFrame = new JFrame("Selecting CheckBox");

  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JCheckBox tickBox = new JCheckBox(label1);

  ActionListener actionListener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent actionEvent) {

    AbstractButton absB = (AbstractButton) actionEvent.getSource();

    boolean slct = absB.getModel().isSelected();

    String nL = (slct ? label2 : label1);

    absB.setText(nL);

}

  };

  ChangeListener chListener = new ChangeListener() {

@Override

public void stateChanged(ChangeEvent changeEvent) {

    AbstractButton absB = (AbstractButton) changeEvent.getSource();

    ButtonModel bMod = absB.getModel();

    boolean armed = bMod.isArmed();

    boolean pressed = bMod.isPressed();

    boolean selected = bMod.isSelected();

    System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);

}

  };

  ItemListener itemlistener = new ItemListener() {

@Override

public void itemStateChanged(ItemEvent itemE) {

    AbstractButton absB = (AbstractButton) itemE.getSource();

    Color fgrnd = absB.getForeground();

    Color bgrnd = absB.getBackground();

    int st = itemE.getStateChange();

    if (st == ItemEvent.SELECTED) {

  absB.setForeground(bgrnd);

  absB.setBackground(fgrnd);

    }

}

  };

  tickBox.addActionListener(actionListener);

  tickBox.addChangeListener(chListener);

  tickBox.addItemListener(itemlistener);

  tickBox.setMnemonic(KeyEvent.VK_S);

  Container contentPane = jFrame.getContentPane();

  contentPane.add(tickBox, BorderLayout.NORTH);

  jFrame.setSize(200, 80);

  jFrame.setVisible(true);
    }
}

 
This was an example on how to create a CheckBox Listener in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button