Reputation: 1559
I'm learning swing and I've created a simple form:
I've implemented on pressing submit button it checks whether all fields are not empty and shows message "Please fill in all the fields" if any field is empty, but I don't know how to return to the form when you press "ok" if any field is empty. Now I'm using "System.exit(0);" but that's not what I need. Thanks.
if (getTxt.trim().equals("") || getTxt2.trim().equals("") || getTxt3.trim().equals("") || getTxt4.trim().equals("") || getTxt5.trim().equals("")) {
JOptionPane.showMessageDialog(null,"Please fill in all the fields!!");
System.exit(0);
}
Upvotes: 0
Views: 2003
Reputation: 15333
You don't need to write System.exit(0) there. It will exit your application.
You can even write your JOptionPane message dialog in better way like this :
JOptionPane.showMessageDialog(yourJFrameName,"Your Message Here", "Title of Dialog Box", kindOfMessage)
yourJFrameName : name of the Frame in which you are displaying your registration frame or if you extending class JFrame then write here this
.
kindOfMessage : it can be
JOptionPane.ERROR_MESSAGE,
JOptionPane.INFORMATION_MESSAGE,
JOptionPane.WARNING_MESSAGE,
JOptionPane.QUESTION_MESSAGE,
or JOptionPane.PLAIN_MESSAGE
Upvotes: 1
Reputation: 15477
Assuming you are writing this code in onClick of Submit button.If so then you can return instead of exit. if you don't have more code in the method then just remove System.exit(0);
if (getTxt.trim().equals("") || getTxt2.trim().equals("") || getTxt3.trim().equals("") || getTxt4.trim().equals("") || getTxt5.trim().equals("")) {
JOptionPane.showMessageDialog(null,"Please fill in all the fields!!");
return;
}
Upvotes: 3
Reputation: 692271
You don't have to do anything. Once the user clicks the OK button, the dialog will close, the showMessageDialog
call will return, and the form wil continue to be displayed. Just remove the System.exit
call (which exits the JVM).
Upvotes: 1
Reputation: 54725
Just simply remove the call to System.exit(0)
. Your application will not exit when the actionPerformed
method associated with the JButton
returns because the Event Dispatch thread will keep it alive, continuing to listen for user input events.
Note that the logic for user interface applications tends to follow this "callback" style of development, whereby fragments of code you've written are called automatically by a library-controlled "I/O" thread. This contrasts with the procedural style of development in non-UI applications where the developer typically dictates the exact order in which statements are executed and is in control of the "primary" thread (or threads).
Upvotes: 2