Java running main method of other class, when JButton is pressed

Adrian picture Adrian · Oct 24, 2013 · Viewed 8.4k times · Source

I am trying to develop a JFrame which has two buttons that would let me to call the main method of other classes. The first try was to put it directly into the actionPerformed of each button, this will cause the JFrame of the other class to open but showing only the title of it and not showing any contents of the JPanel additionally freezing the program (can't even press close button, have to go into task manager or eclipse to kill it). The second try was adding a method call in actionPerformed, and adding the method will this time call the main method of other class however the same result (freeze of program).

For testing purposes I have placed the call to main method of other class, straight in this class main method which has proven to me that the frame of other class has successfully appeared, including all its JPanel contents, functionality etc.

I know I could make some kind of infinite loop in my main method to wait until a boolean is set to true, but then I though there must be some less-expensive way to get it working. So here I am asking this question to you guys.

Here is the code of the 2nd try;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


    public class Chat {
    public static void main (String[] args) {


        JFrame window = new JFrame("Chat Selection");

        //Set the default operation when user closes the window (frame)
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set the size of the window
        window.setSize(600, 400);
        //Do not allow resizing of the window
        window.setResizable(false);
        //Set the position of the window to be in middle of the screen when program is started
        window.setLocationRelativeTo(null);

        //Call the setUpWindow method for setting up all the components needed in the window
        window = setUpWindow(window);

        //Set the window to be visible
        window.setVisible(true);

    }

    private static JFrame setUpWindow(JFrame window) {
        //Create an instance of the JPanel object
        JPanel panel = new JPanel();
        //Set the panel's layout manager to null
        panel.setLayout(null);

        //Set the bounds of the window
        panel.setBounds(0, 0, 600, 400);

        JButton client = new JButton("Run Client");
        JButton server = new JButton("Run Server");
        JLabel author = new JLabel("By xxx");

        client.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //run client main
                runClient();
            }
        });

        server.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //run server main
            }
        });

        panel.add(client);
        client.setBounds(10,20,250,200);
        panel.add(server);
        server.setBounds(270,20,250,200);
        panel.add(author);
        author.setBounds(230, 350, 200, 25);

        window.add(panel);

        return window;
    }

    private static void runClient() {

        String[] args1={"10"};
        ClientMain.main(args1);
    }
}

Answer

Radiodef picture Radiodef · Oct 24, 2013

Only one main method is allowed per application. Honestly I am not sure what you are trying to do or think is supposed to happen when you call main on other classes. When you call main on other classes all you are doing is calling a method that happens to be called main and passing args to it. Your freezing is probably because you are not using Swing correctly:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html