0

I have a swing JFrame and in a some place of the code i need to display a JOptionPane for the user to select: if yes - I need ht JFrame to run again and exit the existing one if no - this is solved I just need to exit.

here is a JFrame code:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainFrame frame = new MainFrame();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }           
        }

    });

}

and here is the JOptionPane code which i created:

int ok = JOptionPane.showConfirmDialog(null, "would you like to enable USB in your device?", "USB", JOptionPane.YES_NO_OPTION);

        if (ok==JOptionPane.YES_OPTION){
            Thread t = new Thread(){

                public void run(){
                    String[] args = { };
                    MainFrame f = new MainFrame();
                    f.main(args);
                }

            }.start();
        }

        else if(ok==JOptionPane.NO_OPTION){
            System.exit(ok);
        }

I created the thread and it is opening another JFrame but not closing the existing one!

Thanks for any help :)

Omarkk
  • 83
  • 1
  • 11
  • if frame is visible, frame.dispose() frame = null; and after that, frame = new .... but that would mean, that frame needs to be in a larger scope – Stultuske May 13 '15 at 11:01
  • 2
    don't invoke static methods on objects, but try to adopt the habit to invoke those methods on the class itself `MainFrame.main(args)` or use non-static methods `public void main(String[] args)` – Mr Tsjolder from codidact May 13 '15 at 11:05
  • 2
    1) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) in a single `JFrame` as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) `t.start();` should be scheduled to occur on the Event Dispatch Thread. – Andrew Thompson May 13 '15 at 11:07
  • @MrTsjolder I changed the calling as it is static to 'MainFrame.main(args)' but this not solving the problem , thanks any way :) – Omarkk May 13 '15 at 11:08
  • I solved the problem by interrupting the current Thread `Thread.currentThread().interrupt();` to end the JOptionPane message and return back to the mainframe again .. Thanks for your help .. – Omarkk May 13 '15 at 11:32

0 Answers0