I have a JFormattedTextField which accepts only numeric input. The custom
mask formatter contains the "#" character for the format string. Works
great, however, I would like to "control" the behaviour of the edit. The
default implementation uses a "beep" if the data input is invalid. I would
like to extend the editing behaviour to include a "JOptionPane" error
message dialog, because not every user has an audible (speakers).
Stuart Leonard - 17 Jan 2005 04:00 GMT
I have tried trapping the invalid entry using either a custom Key listener
or a Document.
The problem is that the data has already been commited to the
JFormattedTextField
before these edits are invoked. For example, if I enter an invalid
character, the event
does not occur. If I press the tab key to change focus, the event is then
fired.
Following is the code that I am using to trap the event.
class FolderKeyEvent extends KeyAdapter
{
/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent event) {
int key = event.getKeyCode();
char textchar = event.getKeyChar();
String keytext = event.getKeyText(key);
System.out.println("keytext = " + keytext);
if (textchar != '0' &&
textchar != '1' )
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(f, "Enter a \"0\" or \"1\" only.");
event.consume();
return;
}
}
}
/**
* Provides a <Document> object to ensure that only a "0" or "1"
* are entered
*/
class FolderDocument extends PlainDocument
{
public void insertString(int pos, String text,
AttributeSet attr) throws BadLocationException
{
System.out.println("\"" + text + "\"");
for (int i=0; i<text.length(); i++)
{
if (text.charAt(i) != '0'
&& text.charAt(i) != '1'
&& text.charAt(i) != ' ')
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(f, "Enter a \"0\" or \"1\"
only.");
return;
}
}
System.out.println("text = " + text);
super.insertString(pos,text,attr);
}
}
>I have a JFormattedTextField which accepts only numeric input. The custom
>mask formatter contains the "#" character for the format string. Works
>great, however, I would like to "control" the behaviour of the edit. The
>default implementation uses a "beep" if the data input is invalid. I would
>like to extend the editing behaviour to include a "JOptionPane" error
>message dialog, because not every user has an audible (speakers).
Roland - 21 Jan 2005 10:50 GMT
> I have a JFormattedTextField which accepts only numeric input. The custom
> mask formatter contains the "#" character for the format string. Works
> great, however, I would like to "control" the behaviour of the edit. The
> default implementation uses a "beep" if the data input is invalid. I would
> like to extend the editing behaviour to include a "JOptionPane" error
> message dialog, because not every user has an audible (speakers).
Create an (anonymous) subclass of JFormattedTextField and override
invalidEdit().
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JFormattedTextField.html#inv
alidEdit()>

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \