I have two JFrame forms that accept information such as Name,Rollno,Enrollment Number,gender,etc.Name and gender are on first JFrame form while the other details are on the second JFrame form.All the information gets stored in MySql Database after clicking 'next form' button.The same button takes the user to the next form.If the user fills all the details in the first form and clicks the 'next form' button of the form and he/she exits the second form,only half the database will be having the details and the other half will have nothing. So, please suggest me such code that when the user clicks the close(X) button of the second form, all the details saved in the Database get deleted. (Forms are entirely coded in NetBeans) Please help me out.
Asked
Active
Viewed 444 times
0
-
U want to delete only data or table structure as well? – Pavan Kumar Jul 17 '17 at 15:15
-
Listen for a close event on the frame and then run your SQL commands to delete the data. https://stackoverflow.com/questions/9093448/do-something-when-the-close-button-is-clicked-on-a-jframe – tima Jul 17 '17 at 15:18
-
@pawansharma only details – Sourabh Jul 17 '17 at 15:19
-
2what u have tried yet can You post the code here?? you can fire a delete query on a button click – Pavan Kumar Jul 17 '17 at 15:21
-
well,i had no idea how to do it, so i haven't tried anything. – Sourabh Jul 17 '17 at 15:23
1 Answers
2
The best way to solve this method is to not save anything into the database until the user completes all forms (ie. cache the information from the first form and save it once all once the user completes all the data).
Option number 2 would be to delete the data when the X button is clicked, however this isn't the best since the application can be force closed or such, case in which you can't perform any action. Anyways, here's how to detect the X button being clicked.
First you want to disable automatic exiting:
// You probably have EXIT_ON_CLOSE instead of this somewhere in your code already, just replace with this
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then you want to handle the close event yourself:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// In here you can delete any data in the database or even use JOptionPane to confirm the exit
// To close the frame once everything is done, do this:
frame.dispose();
}
});
Note: You can use System.exit(0)
instead of frame.dispose()
to ensure that everything is terminated right away.

Chaoz
- 2,119
- 1
- 20
- 39
-
If i choose to go with the first option, then how can i get details the user has entered in the first form into the second form? – Sourabh Jul 17 '17 at 15:55
-
@SourabhShegane Depends on what the form contains. Pretty much just store everything in an array / arraylist / hashmap (probably hashmap
would be the best since you can add pretty much anything - string or primitive types). To save you'd do `hashmap.put("fieldName", fieldValue)`, then to get your data `hashmap.get("fieldName")` with appropiate casting. – Chaoz Jul 17 '17 at 16:06 -