Sunday, September 4, 2011

Java Swing Events

Events are an important part in any GUI program. All GUI applications are event-driven. An application reacts to different event types which are generated during its life. Events are generated mainly by the user of an application. But they can be generated by other means as well. e.g. internet connection, window manager, timer. In the event model, there are three participants:
  • event source
  • event object
  • event listener
The Event source is the object whose state changes. It generates Events. The Event object (Event) encapsulates the state changes in the event source. The Event listener is the object that wants to be notified. Event source object delegates the task of handling an event to the event listener. 

An event object

When something happens in the application, an event object is created. For example, when we click on the button or select an item from a list. There are several types of events. An ActionEvent, TextEvent, FocusEvent, ComponentEvent etc. Each of them is created under specific conditions.
Event object has information about an event, that has happened. In the next example, we will analyze an ActionEvent in more detail.

package zetcode;

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

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Example extends JFrame implements ActionListener {

    private JList list;
    private DefaultListModel model;

    public Example() {
        initUI();
    }

    public final void initUI() {

        JPanel panel = new JPanel();
        panel.setLayout(null);

        model = new DefaultListModel();
        list = new JList(model);
        list.setBounds(150, 30, 220, 150);

        JButton okButton = new JButton("Ok");
        okButton.setBounds(30, 35, 80, 25);

        okButton.addActionListener(this);

        panel.add(okButton);
        panel.add(list);
        add(panel);

        setTitle("Event object");
        setSize(420, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {

        Locale locale = Locale.getDefault();
        Date date = new Date(e.getWhen());
        String s = DateFormat.getTimeInstance(DateFormat.SHORT,
                locale).format(date);

        if (!model.isEmpty()) {
            model.clear();
        }

        if (e.getID() == ActionEvent.ACTION_PERFORMED) {
            model.addElement(" Event Id: ACTION_PERFORMED");
        }

        model.addElement(" Time: " + s);

        String source = e.getSource().getClass().getName();
        model.addElement(" Source: " + source);

        int mod = e.getModifiers();

        StringBuffer buffer = new StringBuffer(" Modifiers: ");

        if ((mod & ActionEvent.ALT_MASK) > 0) {
            buffer.append("Alt ");
        }

        if ((mod & ActionEvent.SHIFT_MASK) > 0) {
            buffer.append("Shift ");
        }

        if ((mod & ActionEvent.META_MASK) > 0) {
            buffer.append("Meta ");
        }

        if ((mod & ActionEvent.CTRL_MASK) > 0) {
            buffer.append("Ctrl ");
        }

        model.addElement(buffer);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Example ex = new Example();
                ex.setVisible(true);
            }
        });
    }
}
 
Event Object 


Source : http://zetcode.com

No comments:

Post a Comment