sailwind
asked on
Characters in JTextField, getting focus.
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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
I am sorry, i thought you use swing.
ignore the above comment please.
thanx
ignore the above comment please.
thanx
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