tags:

views:

72

answers:

4

I was just curious where i should place the main function in a Java Swing program. It seems as If it's just way too short too create a brand new class for.

+2  A: 

Put it into your main JFrame class

public class MyFrame extends JFrame {
    public static void main(String args[]) {
        new MyFrame();
    }

    MyFrame() {
            // ...
    }
}
Georgy Bolyuba
A: 

You can put it in any class. It makes sense to put it in the class that represents your main dialog in your app. There is no need to create a class just for main.

akf
+5  A: 

I would not recommend putting the main method inside of any of your Swing components. It doesn't fit well inside a Swing component because it has nothing to do with the components themselves.

Just create a main method in a separate class. It is alright that it is short.

Mushing the logic for running your program into the display logic seems like too much coupling.

jjnguy
+2  A: 

I would not put it in the View class. If you're using MVC, and Swing is the View, then main belongs with Controller. That's the class responsible for starting the app, instantiating the View, and collaborating with Model objects to fulfill the use cases.

The Controller should implement the Listener interfaces, because it responds to Swing events as they occur.

I would not have your View extend JFrame. Make the working bits of Swing extend JPanel. When the Controller instantiates the View, it should create a JFrame, add in the JPanels it needs, register itself as the Listener for all Swing events, and make the JFrame visible. At that point your app is up, running, and ready to go.

duffymo