1. Chào mừng Khách đã đến với diễn đàn java, nơi hội tụ tất cả những kỹ năng trong lập trình, chia sẻ những kinh nghiệm, bài viết, quý báu. Các bạn chỉ post bài về lập trình trong forum này. Xin Cảm Ơn!
    Dismiss Notice
  2. Trung Tâm Java - Chuyên sâu lĩnh vực đào tạo lập trình viên Java kinh nghiệm thực tế trong công việc. tìm hiểu thêm tại http://trungtamjava.com/
    Dismiss Notice

JAVA in Depth: (SWING) Events

Thảo luận trong 'Java Core' bắt đầu bởi Joe, 23/6/17.

  1. Joe

    Joe New Member

    Hi

    SWING developers usually don't care about the events that are bound to some SWING components (e.g. JTextField, JTextArea, JComboBox, etc.). But have they ever thought about the events ? What are the events ? How the events are triggered ? etc.

    The first question is as clear as the sun's shining. Event's a happening that occurs unexpectedly and arbitrarily. The second question leads to the surmise that event is triggered by a ghost
    [​IMG]
    that lurks somewhere and just appears when it's disturbed...A ghost ? Yes for the religious it's a ghost, but for JAVA developers it's just a THREAD. A SWING event thread to be exact.

    When an event is a thread it runs fully independent from the main thread (app) and sometimes event needs to be tamed or controlled. Otherwise your app might run amok and goes on the rampage. The following example with a JComboBox that needs to be set to the default item at position 0...and terror happens because the "ghost" is insulted.
    PHP:
    public class JavaBadEvent extends JFrame implements ItemListener {
       
    2     public static void main(String... athrows Exception {
    3        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    4        JavaBadEvent je = new JavaBadEvent();
    5    }
    6    private JTextField jtf;
    7    private JComboBox<Stringjcb;
    8    private JLabel jlab;
    9    public JavaBadEvent() {
    10        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    11        jlab = new JLabel();
    12        jcb = new JComboBox<String>("Item_1!Item_2!Item_3!Item_4".split("!"));
    13        jcb.addItemListener(this);

    14        add("North",(new JPanel()).add(jcb));
    15        add("Center",(new JPanel()).add(jlab));
    16        setSize(200100);
    17        setLocation(150150);
    18        setResizable(false);
    19        setVisible(true);
    20   }
    21   public void itemStateChanged(ItemEvent e) {
    22        jlab.setText("Your selected Item:"+(String)jcb.getSelectedItem());
    23        jcb.setSelectedIndex(0);
    24   }
    }
    the terror...

    [​IMG]

    • line 1: the class is an implementation of ItemListener, also an Event-Thread that listens to the CHANGE of Items.
    • line 13: attach the ItemListener thread to JComboBox jcb
    • line 21-24: the implementation of the required method. In this implementation the ItemListener thread is still active...and if any change is make (here: setSelectedItem) it will trigger another action, then another action...an endless loop arises till all resources are consumed: Terror occurs.
    So, how can I set a JComboBox after a Selection of an item back to a selected default without making the ItemListener thread going amok ? Well, first of all: Thread programming requires an understanding of Synchronization, then the management of threads. Let's take a brief look at the JComboBox< E > API and what we can see is that the method addItemListener(ItemListener aListener) has an opposite method removeItemListener(ItemListener aListener).

    Let's modify the codes using the method removeItemListener (see line 22a and 23a)
    PHP:
    public class JavaBadEvent extends JFrame implements ItemListener {
       
    2     public static void main(String... athrows Exception {
    3        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    4        JavaBadEvent je = new JavaBadEvent();
    5    }
    6    private JTextField jtf;
    7    private JComboBox<Stringjcb;
    8    private JLabel jlab;
    9    public JavaBadEvent() {
    10        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    11        jlab = new JLabel();
    12        jcb = new JComboBox<String>("Item_1!Item_2!Item_3!Item_4".split("!"));
    13        jcb.addItemListener(this);

    14        add("North",(new JPanel()).add(jcb));
    15        add("Center",(new JPanel()).add(jlab));
    16        setSize(200100);
    17        setLocation(150150);
    18        setResizable(false);
    19        setVisible(true);
    20   }
    21   public void itemStateChanged(ItemEvent e) {
    22a       jcb.removeItemListener(this);
    22        jlab.setText("Your selected Item:"+(String)jcb.getSelectedItem());
    23        jcb.setSelectedIndex(0);
    23a       jcb.addItemListener(this);
    24   }
    }
    and see what happens...

    [​IMG]

    NO more exceptions, it works !
     
  2. dinhduccuong1989

    dinhduccuong1989 Moderator Thành viên BQT

    Thanks Joe. Next time, I will do video tutorials about swing
     

Chia sẻ trang này