Change Listener in Java

Introduction

In this article we describe change listener in Java. The Netbeans IDE is used for the sample program. In this article we create a color chooser app to define the change listener.

What is change Listener

It is similar to a property change listener. Its works on changing the property of an object. Various Swing components (including JPanel, JTabbedPane, JViewPort) rely on change events for basic functionality, color choosers, slider, and so on. Let's take an example of a color chooser that changes the color of components. The components are like text, image, etcetera.

What the change listener does

  • void stateChanged(ChangeEvent ex)

          Used to change the target of the listener to its state.

The following describes an example to help understand change listeners.

In this example we design a color chooser app. Its purpose is to change the color of text that we display on our frame.

The following procedure is required for developing this app.

Step 1

Open the Netbeans IDE.

fig-1.jpg

Step 2

Now from file menu select "New Project".

fig-2.jpg

Step 3

Now select "Java" -> "Java application" as shown below:

fig-3.jpg

Step 4

Type your project name there, such as "ColorChooserApp" and for the main class type "ChangeListenerExample" as shown below.

Fig-4.jpg

Step 5

Click on "Finish"; your project is generated with some default coding.

Step 6

Now change the code of "ChangeListenerExample" with the following code.

ChangeListenerExample.java

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class ChangeListenerExample extends JPanel implements ChangeListener {
JLabel lbl;
JColorChooser chooseColor;
public ChangeListenerExample() {
super(new BorderLayout());
lbl = new JLabel("Choose Your Favorite Color!", JLabel.CENTER);
lbl.setBackground(Color.red);
lbl.setBackground(Color.green);
lbl.setForeground(Color.blue);
lbl.setOpaque(true);
lbl.setFont(new Font("Segoe UI", Font.BOLD, 24));
lbl.setPreferredSize(new Dimension(100, 65));
JPanel newPanl = new JPanel(new BorderLayout());
newPanl.add(lbl, BorderLayout.CENTER);
newPanl.setBorder(BorderFactory.createTitledBorder("Choose Banner"));
chooseColor = new JColorChooser(lbl.getForeground());
chooseColor.getSelectionModel().addChangeListener(this);
chooseColor.setBorder(BorderFactory.createTitledBorder("Choose Your Text Color"));
add(newPanl, BorderLayout.CENTER);
add(chooseColor, BorderLayout.PAGE_END);
}
public void stateChanged(ChangeEvent ey) {
Color clr = chooseColor.getColor();
lbl.setForeground(clr);
}
private static void createAndShowGUI() {
JFrame frm = new JFrame("ChangeListenerExample");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newCmpnt = new ChangeListenerExample();
newCmpnt.setOpaque(true);
frm.setContentPane(newCmpnt);
frm.pack();
frm.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

fig-6.jpg

fig-7.jpg

Step 7

Now your project is ready to run. Now right-click on the project, next select "Run" as follows:

fig-8.jpg

Step 8

Now click on "Run" and see the output as in the following:

fig-9.jpg

Now change the color. You see that the color of the text will change; the output is as shown below.

fig-10.jpg

Now again change the color.

fig-11.jpg

Now change the color again.

fig-12.jpg

Thanks for reading.