# Java: Example - Generic Main

This file defines a class with a `main` method. This template is a reasonable way to start an application, where `GenericGUI` is a subclass of `JPanel` which contains the code to build a GUI with the appropriate listeners.

This code is typical for building a window -- create a JFrame, tell what to do when the close box is clicked, put a panel with content in the window, arrange (pack) the components according to their layout, and finally make the window visible and start the GUI monitoring process (show).

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
    // GenericApp.java - This is the main method for a generic application.
    // Fred Swartz, 2003-Apr
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    /////////////////////////////////////////////////////// class GenericApp
    class GenericApp {

        //====================================================== method main
        public static void main(String[] args) {
            JFrame window = new JFrame("Generic Application");
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setContentPane(new GenericGUI());
            window.pack();
            window.show();
        }//end main

    }//endclass GenericApp
The text from the above example can be selected, copied, and pasted into an editor.

## Notes by line number

1. Every application has a main method declared as

           public static void main(String[] args)

   The main method can be in any class, but putting it in a class by itself makes the program simpler to understand, at least at first.
2. This main method creates a window (JFrame) and initializes it. The string passed to the JFrame constructor will appear in the window's title bar.
3. The close box on a Java window (JFrame) closes the window, but doesn't stop the program. You can add a listener to do anything you want (like ask the user if they want to save their current work), but the simplest is to just make the default action terminate the program.
4. Each window has a content pane, which is really just a JPanel. There are other panes, but you will probably never have a reason to use them. A JFrame starts with a content pane to which you can add your JPanel. Another common technique, and one that is often used in these notes, is to set the content pane to a JPanel you created. The statement in this program creates a new JPanel with `new GenericGUI()` and sets the content pane to this new JPanel. Of course, you must replace "GenericGUI" with the name of your GUI class.
5. Thecontent JPanel has a layout and components (including subpanels) on it. The size of the JPanel, and hence the window, isn't known until the layout process is actually performed. The `pack()` call arranges the components in the layout. After this call, the position and size of all components is known, and hence the size of the window.
6. The call on `show()`, or the equivalent `setVisible(true)`, makes the window visible. However, it does something that isn't so obvious. It starts up another thread to monitor the GUI interface -- watching for mouse clicks etc. This thread is part of the Java runtime system and coninues to run until your program quits.
7. The main program returns! When a C++ main program returns, execution is terminated, so this last step is a big conceptual leap for programmers who are making the transition to GUI programming from traditional read-compute-write text mode interfaces. The GUI style of programming is to create the GUI interface and then stop doing anything!

   After intialization, everything is done only when a listener is called. The program sleeps until the user does something.