Solved

Characters in JTextField, getting focus.

Posted on 1998-10-22
4
277 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

688 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