Solved

Characters in JTextField, getting focus.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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