Solved

Characters in JTextField, getting focus.

Posted on 1998-10-22
4
252 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

911 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

23 Experts available now in Live!

Get 1:1 Help Now