/**
* @version 1.20 1999-09-26
* @author Cay Horstmann
*/
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyVetoException;
import java.io.Serializable;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
public class IntTextBean extends JTextField implements Serializable {
public IntTextBean() {
this(0, 10);
}
public IntTextBean(int defval, int size) {
super("" + defval, size);
addFocusListener(new FocusListener() {
public void focusGained(FocusEvent event) {
if (!event.isTemporary()) {
lastValue = getValue();
}
}
public void focusLost(FocusEvent event) {
if (!event.isTemporary()) {
editComplete();
}
}
});
}
public void editComplete() {
Integer oldValue = new Integer(lastValue);
Integer newValue = new Integer(getValue());
try {
fireVetoableChange("value", oldValue, newValue);
// survived, therefore no veto
firePropertyChange("value", oldValue, newValue);
} catch (PropertyVetoException e) { // someone didn't like it
JOptionPane.showMessageDialog(this, "" + e, "Input Error",
JOptionPane.WARNING_MESSAGE);
setText("" + lastValue);
requestFocus();
// doesn't work in all JDK versions--see bug #4128659
}
}
public int getValue() {
try {
return Integer.parseInt(getText());
} catch (NumberFormatException exception) {
return 0;
}
}
public void setValue(int v) throws PropertyVetoException {
Integer oldValue = new Integer(getValue());
Integer newValue = new Integer(v);
fireVetoableChange("value", oldValue, newValue);
// survived, therefore no veto
setText("" + v);
firePropertyChange("value", oldValue, newValue);
}
protected Document createDefaultModel() {
return new IntTextDocument();
}
public Dimension getMinimumSize() {
return new Dimension(XMINSIZE, YMINSIZE);
}
private int lastValue;
private static final int XMINSIZE = 50;
private static final int YMINSIZE = 20;
public static void main() {
// JFrame f = new JFrame()
// asdf
}
}
class IntTextDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null)
return;
String oldString = getText(0, getLength());
String newString = oldString.substring(0, offs) + str
+ oldString.substring(offs);
try {
Integer.parseInt(newString + "0");
super.insertString(offs, str, a);
} catch (NumberFormatException e) {
}
}
}
|