0

I need a program that when I click the button, it will rerun the main(),aka, restart the program, but I am stuck on how to do it. I actually do not know if it is possible to do this. Hope someone could help me out. Thank you.

package tst;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class callMain {
    private JButton buttons;

    public static void main(String[] args) {
        System.out.println("how are you");

    }

    public callMain() {
        JButton button = new JButton("btn");
        button.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            // I want to rerun the program
            }
    });
}
8
  • 1
    JButton without container ?
    – Viet
    Dec 1, 2016 at 3:36
  • 2
  • @Jerry06 Thanks for pointing out. I skipped some details in my code ha.
    – alex007
    Dec 1, 2016 at 3:36
  • 1
    Side point: yours is a terrible program design, and you should seriously re-think it. It suggests that you're still thinking in terms of linear console programming and are trying to shoehorn this paradigm into event-driven GUI programming, and when you do that, you end up with bad results that are rigid and brittle. Dec 1, 2016 at 3:41
  • 1
    Instead, think in terms of event driven programming and program state -- reset the GUI's model back to its initial state on press of that button. Dec 1, 2016 at 3:43

1 Answer 1

-1

Do this when you want to call your main method again:

callMain.main(new String[] {});

Or even easier, if you are in the callMain class:

main(new String[] {});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.