I just want to do a textbox class only accepts integers..
I have done something, but ı think it's not enough.
Can anyone help me, please? Thanks...
import java.awt.TextField
public class textbox extends TextField{
private int value;
public textbox(){
super();
}
public textbox(int value){
setDeger(value);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Why are you using a TextField? Why don't you learn Swing instead of AWT, then you can use a JTextField. Actually you can use a JFormattedTextField which support this requirement. Read the API and follow the link to the tutorial for examples.
When user try to enter non-numeric data, it rejects an deletes the input that is not an integer.
And this is the test class
I think you are missing the point here's a hint, with your code i can still call textfield.setText("i'm not a number");
Since you must use
TextArea
, you might have some success with aTextListener
. Add a listener that restricts the characters entered to just numbers.In pseudo code the event method could do this:
This is easier to do with a
JTextField
as you can replace the document model or just use aJFormattedTextField
.