Course Content‎ > ‎Session 7‎ > ‎

Structure of a Primary Window

JFrame

In Java, frame or primary window is a container for user interface elements such as menu bar. JFrame is the component used to create a primary window. For drawing message strings, you need another component called a panel, which you add to the frame. Message strings can also be directly displayed in a frame, but that is not considered good programming practice. A panel simply groups components within a window or another panel. Layout managers enable you to position components within a panel. In JFrame, components go in the content pane, not directly in the frame. Changing other properties (layout manager, background color, etc.) apply to the content pane. You can access content pane via getContentPane, or if you want to replace the content pane with your container (e.g. a JPanel), use setContentPane. JFrame closes automatically when you click on the close button (unlike AWT Frames). However, closing the last JFrame does not result in your program exiting Java. So, your main JFrame still needs a WindowListener. You get Java (Metal) look by default, so you have to explicitly switch if you want native look. Here is a diagram, showing the structure of a JFrame:
     
      
                   Structure of JFrame

The above diagram shows the structure of a JFrame. Four panes are layered in a JFrame. The root pane, layered pane and the glass pane are of no interest to us; they are required to organize the menu bar and content pane. When designing the frame, you add components into the content pane using the following methods:

    Container contentPane = frame.getContentPane();
    
    Component c = ...;
    
    contentPane.add(c);  

If we wish to add text, we can do that by adding a single panel to the content pane, onto which we will write the text. Panels are implemented by using the JPanel class. Panels have a surface onto which you can draw. They are also containers, which can hold user interface elements such as buttons, sliders etc. In order to add a panel,

    Container contentPane = frame.getContentPane();
    
    JPanel p = new JPanel();
    
    contentPane.add(p);  

Frame Layout 

The JFrame class has few methods for changing how frames look. There are other methods used for working with the size and position of a frame, inherited from the various superclasses of JFrame. Here is a diagram showing the hierarchy of JFrame and JPanel classes:


              Structure of JFrame and JPanel classes   

The Component class is the ancestor of all GUI classes. Here is a simple example showing how the frame is created and closed.

    import javax.swing.*;
    
    class FrameExample1 extends JFrame
    {
        public FrameExample1()
        {
            setTitle(Frame Example 1);
            setSize(300, 200);
        }
        
        public static void main(String[] args)
        {
            JFrame frame = new FrameExample1();
            frame.show();
        }
    }

      
 
Analysis of the program
 
In this example, javax.swing package is imported to get the functionality for the Swing library. javax indicates the Java extension package. This program creates a class called FrameExample1, which behaves just like JFrame in the javax.swing package. This class has a single constructor FrameExample1(). The constructor FrameExample1() sets the title to FrameExample1 with the size of the frame equal to 300 x 200 pixels. The main() method shows the frame and exits. In order to show the frame, first of all, a frame object is created by making a call to the new operator, as shown below:
 
    JFrame Frame = new FrameExample1();     

show() or setVisible() methods are used to bring the frame to the front.  The code in the main program only terminates the main thread, not the program. In swing, the program is not terminated by selecting close from the File menu or by clicking the X button in the upper right-hand corner. These options only hide the window. In this program, you can use Control + C to end the program. Closing the frame terminates the program. It is accomplished differently in AWT and Swing. For example, the AWT requires a window listener to be registered with the frame, and this listener overrides windowClosing() method to call System.exit(), which terminates the application. Swing frame can use the same method. However, they can also take advantage of JFrame's additional methods like setDefaultCloseOperation() method and the EXIT_ON_CLOSE argument, to accomplish the same task.

Here is another example showing a frame with the panel and content pane:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class FrameExample4 extends JFrame
    {  
        public FrameExample4()
        {
            setTitle("Frame Example 4");
            setSize(300, 200);
        }
        public static void main(String[] args)  
        {
            //Creating a new frame by calling constructor
            JFrame frame = new FrameExample4();
            frame.show();
            
            //Creating a panel and adding buttons to panel
            JPanel p = new JPanel();
            JButton button1 = new JButton("Button 1");
            JButton button2 = new JButton("Button 2");
            p.add(button1);
            p.add(button2);
            
            //Adding panel to the content pane
            Container contentPane = frame.getContentPane();
            contentPane.add(p);
            
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }  
            });
        }//main
    }//class FrameExample4
    
    


Analysis of the program
 
The first part of the code is very similar to the other examples, we have already written. Now lets look at the main and see the differences. In the main() program, JFrame constructor is called to create the frame. Now a panel is created using the JPanel constructor. Two buttons are added to this panel. getContentPane() method is used to access the content pane and panel is added to the content pane.  

Comments