[Logo]
Forums Register Login
SWING JTextField (No "PlainDocument usage")
Hello!
I'm aware that you can fix the user input length into a JTextField by
creating a PlainDocument class and using setDocument mehtod of the JTF.
I need to do it without creating additional classes.
I know another way.

jfield.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke){
if(MyJTextField.getText().length())
Toolkit.getDefaultToolkit().beep();
ke.consume();
}
public void keyReleased(KeyEvent ke){keypressed(ke);}
public void keyTyped(KeyEvent ke){}

But in this case when I select the text of the JTextField and press say
Bar it doesnt delete. I wouldnt like to do additional for certain keys.
And moreover in this case I get the java.lang.StackOverflowError exception.

Any help?
Thank you!
Moving to the Swing/AWT forum...
you can use the PlainDocument class the same way you've used the KeyListener class

It's done!
I've nothing more to ask on it, so if you want you can close this topic.
I give my thanks to you!
I was mistaken.
It will not do.
The example that you provided generates the "Testing$1.class" subclass
together with the "Testing.class" class.
> The example that you provided generates the "Testing$1.class" subclass

as does the KeyListener class in your posted code, which you indicated is OK
 
Originally posted by fedai gandjaliyev:
I was mistaken.
It will not do.
The example that you provided generates the "Testing$1.class" subclass
together with the "Testing.class" class.


So does the example you gave.

This sounds like a homework assignment, and I think you are looking for a JFormattedTextField and a MaskFormatter.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 20, 2017 10:21:24.