Stanley Mungai
Stanley Mungai

Reputation: 4150

JoptionPane Validation

I have a Swing GUI where I am restricting the user registration so that the username and the password cannot be the same. I am using JoptionPane for the task with the following code:

public void actionPerformed(ActionEvent e) {
String username = tuser.getText();
String password1 = pass1.getText();
String password2 = pass2.getText();
String workclass = wclass.getText();
Connection conn = null;

try {
    if(username.equalsIgnoreCase(password1)) {
       JOptionPane.showMessageDialog(null,
           "Username and Password Cannot be the same. Click OK to Continue",
           "Error", JOptionPane.ERROR_MESSAGE); 
       System.exit(0);
    }
...

The problem is that I had to use System.exit(0); without it, the next code was getting executed. Even after the JOptionPane poped up, the registration was succeeding. I do not need the system to exit, but I need the user to be kept on the registration page after the validation. What is the best way to do this? Is there other convenient ways of doing this rather than using the JOptionPane?

Upvotes: 2

Views: 907

Answers (4)

padman
padman

Reputation: 501

place that next code into else part may be it works

if(username.equalsIgnoreCase(password1))
{
   JOptionPane.showMessageDialog(null, "Username and Password Cannot be the   same. Click OK to Continue","Error",JOptionPane.ERROR_MESSAGE); 

}
else 
{
  //place that next code
  // username and password not equals, then it will execute the code
}

Upvotes: 3

01es
01es

Reputation: 5410

First of all, it is best if the UI and business logic (in this case, validation) are separated. Have them separate sort of suggest a better way of handling interaction on its own. Thus, it makes sense to create a separate class UserValidation with method boolean isValid(). Something like this:

public class UserValidation {
    private final String name;
    private final String passwd;
    private final String passwdRpt;

    public UserValidation(final String name, final String passwd, final String passwdRpt) {
        this.name = name;
        this.passwd = passwd;
        this.passwdRpt = passwdRpt;
    }

    public boolean isValid() {
       // do your validation logic and return true if successful, or false otherwise
    }
}

Then the action code would look like this:

public void actionPerformed(ActionEvent e) {
        if (new UserValidation(tuser.getText(), pass1.getText(), pass2.getText()).isValid()) {
           // do everything needed is validation passes, which should include closing of the frame of dialog used for entering credentials.
        }

        // else update the UI with appropriate error message -- the dialog would not close by itself and would keep prompting user for a valid entry
}

The suggested approach gives you a way to easily unit test the validation logic and use it in different situations. Please also note that if the logic in method isValid() is heavy than it should be executed by a SwingWorker. The invocation of SwingWorker is the responsibility of the action (i.e. UI) logic.

Upvotes: 2

Eng.Fouad
Eng.Fouad

Reputation: 117587

You need to place your code within endless loop, and break it upon successful result. Something like:

while(true)
{
    // get input from user

    if(vlaidInput) break;
}

Upvotes: 3

Robin
Robin

Reputation: 36611

Replace

System.exit(0);

with

return;

if you do not want the rest of the method to be performed

Upvotes: 3

Related Questions