Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SWING JTextField (No "PlainDocument usage")  RSS feed

 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing/AWT forum...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the PlainDocument class the same way you've used the KeyListener class

 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
fedai gandjaliyev
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was mistaken.
It will not do.
The example that you provided generates the "Testing$1.class" subclass
together with the "Testing.class" class.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!