«^»
2.6. Stage C: adding GUI components to the window

Some GUI components will now be put into the window that is displayed by the program. As with the previous program, the first step is to create an object to represent that window:

JFrame tJFrame = new JFrame("GetDateProg: Stage C");
In order to get our program to display a textfield and a button, the program needs to create these GUI components and add them to the content pane of the frame.

The Swing API contains classes that enable us to represent textfields and buttons:

JTextField tJTextField = new JTextField("hello", 35);
JButton tJButton = new JButton("Get Date");
There are a number of constructors for these classes (as shown at javaapi:javax/swing/JTextField.html and javaapi:javax/swing/JButton.html). The ones used above create a textfield containing 35 columns which is initialized to the string "hello", and a button containing a label with the characters "Get Date". Once again, this just creates two objects within an executing Java program that represent a textfield and a button. It does not do anything with them, such as make them visible.

These GUI components need to be added to the content pane of the JFrame window. We can get a reference to the JFrame's content pane by executing the method getContentPane:

Container tContentPane = tJFrame.getContentPane();
The actual way in which GUI components are displayed within a container such as this content pane is controlled by a layout manager. The default layout manager for a content pane is a layout known as BorderLayout.

The BorderLayout layout manager allows you to use a method called add to place components in five divisions of the content page appropriately known as NORTH, WEST, CENTER, EAST and SOUTH. These divisions are illustrated by this diagram:

You do not have to put a component in each division: the layout manager will arrange the spacing of the components that you do provide:

tContentPane.add(tJTextField, BorderLayout.NORTH);
tContentPane.add(tJButton, BorderLayout.SOUTH);
The class java.awt.BorderLayout conveniently provides constants named NORTH, WEST, CENTER, EAST and SOUTH.

If you are unhappy with the layout, you can either use Container's setLayout method to choose another layout manager or you can use an object of class Box or JPanel to group items together. Both of these classes are in the javax.swing package: the Box class uses a layout called BoxLayout, and the JPanel class uses a layout called FlowLayout.

When you have added all of the components to the content pane, you should apply the method pack (from the class java.awt.Window) to the frame. This arranges for the size of the frame to be just big enough to accommodate the components. So this time there is no call of setSize: instead the call of pack determines an appropriate size for the window. A call of pack often appears just before a call of setVisible:

tJFrame.pack();
tJFrame.setVisible(true);

Here is the complete program:

0026: // Stage C: adding GUI components to the window.         // GetDateProg.java
0027: // Barry Cornelius, 22nd November 1999
0028: import java.awt.    BorderLayout;
0029: import java.awt.    Container;
0030: import javax.swing. JButton;
0031: import javax.swing. JFrame;
0032: import javax.swing. JTextField;
0033: public class GetDateProg
0034: {
0035:    public static void main(final String[] pArgs)
0036:    {
0037:       final JFrame tJFrame = new JFrame("GetDateProg: Stage C");
0038:       final JTextField tJTextField = new JTextField("hello", 35);
0039:       final JButton tJButton = new JButton("Get Date");
0040:       final Container tContentPane = tJFrame.getContentPane();
0041:       tContentPane.add(tJTextField, BorderLayout.NORTH);
0042:       tContentPane.add(tJButton, BorderLayout.SOUTH);
0043:       tJFrame.pack();
0044:       tJFrame.setVisible(true);
0045:    }
0046: }

What gets displayed when this program is executed is shown in the Figure. As this time there is no call of setLocation, the window will appear in the top left-hand corner of the screen.