Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: Implementing a validity check of input values

Tags:

java

input

swing

In my Swing application, the user must insert numbers and values, before switching to the next window. Now as a clean program should, I check every input if its 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 following (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'm on to change that, but for this question it's irrelevant.

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

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 as I want, BUT the code itself is very ugly, because having multiple input options the method gets 200, 300 or more lines long (as I do not only check if e.g. it's a number, but also if the number makes sense in context of the program logic and so on). Is there a Swing -own method to check such things? Or has anyone a better idea how to realize exactly this functionality with split methods?

like image 813
Valentino Ru Avatar asked Oct 21 '12 12:10

Valentino Ru


People also ask

How do you validate an entry in Java?

Input validation in java using Scanner in Java We can use regex to acquire a string in a specified format to validate a string value. The hasNext() function used to validate a string that can only include alphabets using a regex.

What is validation in Java Swing?

validate() : In Swing when you create a Component, it is not valid i.e. its valid property is false . A component is said to be valid, when its width, height, location and stuff has been determined. This is usually done by calling their validate() method, directly or indirectly.

How is input validated?

Input validation is the process of testing input received by the application for compliance against a standard defined within the application. It can be as simple as strictly typing a parameter and as complex as using regular expressions or business logic to validate input.

Is Swing outdated Java?

JavaFX new fixes will continue to be supported on Java SE 8 through March 2022 and removed from Java SE 11. Swing and AWT will continue to be supported on Java SE 8 through at least March 2025, and on Java SE 11 (18.9 LTS) through at least September 2026.


2 Answers

One solution would be to use Swing's InputVerifier to validate input for every JTextField used. As the validation functionality is the same for each field, a single instance could 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);
like image 72
Reimeus Avatar answered Nov 08 '22 18:11

Reimeus


You can get real time validation through the use a DocumentFilter

You check out this and this for some examples.

I do think, however, you might find the JFormattedTextField are more suitable solution in this case.

like image 29
MadProgrammer Avatar answered Nov 08 '22 18:11

MadProgrammer