Java Swing: Implementing Validation of Input Values

In my Swing application, the user must insert numbers and values ​​before switching to the next window. Now that a clean program should, I check every input if it is valid or not, and if not, an error message is displayed and the next window does not open.

The structure of this check is as follows (example):

Button buttonToOpenNextWindow = new JButton("next"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(checkValidty){ // (...) new WindowA(); frame.dispose(); // (*) } } }); 

(*) Note: I know that the principle of multiple JFrames is ugly, and I have to change it, but for this question it does not matter.

Now the focus of this question is checkValidity() , which I structured as follows:

 private boolean checkValidity(){ // check input 1 try{ Integer.parseInt(textField1.getText()); }catch (NumberFormatException e){ new ErrorDialog("input 1 is invalid!"); // own implemented dialog return false; } // check input 2 try{ Integer.parseInt(textField2.getText()); }catch (NumberFormatException e){ new ErrorDialog("input 2 is invalid!"); // own implemented dialog return false; } // (...) // check input n try{ Integer.parseInt(textField_n.getText()); }catch (NumberFormatException e){ new ErrorDialog("input n is invalid!"); // own implemented dialog return false; } return true; } 

This works exactly the way I want, but the code itself is very ugly because, having several input parameters, the method gets 200, 300 or more lines long (since I not only check if this number is there, but if the number makes sense in the context of program logic, etc.). Is there a Swing -own method to test such things? Or does anyone know better how to implement this particular functionality using split methods?

+7
source share
4 answers

One solution would be to use a Swing InputVerifier to validate the input for each JTextField used. Since the verification functionality is the same for each field, one instance can be used for all components:

 public class MyNumericVerifier extends InputVerifier { @Override public boolean verify(JComponent input) { String text = ((JTextField) input).getText(); try { Integer.parseInt(text); } catch (NumberFormatException e) { return false; } return true; } } InputVerifier verifier = new MyNumericVerifier() textField1.setInputVerifier(verifier); 
+15
source

I prefer to use an improved version of JFormattedTextField . Thanks to the improved one, I mean improving carriage behavior, checking for every change to provide immediate feedback to the user (for example, changing the background color when typing incorrectly), .... This is combined with a button that is disabled until entry will not be valid.

Key benefits over mouse clicks and error messages:

  • instant feedback for the user. If web applications can avoid going back to the server and use javascript for immediate feedback, there is no excuse for not having this in desktop applications. Pressing the button to confirm is "90".
  • visual feedback is important, but rather InputVerifier , which simply avoids changing focus.
  • highly reusable component. Just make sure your “utility code code” contains a bunch of Format (for dates, doubles, integers, ranges, ...), and you can handle almost any situation.
  • due to the use of Format s, it is easy to configure for different Locale s
  • You do not need to parse the input after the JFormattedTextField . All parsing code is in a format, and you can simply use JFormattedTextField#getValue
  • All checks are performed using the JFormattedTextField . You know that the value you retrieve with getValue is valid.
+6
source

There are many things you can do. Simple is a method that checks the list of strings for parsing in int. For additional checks, you will have more methods that check some common things, like a range of numbers. So divide each type of test into your own method and compose them as needed.

Otherwise, there are complete validation frameworks that handle such things. I would say they are easily accessible.

+2
source

You can get real-time validation using DocumentFilter

You can find this and this for some examples.

I really think, however, you may find JFormattedTextField more suitable solution in this case.

+1
source

All Articles