Solved

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

Posted on 2004-03-31
7
156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

615 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