Solved

Characters in JTextField, getting focus.

Posted on 1998-10-22
4
261 Views
Last Modified: 2012-08-13
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
textfield.

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!
0
Comment
Question by:sailwind
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
diakov earned 100 total points
ID: 1226160
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)
  {
    super("thread");
    component_to_track = c;
  }
  public void run()
  {
    while (!c.isVisible())
    {
      try { sleep(100); } catch (Exception e) {}
    }
    c.requestFocus();
  }
}

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();


0
 
LVL 8

Expert Comment

by:diakov
ID: 1226161
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.

Cheers,
  Nik
0
 
LVL 4

Expert Comment

by:evijay
ID: 1226162
To block user from typing $, try this textfield.



public class MyTextField extends JTextField {

  public MyTextField(int cols) {
    super(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) {
        return;
      }
      if (str.indexOf((int) '$')) {
         StringBuffer sb = new StringBuffer("");
         char[] src = str.toCharArray();
         for (int i = 0; i < src.length; i++)
           if (src[i] != '$')
             sb.append(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

http://java.sun.com/products/jfc/swingdoc-api/javax/swing/JTextField.html

Go thru the description.

vijay
0
 
LVL 4

Expert Comment

by:evijay
ID: 1226163
I am sorry, i thought  you use swing.
ignore the above comment please.
thanx

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet  URL Rewriting 1 36
oracle 11g 23 84
difference of if loops 23 49
Java Restore security prompts not working 10 13
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

810 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