リスナー とは、ボタンをクリックした、メニューを実行した、ウィンドウをリサイズした、マウスを動かした、キーを押したなどのイベント監視し、イベント発生時に対応するアクションを実行するオブジェクトです。主なリスナーには次のようなものがあります。
| リスナー | イベント | 主な監視対象 |
|---|---|---|
| ActionListener | actionPerformed | MenuItem, List, TextField, Button, AbstructButton, JTextField, ButtonModel, JComboBox, Timer, DefaultButtonModel, ComboBoxEditor, JFileChooser, BasicComboBoxEditor |
| WindowListener | windowOpened windowClosing windowClosed windowIconified windowDeiconified windowActivated windowDeactivated |
Window, JWindow, Frame, Dialog |
| MouseListener | mouseClicked mousePressed mouseReleased mouseEntered mouseExited |
Component, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent, TextField, TextArea |
| MouseMotionListener | mouseDragged mouseMoved |
Component, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent, TextField, TextArea |
| KeyListener | keyPressed keyReleased keyTyped |
Component, Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent, TextField, TextArea |
| TextListener | textValueChanged | TextComponent, TextField, TextArea |
| ItemListener | itemStateChanged | Checkbox, List, Choice, ItemSelectable, CheckboxMenuItem, AbstractButton, ButtonModel, JComboBox, DefaultButtonModel |
ボタンが押されたなどのイベントを監視して、イベント発生時にアクションを実行するサンプルです。
import java.awt.*;
import java.awt.event.*;
public class ActionListenerTest extends Frame implements ActionListener {
ActionListenerTest() {
super("ActionListenerTest");
Button b1 = new Button("BUTTON1");
b1.addActionListener(this);
add(b1);
setSize(200, 100);
show();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
public static void main(String [] args) {
new ActionListenerTest();
}
}
ボタン(b1)が押されたことの監視は アクションリスナオブジェクト が行います。アクションリスナオブジェクトは、ActionListener インタフェースを実装(implement)したオブジェクトです。リスナーは実装すべきインタフェースが決まっています。ActionListener の場合は actionPerformed() というインタフェースを実装しなくてはなりません。サンプルでは、自分自身(this)がアクションリスナオブジェクトとなっています。イベント発生時に actionPerformed() インタフェースでこれを捕獲し、System.exit(0); でプログラムを終了しています。
下記のように、インナークラスのインスタンスをアクションリスナオブジェクトとすることもできます。
import java.awt.*;
import java.awt.event.*;
public class ActionListenerTest2 extends Frame {
ActionListenerTest2() {
super("ActionListenerTest2");
Button b1 = new Button("BUTTON1");
b1.addActionListener(new MyActionListener());
add(b1);
setSize(200, 100);
show();
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String [] args) {
new ActionListenerTest2();
}
}
ウィンドウが、開かれた、閉じようとしている、閉じた、アイコン化された、アイコン化解除された、アクティブになった、非アクティブになったというイベントを監視します。
import java.awt.*;
import java.awt.event.*;
public class WindowListenerTest extends Frame implements WindowListener {
WindowListenerTest() {
super("WindowListenerTest");
this.addWindowListener(this);
setSize(200, 100);
show();
}
public void windowOpened(WindowEvent e) { // 開かれた
System.out.println("windowOpened");
}
public void windowClosing(WindowEvent e) { // 閉じられている
System.out.println("windowClosing");
}
public void windowClosed(WindowEvent e) { // 閉じた
System.out.println("windowClosed");
}
public void windowIconified(WindowEvent e) { // アイコン化された
System.out.println("windowIconified");
}
public void windowDeiconified(WindowEvent e) { // 非アイコン化された
System.out.println("windowDeiconified");
}
public void windowActivated(WindowEvent e) { // アクティブになった
System.out.println("windowActivated");
}
public void windowDeactivated(WindowEvent e) { // 非アクティブになった
System.out.println("windowDeactivated");
}
public static void main(String [] args) {
new WindowListenerTest();
}
}
7個すべてのインタフェースを実装するのが面倒なときは、WindowAdapter を用いると便利です。
マウスのボタンに関するイベントを監視します。
import java.awt.*;
import java.awt.event.*;
public class MouseListenerTest extends Frame implements MouseListener {
MouseListenerTest() {
super("MouseListenerTest");
this.addMouseListener(this);
setSize(200, 100);
show();
}
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked");
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited");
}
public static void main(String [] args) {
new MouseListenerTest();
}
}
マウスの動きに関するイベントを監視します。
import java.awt.*;
import java.awt.event.*;
public class MouseMotionListenerTest extends Frame implements MouseMotionListener {
MouseMotionListenerTest () {
super("MouseMotionListenerTest");
addMouseMotionListener(this);
setSize(200, 100);
show();
}
public void mouseDragged(MouseEvent e) {
System.out.println("D: " + e.getX() + ", " + e.getY());
}
public void mouseMoved(MouseEvent e) {
System.out.println("M: " + e.getX() + ", " + e.getY());
}
public static void main(String [] args) {
new MouseMotionListenerTest();
}
}
キー入力に関するイベントを監視します。
import java.awt.*;
import java.awt.event.*;
public class KeyListenerTest extends Frame implements KeyListener {
KeyListenerTest() {
super("KeyListenerTest");
TextField tf1 = new TextField();
tf1.addKeyListener(this);
add(tf1);
setSize(200, 100);
show();
}
public void keyPressed(KeyEvent e) {
System.out.println("Press: " + e.getKeyText(e.getKeyCode()));
}
public void keyReleased(KeyEvent e) {
System.out.println("Release: " + e.getKeyText(e.getKeyCode()));
}
public void keyTyped(KeyEvent e) {
System.out.println("Type: " + e.getKeyChar());
}
public static void main(String [] args) {
new KeyListenerTest();
}
}
テキスト入力に関するイベントを監視します。Swing では DocumentListener を使用するのが一般的です。
import java.awt.*;
import java.awt.event.*;
public class TextListenerTest extends Frame implements TextListener {
TextListenerTest () {
super("TextListenerTest");
TextField tf1 = new TextField();
tf1.addTextListener(this);
add(tf1);
setSize(200, 100);
show();
}
public void textValueChanged(TextEvent e) {
TextField tf = (TextField)e.getSource();
System.out.println(tf.getText());
}
public static void main(String [] args) {
new TextListenerTest();
}
}
チェックボックスなどのアイテムの状態を監視するリスナーです。
import java.awt.*;
import java.awt.event.*;
public class ItemListenerTest extends Frame implements ItemListener {
ItemListenerTest() {
super("ItemListenerTest");
Checkbox cb1 = new Checkbox("CB1");
cb1.addItemListener(this);
add(cb1);
setSize(200, 100);
show();
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("SELECTED");
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
System.out.println("DESELECTED");
}
}
public static void main(String [] args) {
new ItemListenerTest();
}
}
ウィンドウリスナーを実装するには、必要の有無に関わらず windowOpened など 7個のインタフェースを実装する必要があります。この面倒を簡略するために、インタフェースを複数持つものについては アダプター が用意されています。下記のように、ウィンドウリスナーを WindowAdapter のサブクラスとして定義することにより、必要なインタフェースのみを実装することが可能になります。
import java.awt.*;
import java.awt.event.*;
public class WindowAdapterTest extends Frame {
WindowAdapterTest() {
super("WindowAdapterTest");
this.addWindowListener(new MyWindowListener());
setSize(200, 100);
show();
}
public static void main(String [] args) {
new WindowAdapterTest();
}
}
class MyWindowListener extends WindowAdapter {
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
}