プログラマメモ2 - programmer no memo2

AWTEventListenerを使って、テキストコンポーネント上にマウスカーソルを移動させると、枠線を変えるというコードです。 2008/10/09

Toolkit.getDefaultToolkit().addAWTEventListener使うと画面のイベントを根こそぎ取得できるので、それを使って、テキストコンポーネント上にマウスカーソルを移動させると、枠線を変えるというコードです。

static {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
class TempBorder extends LineBorder{
public TempBorder(Color color, Border original) {
super(color, 5);
this.original = original;
}
private static final long serialVersionUID = 1L;
Border original;
}
public void eventDispatched(AWTEvent awtevent) {
// System.out.println(awtevent);
if (awtevent instanceof MouseEvent) {
MouseEvent m = (MouseEvent) awtevent;
if (m.getID() == MouseEvent.MOUSE_ENTERED) {
// System.out.println(awtevent);
if (m.getSource() instanceof JTextComponent) {
JTextComponent textComponent = (JTextComponent) m
.getSource();
TempBorder tempBorder = new TempBorder(Color.RED, textComponent.getBorder());
textComponent
.setBorder(tempBorder);
System.out.println("textcomponent:[" + textComponent + "]");
}
}
if (m.getID() == MouseEvent.MOUSE_EXITED) {
if (m.getSource() instanceof JTextComponent) {
JTextComponent textComponent = (JTextComponent) m
.getSource();
Border border = textComponent.getBorder();
if(border instanceof TempBorder){
TempBorder tempBorder = (TempBorder)border;
textComponent.setBorder(tempBorder.original);
}
}
}
}
}
}, 0xffffffff);
}

: