Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

SWING JTextField (No "PlainDocument usage")  RSS feed

 
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!
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing/AWT forum...
 
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
 
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!