Java: JFrame - Window

Description

The javax.swing.JFrame class is used to create a "window". This window has a few characteristics of its own (title bar, etc), but most of the controls are placed in one of two subareas: content pane or menu bar.

Constructors

You can create a frame like this:

   JFrame w = new JFrame("your title");  // OK, but it's better to use subclassing as below.

Subclass JFrame, build in constructor. Another better way is to define a class, eg MyWindow that extends JFrame, put the code which builds the GUI in the class's constructor, and create an instance of the window from the main program. See example below.

Content pane - Two styles - Get it or set it

There are two common ways to use a JFrame's content pane. Both are commonly used. In both cases, the easiest style is to assign the working version of the content pane to

Common methods

JFrame    w;   // This is the natural Java "window".
Container c;   // This will typically be a JPanel (subclass of Container).
WindowListener listen; //
String    title;
boolean   b;
JMenuBar  mBar;
Using the content pane. See Content Panes
c = w.getContentPane(); Returns window's content pane. Use either get or set, but not both.
w.setContentPane(c); Sets window's content pane to c (or subclass JPanel).
Handling the window's close box. See JFrame Close Box
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This adds a window listener that simply executes System.exit(). It's the short, appropriate, solution for most small programs.
w.addWindowListener(listen); Use this to call your own window closing listener.
Executing the layout.
w.pack(); Finalize layout after everything is added to window and content pane. Don't use validate() with layout managers.
Displaying the window.
w.setVisible(true); Makes the window visible, and starts the Event Dispatch Thread (EDT) which manages the GUI. This is usually called by the main program after calling the constructor. show() should no longer be used.
Miscellaneous.
w.setTitle(title); xxx
w.setResizable(false); Set to false if you don't want the user to resize your window.
w.setJMenuBar(mbar); This is how to add a menubar to the window.
w.setLocationRelativeTo(null); Centers the window. If you even bother with position, this is the most common choice.
Less common positioning and sizing.
w.setSize(w, h); Sets window to pixel width (w) and height (h). The only reasons to use this are to fill the screen or restore a window size from a previous run.
w.setLocation(x, y); Sets the upper left corner of the window to this screen pixel coordinate.

Example

There is typically be a very short main method something like this.

  public static void main(String[] args {
      JFrame windo = new MyExample();
      windo.setVisible(true);
  }

And a class which defines the window.

public class MyExample extends JFrame {
    //... Declare components, something to hold the model.
    . . .
    public MyExample() { // constructor builds GUI
        //... Build the content pane.
        Container content = this.getContentPane();
        content.add(...) // Add components to the content
        . . .
        this.setTitle("My new window");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();   // does layout of components.
    }//end constructor
}