Home » Desktop Java » awt » event » A simple ChangeListener example

About Byron Kiourtzoglou

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.

A simple ChangeListener example

In this example we are going to see how to use the ChangeListener interface in Java. This is very useful when you want to monitor generic changes in your application.

In short, to use a simple ChangeListener one should follow these steps:

  • Create a new ChangeListener instance.
  • Override the stateChanged method to customize the handling of specific events.
  • Use specific functions of components to get better undemanding of the event that occurred.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class StateListener {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame("");


  JButton button = new JButton("Press Me");

  ActionListener actionListner = new ActionListener() {


public void actionPerformed(ActionEvent event) {

    AbstractButton absButton = (AbstractButton) event.getSource();

    boolean selected = absButton.getModel().isSelected();

    System.out.println("Selected=" + selected + "n");



  ChangeListener changeListner = new ChangeListener() {


public void stateChanged(ChangeEvent event) {

    AbstractButton aButton = (AbstractButton) event.getSource();

    ButtonModel aModel = aButton.getModel();

    boolean armed = aModel.isArmed();

    boolean pressed = aModel.isPressed();

    boolean selected = aModel.isSelected();

    System.out.println("Armed :" + armed + " - Pressed :" + pressed + " - Selected :" + selected);





  Container cPane = jFrame.getContentPane();

  cPane.add(button, BorderLayout.CENTER);

  jFrame.setSize(800, 500);


This was an example on how to use ChangeListener in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply


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

Notify of