Reputation: 29
so I am trying to verify what is typed into my text field is only numeric and not alphanumeric. I've looked into formatted textboxes, but haven't been able to implement them, due to lack of understanding the tutorial oracle has up on their site. I did some more searching and found the key listener class and it seemed like the best option.
So when I run the program and type into the text field instead of consuming the character event it just beeps at me. Here's the code:
private void buildPanel()
{
// Create a label to display instructions.
messageLabel = new JLabel("Enter a time in seconds the object has fallen");
// Create a text field 10 characters wide
fallingTextField = new JTextField(10);
// Add a keylistener to the text field
fallingTextField.addKeyListener(new TextFieldListener());
// Create a button with the caption "Calculate"
calcButton = new JButton("Calcualte");
// Add an action listener to the button
calcButton.addActionListener(new CalcButtonListener());
//Create a JPanel object and let the panel field reference it
panel = new JPanel();
// Add the label, text field, and button components to the panel
panel.add(messageLabel);
panel.add(fallingTextField);
panel.add(calcButton);
}
/**
The TextFieldListener class checks to see if a valid key input is typed into
the text field.
*/
private class TextFieldListener implements KeyListener
{
/**
The keyPressed method
@param evt is a key event that verifies a number is inputed otherwise
the event is consumed.
*/
public void keyPressed(KeyEvent evt)
{
char c = evt.getKeyChar();
if(Character.isAlphabetic(c))
{
getToolkit().beep();
evt.consume();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent ev)
{
}
}
Upvotes: 1
Views: 2197
Reputation: 140633
Here:
if(Character.isAlphabetic(c))
{
getToolkit().beep();
You tell your listener to beep every time you enter an alphabetic character. So it beeps when you do that.
Everything working exactly as the code implies it should.
For the other part of your question; check out the Javadoc for consume() (which is inherited from InputEvent).
Consumes this event so that it will not be processed in the default manner by the source which originated it.
You are not on the source side of things. The event has already been created, and dispatched to listeners. Calling consume()
in your context ... simply doesn't do anything any more. It is more or less a "no op".
Again: you wrote code that waits for keyboard input, and when the input is alphabetic, it beeps. That is all that your code does. I assume if you provide a "/" for example ... nothing should happen. If you want to validate, then you need a "feedback" loop; for example your listener could overwrite the text field content when it detects bad input.
Upvotes: 2
Reputation: 141
I think the problem is that the character has already been added by the time the event has been processed. You need to set the value of the field without the last character, like:
fallingTextField.setValue(fallingTextField.getValue().substring(0, fallingTextField.getValue().length - 1))
Alternatively, you can use the example at Oracle site and use a Mask input, like the following:
zipField = new JFormattedTextField(
createFormatter("#####"));
...
protected MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
return formatter;
}
Upvotes: 1