swing テキストコンポーネントで入力制限させてついでに点滅
2007/03/22
java
swing
シナリオ
swingを使用してテキスト入力欄で、半角英数字のみ入力を許可して、関係ない文字を入力したら点滅させて入力させないようにする。
リスナーです。
初期化にテキストコンポーネント自身を使用してます。
背景が白い以外で設定されていることを想定してますので、もともと白ですと点滅しているようにみえないです。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
public class InputLimitationDocument implements UndoableEditListener {
private static final long serialVersionUID = 1L;
private JTextComponent textComponent;
public InputLimitationDocument(JTextComponent textComponent) {
this.textComponent = textComponent;
}
/* 実行中かどうかのフラグ */
boolean isDoing = false;
public void undoableEditHappened(final UndoableEditEvent e) {
if (e.getSource() instanceof Document) {
Document document = (Document) e.getSource();
try {
String s = document.getText(0, document.getLength());
// check
if (!s.matches("[[A-Za-z0-9]\\s]*")) {
e.getEdit().undo();
if (isDoing)
return;
isDoing = true;
final Color orgColor = textComponent.getBackground();
textComponent.setBackground(Color.WHITE);
final Timer timer = new Timer(0, null);
ActionListener actionListener = new ActionListener() {
int count = 0;
public void actionPerformed(ActionEvent e) {
textComponent.setBackground(textComponent
.getBackground() == orgColor ? Color.WHITE
: orgColor);
count++;
// 点滅2回
if (count <= 5)
return;
textComponent.setBackground(orgColor);
isDoing = false;
timer.setRepeats(false);
}
};
timer.setDelay(200);
timer.addActionListener(actionListener);
timer.start();
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
public class InputLimitationDocument implements UndoableEditListener {
private static final long serialVersionUID = 1L;
private JTextComponent textComponent;
public InputLimitationDocument(JTextComponent textComponent) {
this.textComponent = textComponent;
}
/* 実行中かどうかのフラグ */
boolean isDoing = false;
public void undoableEditHappened(final UndoableEditEvent e) {
if (e.getSource() instanceof Document) {
Document document = (Document) e.getSource();
try {
String s = document.getText(0, document.getLength());
// check
if (!s.matches("[[A-Za-z0-9]\\s]*")) {
e.getEdit().undo();
if (isDoing)
return;
isDoing = true;
final Color orgColor = textComponent.getBackground();
textComponent.setBackground(Color.WHITE);
final Timer timer = new Timer(0, null);
ActionListener actionListener = new ActionListener() {
int count = 0;
public void actionPerformed(ActionEvent e) {
textComponent.setBackground(textComponent
.getBackground() == orgColor ? Color.WHITE
: orgColor);
count++;
// 点滅2回
if (count <= 5)
return;
textComponent.setBackground(orgColor);
isDoing = false;
timer.setRepeats(false);
}
};
timer.setDelay(200);
timer.addActionListener(actionListener);
timer.start();
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
}
: