Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-03-31
7
Medium Priority
?
157 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 400 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

715 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