5 Ways to Implement Event Listener

Slider(JSlider, JLabel)
1. event listener using anonymous class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// SliderFrame1 shows event listener using anonymous class
public class SliderFrame1 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame1(String name) {
        super(name);

        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        // using anonymous class
        this.slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider s = (JSlider)e.getSource();
                label.setText("Value : " + s.getValue());
            }
        });

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame1("SliderTest1");
    }
}

2. event listener using interface implements

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// using implements ChangeListener
public class SliderFrame2 extends JFrame implements ChangeListener {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame2(String name) {
        super(name);

        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        this.slider.addChangeListener(this); // implements ChangeListener

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame2("SliderTest2");
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider s = (JSlider)e.getSource();
        label.setText("Value : " + s.getValue());
    }
}

3. event listener using private inner class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderFrame3 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    // using private inner class
    private class SliderChangeListener implements ChangeListener {
        @Override
        public void stateChanged(ChangeEvent e) {
            JSlider s = (JSlider)e.getSource();
            label.setText("Value : " + s.getValue());
        }
    }

    public SliderFrame3(String name) {
        super(name);
        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        this.slider.addChangeListener(new SliderChangeListener()); // private class

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame3("SliderTest3");
    }
}

4. event listener using outer class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// using private inner class
class SliderChangeListener implements ChangeListener {
    JLabel label = null;
    public SliderChangeListener(JLabel label) {
        this.label = label;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        JSlider s = (JSlider)e.getSource();
        label.setText("Value : " + s.getValue());
    }
}

public class SliderFrame4 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame4(String name) {
        super(name);
        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        this.slider.addChangeListener(new SliderChangeListener(this.label)); // outer class

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame4("SliderTest4");
    }
}

5. event listener using lambda

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// SliderFrame5 shows event listener using lambda
public class SliderFrame5 extends JFrame {

    JPanel panel = new JPanel(new BorderLayout());
    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 20);
    JLabel label = new JLabel("Value : ", JLabel.CENTER);

    public SliderFrame5(String name) {
        super(name);

        this.add(panel);
        this.panel.add(slider, BorderLayout.CENTER);
        this.panel.add(label, BorderLayout.SOUTH);

        // using lambda
        this.slider.addChangeListener(e -> { // lambda
                JSlider s = (JSlider)e.getSource();
                label.setText("Value : " + s.getValue());
        });

        this.setSize(250, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new SliderFrame5("SliderTest5");
    }
}

Java Swing Component

https://www.javatpoint.com/java-swing
https://www.javatpoint.com/java-jbutton
https://www.javatpoint.com/java-jlabel
https://www.javatpoint.com/java-jtextfield
https://www.javatpoint.com/java-jcheckbox
https://www.javatpoint.com/java-jradiobutton
https://www.javatpoint.com/java-jcombobox
https://www.javatpoint.com/java-jtable
https://www.javatpoint.com/java-jslider
https://www.javatpoint.com/java-jdialog
https://www.javatpoint.com/java-jpanel
https://www.javatpoint.com/java-jfilechooser
https://www.javatpoint.com/java-jtogglebutton
https://www.javatpoint.com/java-jframe
https://www.javatpoint.com/java-jscrollpane