Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
by zero exception 10 83
maven module vs maven project 3 114
jsp login check 12 65
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.
Suggested Courses

734 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