views:

418

answers:

4

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;
    }
}
A: 

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.

camickr
but it's my homework, ı have to do like that.. :(
alex
the man said it... it's his homework! :)
Lirik
There IS a homework tag to the question.
Zaki
+2  A: 

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");

denis
I.o.w. Look at all the methods with which you can input data into the TextField and reimplement/Override them to reject input that is not an integer.
NomeN
+1  A: 

Since you must use TextArea, you might have some success with a TextListener. Add a listener that restricts the characters entered to just numbers.

In pseudo code the event method could do this:

  1. get event source
  2. determine current cursor position
  3. if the character at the current position is not valid input, then
    1. remove the bad character
    2. reset the text in the field

This is easier to do with a JTextField as you can replace the document model or just use a JFormattedTextField.

Kevin Brock
A: 

try looking into overriding methods

Joel