I have been trying to just display the alphabets in the jtextfield.Even though the other keys are pressed the jtextfield should not display them only the alphabets are to be displayed.can you please help me with this..
Asked
Active
Viewed 349 times
-3
-
Take a look at [DocumentFilter Examples](http://www.jroller.com/dpmihai/entry/documentfilter) and [Implementing a Document Filter](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter) – MadProgrammer Oct 28 '14 at 05:16
-
possible duplicate of [jTextField accept only alphabet and white space](http://stackoverflow.com/questions/14058505/jtextfield-accept-only-alphabet-and-white-space) – Kunjan Thadani Oct 28 '14 at 05:26
-
but sir i dont need to display the other keys pressed in the jtextbox. – siri Oct 28 '14 at 05:29
-
you just need to remove the condition for whitespace. thats it. – Kunjan Thadani Oct 28 '14 at 05:32
2 Answers
1
Start by taking a look at Implementing a Document Filter
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class TestFilter {
public static void main(String[] args) {
new TestFilter();
}
public TestFilter() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JTextField field = new JTextField(10);
((AbstractDocument)field.getDocument()).setDocumentFilter(new CharFilter());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(field);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CharFilter extends DocumentFilter {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String string, AttributeSet attr)
throws BadLocationException {
StringBuilder buffer = new StringBuilder(string);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isAlphabetic(ch)) {
buffer.deleteCharAt(i);
}
}
super.insertString(fb, offset, buffer.toString(), attr);
}
@Override
public void replace(DocumentFilter.FilterBypass fb,
int offset, int length, String string, AttributeSet attr) throws BadLocationException {
if (length > 0) {
fb.remove(offset, length);
}
insertString(fb, offset, string, attr);
}
}
}
You ma also find DocumentFilter Examples helpful

MadProgrammer
- 343,457
- 22
- 230
- 366
-
Works fine for me, trade you a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem – MadProgrammer Oct 28 '14 at 05:57
-
Take the `CharFilter`, copy it to it's own class. Modify your code to use it as demonstrated, `((AbstractDocument)field.getDocument()).setDocumentFilter(new CharFilter());`... – MadProgrammer Oct 28 '14 at 06:16
-
This is an example of the concept of which you are going to have to integrate into your code, we're not going to do it for you...you can't afford our rates... ;) – MadProgrammer Oct 28 '14 at 06:16
-
-1
here is a solution if you want only alphabets to be entered to a jtextfield,and no other keys should be entered even though they get pressed.in your jframe just select your jtextfield rightclick on jtextfield you will find events and then go to events click on keyTyped event and then write the following code to enter only the alphabets into your jtextfield.
private void tfempidKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
char key = evt.getKeyChar();
if (key > '0' && key < '9') {
evt.consume();
}

siri
- 9
- 7
-
`KeyListener` is a poor choice for filtering content within text components, this is why the `DocumentFilter` API was created. There is no guarantee in the order that `KeyListener`s are notified, meaning that the key stroke may already have been delivered to the text component's `Document` and under some circumstances if cause a document mutation exception to be generated. – MadProgrammer Oct 31 '14 at 04:46