Solved

Characters in JTextField, getting focus.

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 54
CSV file parsing thru Java 13 40
java example issue 5 46
How to fix  socket closed error 11 32
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

730 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