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

Difference between Array and ArrayList

  • Resizable
    • Array is static in size that is fixed length data structure, One can not change the length after creating the Array object.
    • ArrayList is dynamic in size. Each ArrayList object has instance variable capacity which indicates the size of the ArrayList. Its capacity grows automatically.
  • Primitives
    • Array can contain both primitive data types (e.g. int, float, double) as well as objects.
    • ArrayList can not contains primitive data types it can only contains objects.
  • Adding elements
    • In array we insert elements using the assignment(=) operator.
    • We can insert elements into the ArrayList using the add() method
  • Length
    • Each array object has the length variable which returns the length of the array.
    • Length of the ArrayList is provided by the size() method.

// Array
int[] integerArray = new int[3];
integerArray[0] = 1;
integerArray[1] = 2;
integerArray[2] = 3;
for (int i : integerArray) System.out.println(i);
for (int j=0; j<integerArray.length; j++) System.out.println(integerArray[ j ]);
int k = 0;
while (k < integerArray.length) System.out.println(integerArray[k++]);

// ArrayList
ArrayList integerList = new ArrayList();
integerList.add(1); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object
integerList.add(2); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object
integerList.add(3); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object
for (int m : integerList) System.out.println(m);
for (int n=0; n<integerList.size(); n++) System.out.println(integerList.get(n));
Iterator itr = integerList.iterator();
while (itr.hasNext()) System.out.println(itr.next());

Array vs ArrayList

ArrayArrayList

int[] integerArray = new int[3];

integerArray[0] = 1;

integerArray[1] = 2;

integerArray[2] = 3;

for (int i : integerArray) System.out.println(i);

for (int j=0; j<integerArray.length; j++) System.out.println(integerArray[ j ]);

int k = 0;

while (k < integerArray.length) System.out.println(integerArray[k++]);

 

ArrayList<Integer> integerList = new ArrayList<Integer>();

integerList.add(1); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

integerList.add(2); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

integerList.add(3); //cannot store primitive in ArrayList, instead autoboxing will convert int to Integer object

for (int m : integerList) System.out.println(m);

for (int n=0; n<integerList.size(); n++) System.out.println(integerList.get(n));

Iterator<Integer> itr = integerList.iterator();

while (itr.hasNext()) System.out.println(itr.next());

== vs equal vs hashCode vs contains

CollectionEqualsHashcodeContainsTest (updated)


class CollectionEqualsHashcodeContainsTest{
    // getReference 
    public static String getReference(Object o){
        return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
    }

    public static void main(String[] args)  {

	// primitive type (== 연산자는 값이 같으면 true)
	int i = 1000;
	int j = 1000;
	int k = i;
	System.out.println("i == j " + (i == j));
	System.out.println("i == k " + (i == k));
	System.out.println();

	// reference type (reference가 같으면 true)
	// WARNING: JVM tries to save memory, when the Integer falls in a range (from -128 to 127). Integer v1 = 100, v2 = 100 v1 == v2
	Object o1 = i; // boxing
	Object o2 = j; // boxing
	Object o3 = o1;
	System.out.println(getReference(o1) + " o1=" + o1);
	System.out.println(getReference(o2) + " o2=" + o2);
	System.out.println(getReference(o3) + " o3=" + o3);
	System.out.println("o1 == o2 " + (o1 == o2));
	System.out.println("o1 == o3 " + (o1 == o3));
	System.out.println("o1 equals o2 " + o1.equals(o2));
	System.out.println("o1 equals o3 " + o1.equals(o3));
	System.out.println("o1.hashCode() == o2.hashCode() " + (o1.hashCode() == o2.hashCode()));
	System.out.println("o1.hashCode() == o3.hashCode() " + (o1.hashCode() == o3.hashCode()));
	System.out.println();

	// reference type (== 연산자는 reference가 같으면 true)
	Person p1 = new Person("P",10);
	Person p2 = new Person("P",10);
	Person p3 = p1;
	System.out.println(getReference(p1) + " p1=" + p1);
	System.out.println(getReference(p2) + " p2=" + p2);
	System.out.println(getReference(p3) + " p3=" + p3);
	System.out.println("p1 == p2 " + (p1 == p2));
	System.out.println("p1 == p3 " + (p1 == p3));
	System.out.println("p1 equals p2 " + p1.equals(p2));
	System.out.println("p1 equals p3 " + p1.equals(p3));
	System.out.println("p1.hashCode() == p2.hashCode() " + (p1.hashCode() == p2.hashCode()));
	System.out.println("p1.hashCode() == p3.hashCode() " + (p1.hashCode() == p3.hashCode()));
	System.out.println();

	// String type (== 연산자는 reference가 같으면 true)
	String s1 = "PP"; // String literal을 사용할 경우, pool에서 관리
	String s2 = "PP"; // String literal을 사용할 경우, pool에서 관리
	String s3 = s1;
	String s4 = "P" + "P"; // String literal을 사용할 경우, pool에서 관리
	String s5 = new String("PP");
	String s6 = "PP" + ""; // String literal을 사용할 경우, pool에서 관리
	String s7 = s1 + ""; // String + String literal이라서 새로 생성
	System.out.println(getReference(s1) + " s1=" + s1);
	System.out.println(getReference(s2) + " s2=" + s2);
	System.out.println(getReference(s3) + " s3=" + s3);
	System.out.println(getReference(s4) + " s4=" + s4);
	System.out.println(getReference(s5) + " s5=" + s5);
	System.out.println(getReference(s6) + " s6=" + s6);
	System.out.println(getReference(s7) + " s7=" + s7);

	System.out.println("s1 == s2 " + (s1 == s2));
	System.out.println("s1 == s3 " + (s1 == s3));
	System.out.println("s1 == s4 " + (s1 == s4));
	System.out.println("s1 == s5 " + (s1 == s5));
	System.out.println("s1 == s6 " + (s1 == s6));
	System.out.println("s1 == s7 " + (s1 == s7));
	System.out.println("s1 equals s2 " + s1.equals(s2));
	System.out.println("s1 equals s3 " + s1.equals(s3));
	System.out.println("s1 equals s4 " + s1.equals(s4));
	System.out.println("s1 equals s5 " + s1.equals(s5));
	System.out.println("s1 equals s6 " + s1.equals(s6));
	System.out.println("s1 equals s7 " + s1.equals(s7));
	System.out.println("s1.hashCode() == s2.hashCode() " + (s1.hashCode() == s2.hashCode()));
	System.out.println("s1.hashCode() == s3.hashCode() " + (s1.hashCode() == s3.hashCode()));
	System.out.println("s1.hashCode() == s4.hashCode() " + (s1.hashCode() == s4.hashCode()));
	System.out.println("s1.hashCode() == s5.hashCode() " + (s1.hashCode() == s5.hashCode()));
	System.out.println("s1.hashCode() == s6.hashCode() " + (s1.hashCode() == s6.hashCode()));
	System.out.println("s1.hashCode() == s7.hashCode() " + (s1.hashCode() == s7.hashCode()));
	System.out.println();

	// ArrayList
	System.out.println("pList");
	List<Person> pList = new ArrayList<Person>();
	pList.add(p1);
	pList.add(p2);
	pList.add(p3);
	pList.forEach((p) -> System.out.println(p));
	System.out.println("pList contains p1: " + pList.contains(p1));
	System.out.println("pList contains p2: " + pList.contains(p2));
	System.out.println("pList contains p3: " + pList.contains(p3));
	System.out.println("pList contains new Person: " + pList.contains(new Person("P",10)));
	System.out.println();

	// HashSet의 경우 hashCode가 일치하면 동일한 것으로 간주하여 replace함
	System.out.println("pSet");
	Set<Person> pSet = new HashSet();
	pSet.add(p1);
	pSet.add(p2);
	pSet.add(p3);
	pSet.forEach((p) -> System.out.println(p));
	System.out.println("pSet contains p1: " + pSet.contains(p1));
	System.out.println("pSet contains p2: " + pSet.contains(p2));
	System.out.println("pSet contains p3: " + pSet.contains(p3));
	System.out.println("pSet contains new Person: " + pSet.contains(new Person("P",10)));
	System.out.println();

	// HashMap의 경우 key 값은 hashCode가 일치하면 동일한 것으로 간주하여 replace함
	System.out.println("pMap");
	Map<Person, Integer> pMap = new HashMap<Person, Integer>();
	pMap.put(p1, 1);
	pMap.put(p2, 2);
	pMap.put(p3, 3);
	pMap.forEach((p, e) -> System.out.println(p + " " + e));
	System.out.println("pMap contains p1: " + pMap.containsKey(p1));
	System.out.println("pMap contains p2: " + pMap.containsKey(p2));
	System.out.println("pMap contains p3: " + pMap.containsKey(p3));
	System.out.println("pMap contains new Person: " + pMap.containsKey(new Person("P",10)));
	System.out.println();

	// Array
	System.out.println("pArray");
	Person[] pArray = new Person[3];
	pArray[0] = p1;
	pArray[1] = p2;
	pArray[2] = p3;
	Arrays.asList(pArray).forEach(p -> System.out.println(p));
	System.out.println("pArray contains p1: " + Arrays.asList(pArray).contains(p1));
	System.out.println("pArray contains p2: " + Arrays.asList(pArray).contains(p2));
	System.out.println("pArray contains p3: " + Arrays.asList(pArray).contains(p3));
	System.out.println("pArray contains new Person: " + Arrays.asList(pArray).contains(new Person("P",10)));
	System.out.println();
    }
}