JTextField accepts only alphabet and space

I want the user to enter only the alphabet or a space if the user enters a different character, I want to pass the message using jOptionPane I was looking for and I tried the code below

if (!(Pattern.matches("^[a-zA-Z]+$", answerField1.getText()))) JOptionPane.showMessageDialog(null, "Please enter a valid character", "Error", JOptionPane.ERROR_MESSAGE); 

but now everything that I enter gives an error

now i changed the code

 Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$"); if (!(letterPattern.matcher(answerField1.getText()).matches())) { JOptionPane.showMessageDialog(null, "Please enter a valid character", "Error", JOptionPane.ERROR_MESSAGE); } 

now it gives a message only the first time the user enters a number. how can i solve this problem?

+2
source share
2 answers

Use DocumentFilter , here is an example I made, it will only accept alphabetic characters and spaces:

 import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; import javax.swing.text.DocumentFilter.FilterBypass; public class Test { public Test() { initComponents(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } private void initComponents() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField jtf = new JTextField(); //add filter to document ((AbstractDocument) jtf.getDocument()).setDocumentFilter(new MyDocumentFilter()); frame.add(jtf); frame.pack(); frame.setVisible(true); } } class MyDocumentFilter extends DocumentFilter { @Override public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException { for (int n = string.length(); n > 0; n--) {//an inserted string may be more than a single character ie a copy and paste of 'aaa123d', also we iterate from the back as super.XX implementation will put last insterted string first and so on thus 'aa123d' would be 'daa', but because we iterate from the back its 'aad' like we want char c = string.charAt(n - 1);//get a single character of the string System.out.println(c); if (Character.isAlphabetic(c) || c == ' ') {//if its an alphabetic character or white space super.replace(fb, i, i1, String.valueOf(c), as);//allow update to take place for the given character } else {//it was not an alphabetic character or white space System.out.println("Not allowed"); } } } @Override public void remove(FilterBypass fb, int i, int i1) throws BadLocationException { super.remove(fb, i, i1); } @Override public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException { super.insertString(fb, i, string, as); } } 
+8
source

You have a semicolon at the end of the first line. Thus, this is not very well tested.

A statement like this (what you have):

 if (condition) ; 

will execute an empty statement (;) if the condition is true, and then go to the next line. If the condition is false, it simply moves on to the next line. These two actions have the same result.

You can try using curly braces for all if statements. Sometimes it is tiring, but complicates corruption.

 if (!(Pattern.matches("^[a-zA-Z]+$", answerField1.getText()))) { JOptionPane.showMessageDialog(null, "Please enter a valid character", "Error", JOptionPane.ERROR_MESSAGE); } 

What would i do. You can just delete the semicolon.

+1
source

All Articles