Reputation: 9
I need to type only characters in the text field area in java. I tried below codes by changing to various types.
char a= evt.getKeyChar();
if(!(a>='0'&&a<='9')){
evt.consume();
}
Upvotes: 1
Views: 1411
Reputation: 285415
It looks like you're using a KeyListener in a JTextField, and if so, don't, as you'll mess up the JTextField function. Instead use either a JFormattedTextField or give the text field's Document a DocumentListener.
Edit
For example:
import java.text.Format;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
public class DigitOnlyFieldTest {
private static void createAndShowGui() {
Format digitFormat = NumberFormat.getIntegerInstance();
JFormattedTextField digitField = new JFormattedTextField(digitFormat);
digitField.setColumns(10);
JTextField textField = new JTextField(10);
((PlainDocument) textField.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
text = text.replaceAll("\\D", "");
super.insertString(fb, offset, text, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
text = text.replaceAll("\\D", "");
super.replace(fb, offset, length, text, attrs);
}
});
JPanel mainPanel = new JPanel();
mainPanel.add(new JLabel("Formatted Text Field: "));
mainPanel.add(digitField);
mainPanel.add(new JLabel("Text Field with Doc Filter: "));
mainPanel.add(textField);
JFrame frame = new JFrame("DigitOnlyFieldTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Now this will work with user text entry, with copy and paste, and with software-based text entry.
Upvotes: 3
Reputation: 24616
Try to use DocumentFilter, and attach that to the Document
of the JTextField
.
Here is a small example for the same, for help:
package to.uk.gagandeepbali.examples;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import java.awt.*;
/**
* Created by Gagandeep Bali on 7/12/2014.
*/
public class FilterCharacters {
private void displayGUI() {
JFrame frame = new JFrame("Filtering Text Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
JTextField tField = new JTextField(10);
((AbstractDocument)tField.getDocument()).setDocumentFilter(new MyDocumentFilter());
contentPane.add(tField);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new FilterCharacters().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
package to.uk.gagandeepbali.examples;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import java.awt.*;
/**
* Created by Gagandeep Bali on 7/12/2014.
*/
public class MyDocumentFilter extends DocumentFilter {
@Override
public void insertString(DocumentFilter.FilterBypass fp, int offset,
String stringToFilter, AttributeSet aset)
throws BadLocationException {
int len = stringToFilter.length();
if (Character.isLetter(stringToFilter.charAt(len - 1)))
super.insertString(fp, offset, stringToFilter, aset);
else
Toolkit.getDefaultToolkit().beep();
}
@Override
public void replace(DocumentFilter.FilterBypass fp, int offset, int length,
String stringToFilter, AttributeSet aset)
throws BadLocationException {
int len = stringToFilter.length();
if (Character.isLetter(stringToFilter.charAt(len - 1)))
super.replace(fp, offset, length, stringToFilter, aset);
else
Toolkit.getDefaultToolkit().beep();
}
}
Upvotes: 4
Reputation: 739
Try this example it may help to you
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class SwingDemo extends JFrame {
private JTextField textField;
public SwingDemo() {
super("TextField Filter Example By SuRu");
setBounds(100, 100, 500, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
getContentPane().add(new JLabel("Accepts Only Digits: "));
textField = new JTextField(40);
getContentPane().add(textField);
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (!Character.isDigit(e.getKeyChar())) {
e.consume();
}
}
});
}
public static void main(String[] args) {
new SwingDemo().setVisible(true);
}
}
Upvotes: -1