Developing a swing application where I need to redirect the user to another JFrame if he clicks the No button
on a JoptionPane.showConfirmDialog
. I wrote this code :
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int a=JOptionPane.showConfirmDialog(this, "Did you complete your task?");
JOptionPane.showMessageDialog(null, a);
if(a==1){
RedirectedForm rf=new RedirectedForm();
rf.setVisible(true);
}
}
But if(a==1){...}
part does not seem to work. Even if I create a reference to the JFrame-RedirectedForm
which I want to redirect to, the window gets disposed always. Can anybody explain the reason and suggest a solution to this?
Any help is appreciated, Thanks!!!
Providing both the java classes below :
WindowClosing.java
import javax.swing.JOptionPane;
public class WindowClosing extends javax.swing.JFrame {
public WindowClosing() {
initComponents();
}
private void initComponents() {
window_close_label = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
window_close_label.setText("Window Close Form");
pack();
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int a=JOptionPane.showConfirmDialog(this, "Did you complete your task?");
JOptionPane.showMessageDialog(null, a);
if(a==1){
RedirectedForm rf=new RedirectedForm();
rf.setVisible(true);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WindowClose().setVisible(true);
}
});
}
private javax.swing.JLabel window_close_label;
}
RedirectedForm.java
public class RedirectedForm extends javax.swing.JFrame {
public RedirectedForm() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("You have been redirected to this Form becuse you have closed the previous one");
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RedirectForm().setVisible(true);
}
});
}
private javax.swing.JLabel jLabel1;
}
I have edited the code once more, hope this will give a clearer idea about the problem