Question

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);
    }
}
Was it helpful?

Solution 2

The problem you're having is that Java Swing is single threaded. When you're running the main function of the other class, however you do it, the GUI won't be able to keep running until it returns. Try spawning off a new thread that calls the second main method.

private static void runClient() {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            String[] args1={"10"};
            ClientMain.main(args1);
        }
    });
}

EDIT: Updated, as per @Radiodef's suggestion. Missed at the top when you said this second class had to display things on the GUI. Definitely want to go with the invokeLater then.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top