Reputation: 109
I am trying to disallow special characters in JTextField
. This is for a project where I need to do form validation. At the moment I can only allow numbers but I cannot disallow special characters such as @
or #
or /
or \
. Below is what I have made so far.
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
int i= Integer.parseInt(jTextField2.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter Int Value", "Invail Input", JOptionPane.ERROR_MESSAGE);
}
}
Upvotes: 0
Views: 2744
Reputation: 20913
Here is an example that uses InputVerifier
. It is a minimal example so you can copy the code as is and run it. Note that class InputVerifier
was modified in JDK 9. Nonetheless the below code should work with any JDK version.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.text.JTextComponent;
public class InpVerif implements Runnable {
private JFrame frame;
@Override // java.lang.Runnable
public void run() {
createAndDisplayGui();
}
private void createAndDisplayGui() {
frame = new JFrame("Input Verifier");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets.bottom = 5;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.top = 5;
JLabel label = new JLabel("Enter Text");
mainPanel.add(label, gbc);
JTextField textField = new JTextField(10);
textField.setInputVerifier(new SpecialCharactersVerifier());
gbc.gridx = 1;
mainPanel.add(textField, gbc);
JLabel label2 = new JLabel("Disallowed Characters");
gbc.gridx = 0;
gbc.gridy = 1;
mainPanel.add(label2, gbc);
JTextField textField2 = new JTextField("@ # / \\");
gbc.gridx = 1;
mainPanel.add(textField2, gbc);
return mainPanel;
}
private class SpecialCharactersVerifier extends InputVerifier {
private String regex = "^.*[@#/\\\\].*$";
@Override
public boolean shouldYieldFocus(JComponent input) {
boolean verified = verify(input);
if (!verified) {
JOptionPane.showMessageDialog(frame,
"These characters not allowed: @ # / \\",
"ERROR: Illegal Character[s]",
JOptionPane.ERROR_MESSAGE);
}
return verified;
}
@Override
public boolean verify(JComponent input) {
boolean verified = true;
if (input instanceof JTextComponent) {
JTextComponent textComponent = (JTextComponent) input;
String text = textComponent.getText();
if (text != null) {
verified = !text.matches(regex);
}
}
return verified;
}
}
/**
* @param args
*/
public static void main(String[] args) {
EventQueue.invokeLater(new InpVerif());
}
}
Note that JavaFX is [supposedly] the successor to Swing, so if you are considering new GUI development in java, perhaps you should consider using JavaFX. If you must use Swing, I highly recommend Creating a GUI With JFC/Swing.
Upvotes: 1
Reputation: 1695
I think, instead of adding listeners and using just a simple JTextField
, you can use JFormattedTextField
. As it is said in Oracle docs:
JFormattedTextField - A JTextField subclass that allows you to specify the legal set of characters that the user can enter.
Check the links below:
Upvotes: 2
Reputation: 1682
Maybe something like this:
public void keyPressed(KeyEvent ke) {
String value = tf.getText();
if (ke.getKeyChar() != '@' && ke.getKeyChar() != '#' && ke.getKeyChar() != '/'
&& ke.getKeyChar() != '\') {
tf.setEditable(true);
} else {
tf.setEditable(false);
label.setText("Please enter Int Value, Invail Input");
}
}
Upvotes: 0
Reputation: 822
Entering numeric values JTextField :
you can use addKeyListener : keyPressed
jTextField2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent k) {
String value = jTextField2.getText();
int len = value.length();
if (k.getKeyChar() >= '0' && k.getKeyChar() <= '9') {
jTextField2.setEditable(true);
} else {
jTextField2.setEditable(false);
}
}
});
Upvotes: 0