• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

SWING JTextField (No "PlainDocument usage")

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Swing/AWT forum...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic