• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Characters in JTextField, getting focus.

Hi there, I have 2 questions.

1)   In my textfield, I would like to watch for certain
characters. For example, if the character '$' is entered
into the textfield, I would like to block it and prevent
it from being entered into the textfield. What I want to
do is that if the user types in:

"my n$ame is T$om"

The textfield will block and ignore the '$' when it is
entered, showing: "my name is Tom". How would I do this?
I tried using keyListener to trap when the '$' is entered,
but I can't prevent it from being entered into the

Also, is it possible to replace '$' with '=' every time
it's entered? Our example would show: "my n=ame is T=om"
then... How to replace the characters?

2. How would I make this textfield get the focus when
   the panel with the textfield is created? Should I
   just call requestFocus()?

Thanks in advance!
  • 2
  • 2
1 Solution
1. call e.consume(); to prevent it enter.
2. Yes, you should call requestFocus() on this field. But it works when the field is visible. Since java has it's own oppinion when a component is visible I use mainly the following scenario:

I define:

class VisibilityManager extends Thread
  Component component_to_track;
  VisibilityManager(Component c)
    component_to_track = c;
  public void run()
    while (!c.isVisible())
      try { sleep(100); } catch (Exception e) {}

Then at the end of the constructor of the class where I want to have the field, I put a line:

(new VisibilityManager(my_textfield)).start();

add-on to 1.

You can replace the character by posting another event to the system queue. You have to create it on the basis of the old one, with the same time, and insert it in the system queue.
This is very tricky but works.

Another, more easy to implement way is to ask for the caret position, get the content of the field, insert the new character, consume the event with the $, and set the new content.

To block user from typing $, try this textfield.

public class MyTextField extends JTextField {

  public MyTextField(int cols) {

  protected Document createDefaultModel() {
    return new MyDocument();

  static class MyDocument extends PlainDocument {

    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException {

      if (str == null) {
      if (str.indexOf((int) '$')) {
         StringBuffer sb = new StringBuffer("");
         char[] src = str.toCharArray();
         for (int i = 0; i < src.length; i++)
           if (src[i] != '$')
         super.insertString(offs, sb.toString(), a);
        } else {
           super.insertString(offs, str, a);
   } // end of class mydocument

 You can go thru the JTextField Documentation where in he gives how to develop a uppercase textfield at


Go thru the description.

I am sorry, i thought  you use swing.
ignore the above comment please.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now