Solved

Limiting keystrokes but not what is placed in the text field by textField.setText()

Posted on 2004-03-31
7
149 Views
Last Modified: 2010-03-31
I know using a masked formatter or a document can limit what is put into a textfield thorugh the keyboard, but they also limit what can be put into the text field by .setText(). Is there a way that unallowed text could be put into the text field via setText() but not allow the user to type in the unallowed characters?
0
Comment
Question by:wallway
  • 3
  • 2
7 Comments
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10724841
class LengthValidator extends javax.swing.text.PlainDocument{
    public void insertString(int offs,String ,AttributeSet a)throws BadLocationException{
        if (offs<4){
            super.insertString(offs,str,a);
        }    
    }    
}


JTextField tField=new JTextField(new LengthValidator(),"test",4);
0
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10724853
I think I missunderstood you question but anyway this might give you an idea
0
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10724922
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 100 total points
ID: 10725005
Add a simple KeyListener (KeyAdapter), something like this:

public class kA extends KeyAdapter {

     public kA() {
         super();
     }


public void keyTyped(KeyEvent e){

if(!(e.getKeyChar()=='0'||e.getKeyChar()=='1')){e.consume();}
}

}



In that 3rd to last line, just add in whatever characters you dont want added. The rest of your code is still free to say:  textfield.setText(textfield.getText()+ ......
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10725015
>> you dont want added ...

or in this case, do want added!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 90
Need to track down Infection in a Server 2008 domain user profile 7 48
Java Jpanels and Jframe 8 28
object oriented programming comparison 5 55
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now