|
|
Строка 1: |
Строка 1: |
- | 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 it's 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 <b>Event source</b> is the object whose state changes. It generates Events.
| |
- | The <b>Event object</b> (Event) encapsulates the state changes in the event source.
| |
- | The <b>Event listener</b> is the object that wants to be notified. Event source object delegates the task of handling an event to the event listener.
| |
- |
| |
- |
| |
- | Event handling in Java Swing toolkit is very powerful and flexible. Java uses Event Delegation Model.
| |
- | We specify the objects that are to be notified when a specific event occurs.
| |
- |
| |
- |
| |
- | == 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 list. There are several types of events. An <b>ComponentEvent</b> 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
| |
- | <b>ActionEvent</b> in more detail.
| |
- |
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- |
| |
- | import java.text.DateFormat;
| |
- |
| |
- | import java.util.Calendar;
| |
- | 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;
| |
- |
| |
- |
| |
- | public class EventObject extends JFrame {
| |
- |
| |
- |
| |
- | private JList list;
| |
- | private DefaultListModel model;
| |
- |
| |
- | public EventObject() {
| |
- |
| |
- | setTitle("Event Object");
| |
- |
| |
- | JPanel panel = new JPanel();
| |
- | panel.setLayout(null);
| |
- |
| |
- | model = new DefaultListModel();
| |
- | list = new JList(model);
| |
- | list.setBounds(150, 30, 220, 150);
| |
- |
| |
- | JButton ok = new JButton("Ok");
| |
- | ok.setBounds(30, 35, 80, 25);
| |
- |
| |
- | ok.addActionListener(new ActionListener() {
| |
- | public void actionPerformed(ActionEvent event) {
| |
- |
| |
- | Calendar cal = Calendar.getInstance();
| |
- | cal.setTimeInMillis(event.getWhen());
| |
- | Locale locale = Locale.getDefault();
| |
- | Date date = new Date();
| |
- | String s = DateFormat.getTimeInstance(DateFormat.SHORT,
| |
- | locale).format(date);
| |
- |
| |
- | if ( !model.isEmpty() )
| |
- | model.clear();
| |
- |
| |
- | if (event.getID() == ActionEvent.ACTION_PERFORMED)
| |
- | model.addElement(" Event Id: ACTION_PERFORMED");
| |
- |
| |
- | model.addElement(" Time: " + s);
| |
- |
| |
- | String source = event.getSource().getClass().getName();
| |
- | model.addElement(" Source: " + source);
| |
- |
| |
- | int mod = event.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);
| |
- | }
| |
- | });
| |
- |
| |
- | panel.add(ok);
| |
- | panel.add(list);
| |
- | add(panel);
| |
- |
| |
- | setSize(420, 250);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new EventObject();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | The code example shows a button and a list. If we click on the button, information about the event is displayed in the list.
| |
- | In our case, we are talking about an <b>ActionEvent</b> class.
| |
- | The data will be the time, when the event occured, the id of the event, the event source and the modifier keys.
| |
- |
| |
- | <source lang="java">
| |
- | public void actionPerformed(ActionEvent event) {
| |
- | </source>
| |
- |
| |
- | Inside the action listener, we have an event parameter. It is the instance of the event, that has occured. In our case it is
| |
- | an <b>ActionEvent</b>.
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | cal.setTimeInMillis(event.getWhen());
| |
- | </source>
| |
- |
| |
- | Here we get the time, when the event occured. The method returns time value in milliseconds. So we must format it appropriately.
| |
- |
| |
- | <source lang="java">
| |
- | String source = event.getSource().getClass().getName();
| |
- | model.addElement(" Source: " + source);
| |
- | </source>
| |
- |
| |
- |
| |
- | Here we add the name of the source of the event to the list. In our case the source is a <b>JButton</b>.
| |
- |
| |
- | <source lang="java">
| |
- | int mod = event.getModifiers();
| |
- | </source>
| |
- |
| |
- | We get the modifier keys. It is a bitwise-or of the modifier constants.
| |
- |
| |
- | <source lang="java">
| |
- | if ((mod & ActionEvent.SHIFT_MASK) > 0)
| |
- | buffer.append("Shift ");
| |
- |
| |
- | </source>
| |
- |
| |
- | Here we determine, whether we have pressed a Shift key.
| |
- |
| |
- |
| |
- | [[image: java_swing_eventobject.png | center]]
| |
- |
| |
- | == Implementation ==
| |
- | There are several ways, how we can implement event handling in Java Swing toolkit.
| |
- | * Anonymous inner class
| |
- | * Inner class
| |
- | * Derived class
| |
- |
| |
- | === Anonymous inner class ===
| |
- |
| |
- |
| |
- | We will illustrate these concepts on a simple event example.
| |
- |
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- |
| |
- | import javax.swing.JButton;
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JPanel;
| |
- |
| |
- |
| |
- | public class SimpleEvent extends JFrame {
| |
- |
| |
- |
| |
- | public SimpleEvent() {
| |
- |
| |
- | setTitle("Simle Event");
| |
- |
| |
- | JPanel panel = new JPanel();
| |
- | panel.setLayout(null);
| |
- |
| |
- | JButton close = new JButton("Close");
| |
- | close.setBounds(40, 50, 80, 25);
| |
- |
| |
- | close.addActionListener(new ActionListener() {
| |
- | public void actionPerformed(ActionEvent event) {
| |
- | System.exit(0);
| |
- | }
| |
- |
| |
- | });
| |
- |
| |
- | panel.add(close);
| |
- | add(panel);
| |
- |
| |
- | setSize(300, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new SimpleEvent();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | In this example, we have a button that closes the window upon clicking.
| |
- |
| |
- | <source lang="java">
| |
- | JButton close = new JButton("Close");
| |
- | </source>
| |
- |
| |
- | The button is the <b>event source</b>. It will generate events.
| |
- |
| |
- | <source lang="java">
| |
- | close.addActionListener(new ActionListener() {
| |
- | public void actionPerformed(ActionEvent event) {
| |
- | System.exit(0);
| |
- | }
| |
- | });
| |
- | </source>
| |
- |
| |
- | Here we <b>register</b> an action listener with the button. This way, the events are sent to the <b>event target</b>.
| |
- | The event target in our case is <b>ActionListener</b> class. In this code, we use an <b>anonymous inner class</b>.
| |
- |
| |
- |
| |
- |
| |
- | === Inner class ===
| |
- |
| |
- |
| |
- | Here we implement the example using an inner <b>ActionListener</b> class.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- |
| |
- | import javax.swing.JButton;
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JPanel;
| |
- |
| |
- |
| |
- | public class InnerClass extends JFrame {
| |
- |
| |
- | public InnerClass() {
| |
- |
| |
- | setTitle("Using inner class");
| |
- |
| |
- | JPanel panel = new JPanel();
| |
- | panel.setLayout(null);
| |
- |
| |
- | JButton close = new JButton("Close");
| |
- | close.setBounds(40, 50, 80, 25);
| |
- |
| |
- | ButtonListener listener = new ButtonListener();
| |
- | close.addActionListener(listener);
| |
- |
| |
- | panel.add(close);
| |
- | add(panel);
| |
- |
| |
- | setSize(300, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | class ButtonListener implements ActionListener {
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | System.exit(0);
| |
- | }
| |
- | }
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new InnerClass();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | ButtonListener listener = new ButtonListener();
| |
- | close.addActionListener(listener);
| |
- | </source>
| |
- |
| |
- | Here we have a non anonymous inner class.
| |
- |
| |
- | <source lang="java">
| |
- | class ButtonListener implements ActionListener {
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | System.exit(0);
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | The button listener is defined here.
| |
- |
| |
- |
| |
- |
| |
- | === A derived class implementing the listener ===
| |
- |
| |
- | The following example will derive a class from a component and implement an action listener inside the
| |
- | class.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- |
| |
- | import javax.swing.JButton;
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JPanel;
| |
- |
| |
- |
| |
- | public class UsingInterface extends JFrame {
| |
- |
| |
- |
| |
- | public UsingInterface() {
| |
- |
| |
- | setTitle("Using inner class");
| |
- |
| |
- | JPanel panel = new JPanel();
| |
- | panel.setLayout(null);
| |
- |
| |
- | MyButton close = new MyButton("Close");
| |
- | close.setBounds(40, 50, 80, 25);
| |
- |
| |
- | panel.add(close);
| |
- | add(panel);
| |
- |
| |
- | setSize(300, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | class MyButton extends JButton implements ActionListener {
| |
- |
| |
- | public MyButton(String text) {
| |
- | super.setText(text);
| |
- | addActionListener(this);
| |
- | }
| |
- |
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | System.exit(0);
| |
- | }
| |
- |
| |
- | }
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new UsingInterface();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | In this example, we create a MyButton class, which will implement the action listener.
| |
- |
| |
- | <source lang="java">
| |
- | MyButton close = new MyButton("Close");
| |
- | </source>
| |
- |
| |
- | Here we create the MyButton custom class.
| |
- |
| |
- | <source lang="java">
| |
- | class MyButton extends JButton implements ActionListener {
| |
- | </source>
| |
- |
| |
- | The MyButton class is extended from the <b>JButton</b> class. It implements the
| |
- | <b>ActionListener</b> interface. This way, the event handling is managed within the MyButton class.
| |
- |
| |
- | <source lang="java">
| |
- | addActionListener(this);
| |
- | </source>
| |
- |
| |
- |
| |
- | Here we add the action listener to the MyButton class.
| |
- |
| |
- |
| |
- |
| |
- | == Multiple sources ==
| |
- |
| |
- | <p>A listener can be plugged into several sources. This will be explained in the next example.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.BorderLayout;
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- |
| |
- | import javax.swing.BorderFactory;
| |
- | import javax.swing.JButton;
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JLabel;
| |
- | import javax.swing.JPanel;
| |
- | import javax.swing.border.EtchedBorder;
| |
- |
| |
- |
| |
- | public class MultipleSources extends JFrame {
| |
- |
| |
- | JLabel statusbar;
| |
- |
| |
- | public MultipleSources() {
| |
- |
| |
- | setTitle("Multiple Sources");
| |
- | JPanel panel = new JPanel();
| |
- | statusbar = new JLabel(" ZetCode");
| |
- |
| |
- | statusbar.setBorder(BorderFactory.createEtchedBorder(
| |
- | EtchedBorder.RAISED));
| |
- |
| |
- | panel.setLayout(null);
| |
- |
| |
- | JButton close = new JButton("Close");
| |
- | close.setBounds(40, 30, 80, 25);
| |
- | close.addActionListener(new ButtonListener());
| |
- |
| |
- | JButton open = new JButton("Open");
| |
- | open.setBounds(40, 80, 80, 25);
| |
- | open.addActionListener(new ButtonListener());
| |
- |
| |
- | JButton find = new JButton("Find");
| |
- | find.setBounds(40, 130, 80, 25);
| |
- | find.addActionListener(new ButtonListener());
| |
- |
| |
- | JButton save = new JButton("Save");
| |
- | save.setBounds(40, 180, 80, 25);
| |
- | save.addActionListener(new ButtonListener());
| |
- |
| |
- | panel.add(close);
| |
- | panel.add(open);
| |
- | panel.add(find);
| |
- | panel.add(save);
| |
- |
| |
- | add(panel);
| |
- | add(statusbar, BorderLayout.SOUTH);
| |
- |
| |
- | setSize(400, 300);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | class ButtonListener implements ActionListener {
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | JButton o = (JButton) e.getSource();
| |
- | String label = o.getText();
| |
- | statusbar.setText(" " + label + " button clicked");
| |
- | }
| |
- | }
| |
- |
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new MultipleSources();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | We create four buttons and a statusbar. The statusbar will display an informative message upon clicking on the
| |
- | button.
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | close.addActionListener(new ButtonListener());
| |
- | ...
| |
- | open.addActionListener(new ButtonListener());
| |
- | ...
| |
- | </source>
| |
- |
| |
- | Each button will be registered against a <b>ButtonListener</b> class.
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | JButton o = (JButton) e.getSource();
| |
- | String label = o.getText();
| |
- | </source>
| |
- |
| |
- | Here we determine, which button was pressed.
| |
- |
| |
- | <source lang="java">
| |
- | statusbar.setText(" " + label + " button clicked")
| |
- | </source>
| |
- |
| |
- |
| |
- | We update the statusbar.
| |
- |
| |
- |
| |
- | <br>
| |
- | [[image: java_swing_multiplesources.png | center]]
| |
- |
| |
- |
| |
- |
| |
- | == Multiple listeners ==
| |
- | We can register several listeners for one event.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.BorderLayout;
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- |
| |
- | import java.util.Calendar;
| |
- |
| |
- | import javax.swing.BorderFactory;
| |
- | import javax.swing.JButton;
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JLabel;
| |
- | import javax.swing.JPanel;
| |
- | import javax.swing.JSpinner;
| |
- | import javax.swing.SpinnerModel;
| |
- | import javax.swing.SpinnerNumberModel;
| |
- | import javax.swing.border.EtchedBorder;
| |
- |
| |
- |
| |
- | public class MultipleListeners extends JFrame {
| |
- |
| |
- | private JLabel statusbar;
| |
- | private JSpinner spinner;
| |
- | private static int count = 0;
| |
- |
| |
- | public MultipleListeners() {
| |
- |
| |
- | setTitle("Multiple Listeners");
| |
- | JPanel panel = new JPanel();
| |
- | statusbar = new JLabel("0");
| |
- |
| |
- | statusbar.setBorder(BorderFactory.createEtchedBorder(
| |
- | EtchedBorder.RAISED));
| |
- |
| |
- | panel.setLayout(null);
| |
- |
| |
- | JButton add = new JButton("+");
| |
- | add.setBounds(40, 30, 80, 25);
| |
- | add.addActionListener(new ButtonListener1());
| |
- | add.addActionListener(new ButtonListener2());
| |
- |
| |
- | Calendar calendar = Calendar.getInstance();
| |
- | int currentYear = calendar.get(Calendar.YEAR);
| |
- |
| |
- | SpinnerModel yearModel = new SpinnerNumberModel(currentYear,
| |
- | currentYear - 100,
| |
- | currentYear + 100,
| |
- | 1);
| |
- |
| |
- | spinner = new JSpinner(yearModel);
| |
- | spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));
| |
- |
| |
- | spinner.setBounds(190, 30, 80, 25);
| |
- |
| |
- | panel.add(add);
| |
- | panel.add(spinner);
| |
- |
| |
- | add(panel);
| |
- | add(statusbar, BorderLayout.SOUTH);
| |
- |
| |
- | setSize(300, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | class ButtonListener1 implements ActionListener {
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | Integer val = (Integer) spinner.getValue();
| |
- | spinner.setValue(++val);
| |
- | }
| |
- | }
| |
- |
| |
- | class ButtonListener2 implements ActionListener {
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | statusbar.setText(Integer.toString(++count));
| |
- | }
| |
- | }
| |
- |
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new MultipleListeners();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- |
| |
- | In this example, we have a button, spinner and a statusbar. We use two button listeners for one event. One click of a button will add one year to the spinner component and update the statusbar. The statusbar will show, how many times we have clicked on the button.
| |
- |
| |
- | <source lang="java">
| |
- | add.addActionListener(new ButtonListener1());
| |
- | add.addActionListener(new ButtonListener2());
| |
- | </source>
| |
- |
| |
- | We register two button listeners.
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | SpinnerModel yearModel = new SpinnerNumberModel(currentYear,
| |
- | currentYear - 100,
| |
- | currentYear + 100,
| |
- | 1);
| |
- | spinner = new JSpinner(yearModel);
| |
- | </source>
| |
- |
| |
- | Here we create the spinner component. We use a year model for the spinner. The <b>SpinnerNumberModel</b>
| |
- | arguments are initial value, min, max values and the step.
| |
- |
| |
- | <source lang="java">
| |
- | spinner.setEditor(new JSpinner.NumberEditor(spinner, "#"));
| |
- |
| |
- | </source>
| |
- |
| |
- | We remove the thousands separator.
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | Integer val = (Integer) spinner.getValue();
| |
- | spinner.setValue(++val);
| |
- | </source>
| |
- |
| |
- | Here we increase the year number.
| |
- |
| |
- | <br>
| |
- |
| |
- | [[image: java_swing_multiplelisteners.png | center]]
| |
- |
| |
- |
| |
- |
| |
- |
| |
- | == Removing listeners ==
| |
- | The Java Swing toolkit enables us to remove the registered listeners.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.event.ActionEvent;
| |
- | import java.awt.event.ActionListener;
| |
- | import java.awt.event.ItemEvent;
| |
- | import java.awt.event.ItemListener;
| |
- |
| |
- | import javax.swing.JButton;
| |
- | import javax.swing.JCheckBox;
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JLabel;
| |
- | import javax.swing.JPanel;
| |
- |
| |
- |
| |
- | public class RemoveListener extends JFrame {
| |
- |
| |
- | private JLabel text;
| |
- | private JButton add;
| |
- | private JCheckBox active;
| |
- | private ButtonListener buttonlistener;
| |
- | private static int count = 0;
| |
- |
| |
- | public RemoveListener() {
| |
- |
| |
- | setTitle("Remove listener");
| |
- | JPanel panel = new JPanel();
| |
- |
| |
- | panel.setLayout(null);
| |
- |
| |
- | add = new JButton("+");
| |
- | add.setBounds(40, 30, 80, 25);
| |
- | buttonlistener = new ButtonListener();
| |
- |
| |
- | active = new JCheckBox("Active listener");
| |
- | active.setBounds(160, 30, 140, 25);
| |
- |
| |
- | active.addItemListener(new ItemListener() {
| |
- | public void itemStateChanged(ItemEvent event) {
| |
- | if (active.isSelected()) {
| |
- | add.addActionListener(buttonlistener);}
| |
- | else {
| |
- | add.removeActionListener(buttonlistener);
| |
- | }
| |
- | }
| |
- | });
| |
- |
| |
- | text = new JLabel("0");
| |
- | text.setBounds(40, 80, 80, 25);
| |
- |
| |
- | panel.add(add);
| |
- | panel.add(active);
| |
- | panel.add(text);
| |
- |
| |
- | add(panel);
| |
- |
| |
- | setSize(310, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | class ButtonListener implements ActionListener {
| |
- | public void actionPerformed(ActionEvent e)
| |
- | {
| |
- | text.setText(Integer.toString(++count));
| |
- | }
| |
- | }
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new RemoveListener();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | We have three components on the panel. A button, check box and a label. By toggling the check box, we add or remove
| |
- | the listener for a button.
| |
- |
| |
- | <source lang="java">
| |
- | buttonlistener = new ButtonListener();
| |
- | </source>
| |
- |
| |
- | We have to create a non anonymous listener, if we want to later remove it. We need a reference to it.
| |
- |
| |
- | <source lang="java">
| |
- | if (active.isSelected()) {
| |
- | add.addActionListener(buttonlistener);}
| |
- | else {
| |
- | add.removeActionListener(buttonlistener);
| |
- | }
| |
- | </source>
| |
- |
| |
- | We determine, whether the check box is selected. Then we add or remove the listener.
| |
- |
| |
- |
| |
- | <br>
| |
- | [[image: java_swing_removelistener.png | center]]
| |
- |
| |
- |
| |
- |
| |
- |
| |
- | == Moving a window ==
| |
- |
| |
- | The following example will look for a position of a window on the screen.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.Font;
| |
- | import java.awt.event.ComponentEvent;
| |
- | import java.awt.event.ComponentListener;
| |
- |
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JLabel;
| |
- | import javax.swing.JPanel;
| |
- |
| |
- |
| |
- | public class MovingWindow extends JFrame implements ComponentListener {
| |
- |
| |
- | private JLabel labelx;
| |
- | private JLabel labely;
| |
- |
| |
- | public MovingWindow() {
| |
- |
| |
- | setTitle("Moving window");
| |
- |
| |
- | JPanel panel = new JPanel();
| |
- | panel.setLayout(null);
| |
- |
| |
- | labelx = new JLabel("x: ");
| |
- | labelx.setFont(new Font("Serif", Font.BOLD, 14));
| |
- | labelx.setBounds(20, 20, 60, 25);
| |
- |
| |
- | labely = new JLabel("y: ");
| |
- | labely.setFont(new Font("Serif", Font.BOLD, 14));
| |
- | labely.setBounds(20, 45, 60, 25);
| |
- |
| |
- | panel.add(labelx);
| |
- | panel.add(labely);
| |
- |
| |
- | add(panel);
| |
- |
| |
- | addComponentListener(this);
| |
- |
| |
- | setSize(310, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- | public void componentResized(ComponentEvent e) {
| |
- | }
| |
- |
| |
- | public void componentMoved(ComponentEvent e) {
| |
- | int x = e.getComponent().getX();
| |
- | int y = e.getComponent().getY();
| |
- | labelx.setText("x: " + x);
| |
- | labely.setText("y: " + y);
| |
- | }
| |
- |
| |
- | public void componentShown(ComponentEvent e) {
| |
- | }
| |
- |
| |
- | public void componentHidden(ComponentEvent e) {
| |
- | }
| |
- |
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new MovingWindow();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | The example shows the current window coordinates on the panel.
| |
- | To get the window position, we use the <b>ComponentListener</b>
| |
- |
| |
- | <source lang="java">
| |
- | labelx.setFont(new Font("Serif", Font.BOLD, 14));
| |
- | </source>
| |
- |
| |
- | We make the font bigger, the default one is a bit small.
| |
- |
| |
- | <source lang="java">
| |
- | int x = e.getComponent().getX();
| |
- | int y = e.getComponent().getY();
| |
- | </source>
| |
- |
| |
- | Here we get the x and the y positions.
| |
- |
| |
- | Notice, that we have to implement all four methods, that are available in the <b>ComponentListener</b>.
| |
- | Even, if we do not use them.
| |
- |
| |
- |
| |
- | <br>
| |
- | [[image: java_swing_movingwindow.png | center]]
| |
- |
| |
- |
| |
- |
| |
- |
| |
- | == Adapters ==
| |
- | Adapters are convenient classes. In the previous code example, we had to implement all four methods of a <b>ComponentListener</b> class. Even if we did not use them. To avoid unnecessary coding, we can use adapters. Adapter is a class that implements all necessary methods. They
| |
- | are empty. We then use only those methods, that we actually need.
| |
- | There is no adapter for a button click event. Because there we have only one method to implement. The <b>actionPerformed()</b> method.
| |
- | We can use adapters in situations, where we have more than one method to implement.
| |
- |
| |
- |
| |
- | The following example is a rewrite of the previous one, using a <b>ComponentAdapter</b>.
| |
- |
| |
- | <source lang ="java">
| |
- | import java.awt.Font;
| |
- | import java.awt.event.ComponentAdapter;
| |
- | import java.awt.event.ComponentEvent;
| |
- |
| |
- | import javax.swing.JFrame;
| |
- | import javax.swing.JLabel;
| |
- | import javax.swing.JPanel;
| |
- |
| |
- |
| |
- | public class Adapter extends JFrame {
| |
- |
| |
- | private JLabel labelx;
| |
- | private JLabel labely;
| |
- |
| |
- | public Adapter() {
| |
- |
| |
- | setTitle("Adapter");
| |
- |
| |
- | JPanel panel = new JPanel();
| |
- | panel.setLayout(null);
| |
- |
| |
- | labelx = new JLabel("x: ");
| |
- | labelx.setFont(new Font("Serif", Font.BOLD, 14));
| |
- | labelx.setBounds(20, 20, 60, 25);
| |
- |
| |
- | labely = new JLabel("y: ");
| |
- | labely.setFont(new Font("Serif", Font.BOLD, 14));
| |
- | labely.setBounds(20, 45, 60, 25);
| |
- |
| |
- |
| |
- | panel.add(labelx);
| |
- | panel.add(labely);
| |
- |
| |
- | add(panel);
| |
- | addComponentListener(new MoveAdapter());
| |
- |
| |
- | setSize(310, 200);
| |
- | setLocationRelativeTo(null);
| |
- | setDefaultCloseOperation(EXIT_ON_CLOSE);
| |
- | setVisible(true);
| |
- | }
| |
- |
| |
- |
| |
- | class MoveAdapter extends ComponentAdapter {
| |
- | public void componentMoved(ComponentEvent e) {
| |
- | int x = e.getComponent().getX();
| |
- | int y = e.getComponent().getY();
| |
- | labelx.setText("x: " + x);
| |
- | labely.setText("y: " + y);
| |
- | }
| |
- | }
| |
- |
| |
- | public static void main(String[] args) {
| |
- | new Adapter();
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- | This example is a rewrite of the previous one. Here we use the <b>ComponentAdapter</b>.
| |
- |
| |
- |
| |
- | <source lang="java">
| |
- | addComponentListener(new MoveAdapter());
| |
- | </source>
| |
- |
| |
- | Here we register the component listener.
| |
- |
| |
- | <source lang="java">
| |
- | class MoveAdapter extends ComponentAdapter {
| |
- | public void componentMoved(ComponentEvent e) {
| |
- | int x = e.getComponent().getX();
| |
- | int y = e.getComponent().getY();
| |
- | labelx.setText("x: " + x);
| |
- | labely.setText("y: " + y);
| |
- | }
| |
- | }
| |
- | </source>
| |
- |
| |
- |
| |
- | Inside the MoveAdapter inner class, we define the <b>componentMoved()</b> method. All the other methods are left empty.
| |