Solved

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

Posted on 2004-03-31
7
146 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

20 Experts available now in Live!

Get 1:1 Help Now