2

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

mustangDC
  • 945
  • 1
  • 12
  • 33
  • What does the Feedback class do? That's the code we need to see, as well as the code that the formWindowClosing method is used in. Create a short, self-contained, **runnable** Java application that illustrates the problem, and then we can help you. – Gilbert Le Blanc May 23 '15 at 17:28
  • Feedback class is just another form which has some `JLabel`, `JtextField` and `JButtons`. Nothing fancy at all. ***Posting the code in my question*** – mustangDC May 23 '15 at 17:39
  • @GilbertLeBlanc : Hope the code is as per you have wanted, as I have removed the declarations and other Layout and Image settings – mustangDC May 23 '15 at 17:58
  • @GilbertLeBlanc : I am pretty much sure that the `Feedback.java` is not disposing itself anywhere/or any bug is present in the code which automatically disposes it – mustangDC May 23 '15 at 18:21
  • Should not `fb.setVisible(true)` be called from another thread? – Zereges May 23 '15 at 22:48
  • Can you please suggest the way of doing that? @Zereges.... I am still discovering things as a beginner. – mustangDC May 24 '15 at 02:19
  • `initComponents()` is undefined for both classes. Copy-paste both to your editor in a new project and fix **all** compilation errors. You also have a lot of unnecessary code, like the logging stuff and a lot of action listeners. – user1803551 May 24 '15 at 04:49
  • @user1803551 - I have clearly mentioned that declarations and initialization are not given as it would enlarge the code as I have done it in an IDE. But I have checked all classes, there are no errors and i can guarantee that. – mustangDC May 24 '15 at 06:06
  • This is still not an [MCVE](http://stackoverflow.com/help/mcve) as requested in the *first* comment. You don't need to guarantee anything - my compiler gives compilation errors which are clearly correct. – user1803551 May 24 '15 at 06:12
  • I am providing the compiled, error free code @user1803551. But should I mention the code related to Layout, Panel, Button settings? – mustangDC May 24 '15 at 06:19
  • @user1803551 : Did you see my EDIT 2? – mustangDC May 24 '15 at 06:23
  • 1
    Yes, and the code is not confusing, it's just not compiling. Also, you will want to explain what a "*page*" is because this is not a browser. If you want a frame that when you click on a button changes its contents, then post code that tries to do that and only that (see also `CardLayout`).. – user1803551 May 24 '15 at 06:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78619/discussion-between-mustangdc-and-user1803551). – mustangDC May 24 '15 at 06:31
  • @user1803551, Hope this is what is needed – mustangDC May 24 '15 at 07:23
  • @GilbertLeBlanc : Hope this is correct, precise and optimum – mustangDC May 24 '15 at 07:24
  • Close enough (still, put your code in a compiler and you'll see that you mistyped the names of the classes in the constructors; easy to fix though). – user1803551 May 24 '15 at 07:27
  • :) . Sorry for that but appreciate that you have corrected. Thanks!!! @user1803551 – mustangDC May 24 '15 at 07:29

1 Answers1

2

Your main problem is probably that you set

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

This causes the whole application to exit when you close the frame, regardless of which option you chose in the dialog. Nevertheless, you shouldn't use multiple JFrames, but instead use CardLayout as demonstrated here:

public class Example extends JFrame {

    private String redirectedForm = "RedirectedForm";
    private String windowClosing = "WindowClosing";

    Example() {

        CardLayout cl = new CardLayout();
        getContentPane().setLayout(cl);
        add(new WindowClosing(), windowClosing);
        add(new RedirectedForm(), redirectedForm);

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent evt) {

                int a = JOptionPane.showConfirmDialog(Example.this, "Did you complete your task?");
                if (a == JOptionPane.CANCEL_OPTION) {
                    return;
                }
                if (a == JOptionPane.NO_OPTION) {
//                  cl.next(frame.getContentPane()); // This to show the next card
                    cl.show(getContentPane(), redirectedForm); // This is to show a specified card
                }
                else {
                    dispose();
                }
            }
        });

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String args[]) {

        new Example();
    }
}

class WindowClosing extends JPanel {

    public WindowClosing() {

        JLabel closeLabel = new JLabel("The WindowClosing panel");
        add(closeLabel);
    }
}

class RedirectedForm extends JPanel {

    public RedirectedForm() {

        JLabel label = new JLabel("You have been redirected to this Form becuse you have closed the previous one");
        add(label);
    }
}

By setting DO_NOTHING_ON_CLOSE the windowClosing event will not terminate the program. Instead you choose what do do depending on the selected option: nothing, exit, or switch panel.

Notes:

  • Don't use the int values of static constants such as JOptionPane.NO_OPTION, just use the reference to it instead.
  • You might want to set an owner for the JOptionPane.
Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Thats is what I needed. Infact came to know about new concepts, will definitely implement that. Cheers and Thanks!!! – mustangDC May 24 '15 at 09:33