vsarunov
vsarunov

Reputation: 1547

Restart application by disposing the old

Good day, I was looking for a way of restarting my application, I came up of with a concept of this:

submit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {

              new Application();
              dispose();


            }
        });

Where the Object that is being disposed of, is the new Object that is being created . My question is if it is a good way of restarting the application? Why not? and is there a better way?

P.S if similar question exist I truly could not have found it.

Upvotes: 1

Views: 93

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109597

Following the MVC paradigm, I have a main application that is the controller, and starts the view, a JFrame, using SwingUtilities.invokeLater. And on the JFrame the correct setDefaultCloseOperation(DISPOSE_ON_CLOSE)

So the JFrame actionPerformed would call the controller for a restart.

A more clear class structure one has, when one wants a single-instance application. Opening the application again, say by opening another document, should go to a single application running.

My standard solution is to make an RMI port bound application, and in the main check whether already an application runs on that port and transfer the command line for a start to that remote RMI server, and exit.

  • Controller
  • View (JFrame)
  • Model + View (JInternalFrame)
  • Model + View
  • Model + View

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

My question is if it is a good way of restarting the application?

No it's not.

Why not?

There's no retention of previous information, you would need to unnecessarily duplicate reading in of resources, and you would likely irritate the user by throwing multiple needlessly created windows at them.

Also, ask yourself how many professional applications that you use behave this way. If you need to work on a different word processing document, does MS Word close and restart? Does Excel do this if you need to read a new spreadsheet?

and is there a better way?

Yes, give all pertinent classes in your program a reset() method that resets all fields of the class and GUI components to their original state and that calls this same method on all constituent objects that the class contains so that a reset() call on the main class will result in cascading calls to all sub-objects. How this is coded will depend completely on the specifics of your program.

If your GUI is built along a clear MVC design pattern, then typically a control object would call reset() on the main model object, and then the GUI (the view) would react to the change in the model's state.

Upvotes: 5

Related Questions