This lesson, and the next several lessons will concentrate on the use of the GUI components available in the JDK 1.1.x release. You should also be aware that an entirely new set of lightweight GUI components, known collectively as the Swing set, are in the pre-beta evaluation stage at JavaSoft as of January 1998.
It is rumored that JDK 1.2 (or perhaps 2.0) will be released in the first or second quarter of 1998, and that the Swing components will be part of that release.
The Swing components are not intended to replace the AWT components from JDK 1.1, but rather are intended to supplement those components with a set of components that provide a consistent look and feel across all platforms. In addition to providing a consistent look and feel, several components (such as progress bars) are included in the Swing set that are not included in the JDK 1.1 AWT.
We have learned how to handle events in both JDK 1.0.2 and JDK 1.1. We have learned how to use the layout managers in JDK 1.1. These two topics form the basis for the design and implementation of a Graphical User Interface.
The next step is to take a look at the variety of components that are available to combine with layout and event handling to produce an effective Graphical User Interface.
The available components are defined by classes in the package java.awt. Our approach will be to group those classes into categories and study the material on a category basis. As of this writing, it looks as if the categories will be:
This lesson will concentrate on the Container classes.
In our previous studies of layout managers, we made extensive use of Container classes. However, since our emphasis was on layout, we simply took the containers for granted and didn't provide much in the way of discussion relative to the containers themselves.
We will rectify that situation in this lesson and learn more about the Container classes.
java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.awt.Window | +----java.awt.Frame |
All objects, including arrays, implement the methods of the Object class. In keeping with the general concepts of inheritance in Object-Oriented Programming, the methods provided by the Object class are general-purpose in nature.
As of 3/9/97, there are no fields and there are eleven methods in the Object class, including such general-purpose methods as the familiar toString() and finalize() methods.
Component has no public constructors so it cannot be instantiated. However, it can be extended which provides a new capability of JDK 1.1 known as Lightweight Components which will be the topic of a subsequent lesson.
As of 3/9/97, Component has six fields which are inherited by its subclasses. Also as of 3/9/97, Component has more than 100 methods. Thus, many of the methods used by the different classes used to produce GUIs are inherited from the Component class.
As we will see, the Panel class, the Window class, and the Frame class are subclasses of Container and inherit the methods defined in Container.
During our studies of layout managers, we saw sample programs which used the add() method of the Container class to place other components into objects subclassed from Container.
According to the JDK 1.1 documentation:
"Components added to a container are tracked in a list. The order of the list will define the components' front-to-back stacking order within the container. If no index is specified when adding a component to a container, it will be added to the end of the list (and hence to the bottom of the stacking order)." |
The list of methods includes five overloaded versions of the add() method. During our studies of the layout managers, we learned that the different layout managers require the use of different versions of the add() method for placing components into container objects.
The Container class is also the home of the setLayout() method which is used to specify the layout manager for a particular container and the validate() method which is used to cause changes to a layout to take effect. We have used these two methods in numerous sample programs in previous lessons.
As of 3/9/97, Panel has only one method: addNotify(), which is used to creates the Panel's peer. Briefly, the peer is the platform-dependent manifestation of the platform-independent object.
Normally a panel has no visual manifestation in its own right.. However, it is possible to make a panel visible by setting its background color to something other than the default. The following sample program causes three Panel objects to have background colors of yellow, red, and blue in order to make them visible for purposes of illustration.
This program illustrates the use of Panel objects to create a composite top-level Graphical User Interface object. Three Panel objects are added to a Frame object using the add() method.
The layout manager for the Frame object is specified to be FlowLayout using the setLayout() method.
The background colors of the three panels are set to yellow, red, and blue so that they will be visible in the Frame object.
One component is placed on each of the Panel objects using the add() method.
If you compile and run this program (and make certain that the Frame is sufficiently wide) you should see three Panel objects lined up across the top of the Frame object. The color of the panels going from left to right should be yellow, red, and blue. The yellow panel should contain a TextField object, the red panel should contain a Label object, and the blue panel should contain a Button object.
If the Frame object is not sufficiently wide to accommodate the three panels across the top, they will automatically rearrange themselves in the standard fashion for a FlowLayout manager.
A windowClosing() event listener object is instantiated and registered on the frame to terminate the program when the frame is closed.
The program was tested using JDK 1.1 running under Win95.
Panel leftPanel = new Panel(); leftPanel.setBackground(Color.yellow); leftPanel.add(new TextField("Left Panel is Yellow")); |
Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.add(leftPanel); myFrame.add(middlePanel); myFrame.add(rightPanel); |
/*File Container01.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 This program illustrates the use of Panel objects to create a composite top-level Graphical User Interface object. The program was tested using JDK 1.1.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; //=======================================================// public class Container01 { public static void main(String[] args){ //instantiate a Graphical User Interface object GUI gui = new GUI(); }//end main }//end class Container01 //=======================================================// class GUI { public GUI(){//constructor //Build three Panels with colored backgrounds each // containing a non-active component. Panel leftPanel = new Panel(); leftPanel.setBackground(Color.yellow); leftPanel.add(new TextField("Left Panel is Yellow")); Panel middlePanel = new Panel(); middlePanel.setBackground(Color.red); middlePanel.add(new Label("Middle Panel is Red")); Panel rightPanel = new Panel(); rightPanel.setBackground(Color.blue); rightPanel.add(new Button("Right Panel is Blue")); //Instantiate a Frame object using a FlowLayout manager // and place the three Panel objects on the Frame Frame myFrame = new Frame( "Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.add(leftPanel); myFrame.add(middlePanel); myFrame.add(rightPanel); myFrame.setSize(500,200); myFrame.setVisible(true); //Instantiate and register a window listener to // terminate the program when the Frame is closed. myFrame.addWindowListener(new Terminate()); }//end constructor }//end class GUI definition //=======================================================// class Terminate extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing }//end class Terminate |
The default layout for a Window object is BorderLayout. Windows are capable of generating the following window events:
The definition of the constructor is as follows:
public Window(Frame parent)
Parameters: parent - the owner of the dialog |
"Window is rarely used directly; its subclasses Frame and Dialog are more commonly useful." -- Java in a Nutshell, by David Flanagan. |
"Generally you won't create Window objects directly. Instead, you will use a subclass of Window called Frame, described next." -- Java, the Complete Reference, by Herbert Schildt and Patrick Naughton. |
"The Window class is a lot like Panel except that it creates its own top-level window, as opposed to being contained by any other Panel. Most users of the Window class will more than likely use the Frame subclass, described below, which has several convenience methods to deal with resizing, window titles, cursors, and menu bars. That said, let's skip ahead to the Frame class." -- The Java Handbook, by Patrick Naughton. |
I particularly like to use the Frame class for sample programs because it is easy to instantiate, and equally important, it is easy to terminate.
The Frame class extends the Window class. A Frame object is an (optionally) resizable top-level window with
Frames are capable of generating the following types of window events:
As of 3/9/97, the Frame class had fourteen fields, all of which are used in dealing with the appearance of the cursor.
Also, as of that date, it had two overloaded public constructors:
In addition, on 3/9/97, the Frame class had more than a dozen methods, a couple of which were deprecated methods left over from JDK 1.0.2. Some of the deprecated methods have to do with getting and setting the cursor. From the specifications, it appears that those methods have been moved up the hierarchy to the Component class.
This program illustrates use of the Frame class and some of its methods. The program instantiates a Frame object with three buttons labeled:
When the user clicks the Hand Cursor button, the shape of the cursor is changed to the familiar hand-shaped cursor using the setCursor() method.
When the user clicks the Default Cursor, the shape of the cursor is changed back to the default cursor for the system.
Note that in the two latter cases, you must move the mouse pointer away from the button to see the change in the cursor take place.
This is an extremely simple program in comparison with other programs already studied in these lessons.
To prevent the program from being boring, I decided to use this opportunity to review the abbreviated syntax of Inner Classes (a topic covered in an earlier lesson).
All of the event Listener objects are instantiated and registered on the three Button objects and the "close" box using the abbreviated syntax of Inner Classes. If this looks foreign to you, you might want to go back and review that lesson.
A windowClosing() event listener object is instantiated and registered on the frame to terminate the program when the frame is closed.
The program was tested using JDK 1.1 running under Win95.
Button titleButton = new Button("GetTitle"); |
myFrame = new Frame("Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.add(titleButton); |
titleButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println(myFrame.getTitle()); }//end actionPerformed() }//end ActionListener );//end addActionListener() |
myFrame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing() }//end WindowAdapter );//end addWindowListener |
/*File Container02.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 This program illustrates use of the Frame class and some of its methods. The program was tested using JDK 1.1.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; import java.util.*; //=======================================================// public class Container02 { public static void main(String[] args){ //instantiate a Graphical User Interface object GUI gui = new GUI(); }//end main }//end class Container02 //=======================================================// class GUI { Frame myFrame; public GUI(){//constructor //Instantiate three Button objects with the // labels shown Button titleButton = new Button("GetTitle"); Button handCursorButton = new Button("Hand Cursor"); Button defaultCursorButton = new Button( "Default Cursor"); //Instantiate a Frame object with title shown. // Specify FlowLayout mgr. myFrame = new Frame("Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); //Add the three Button objects to the Frame object. myFrame.add(titleButton); myFrame.add(handCursorButton); myFrame.add(defaultCursorButton); //Set the Frame size and make it visible myFrame.setSize(250,200); myFrame.setVisible(true); //Instantiate and register ActionListener objects on // the three Button objects using the abbreviated // syntax for Inner Classes. //---------------------------------------------------// titleButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println(myFrame.getTitle()); }//end actionPerformed() }//end ActionListener );//end addActionListener() //---------------------------------------------------// handCursorButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ myFrame.setCursor( new Cursor(Cursor.HAND_CURSOR)); }//end actionPerformed() }//end ActionListener );//end addActionListener() //---------------------------------------------------// defaultCursorButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ myFrame.setCursor( new Cursor(Cursor.DEFAULT_CURSOR)); }//end actionPerformed() }//end ActionListener );//end addActionListener() //---------------------------------------------------// //Instantiate and register a WindowListener object // on the Frame object to terminate the program when // the user clicks the "close" box on the Frame object. myFrame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing() }//end WindowAdapter );//end addWindowListener }//end constructor }//end class GUI definition |
/*File SampProg146.java Copyright 1997, R.G.Baldwin From lesson 130. Without viewing the solution that follows, write a Java program that meets the following specifications. When the program starts, a 500x100 Frame object appears on the screen with your name in the top banner. The Frame contains a yellow rectangle that fills most of the white client area of the Frame. The yellow rectangle contains two objects which are not necessarily the same size. On the left is a TextField containing the text "Left Panel is Yellow". On the right is a red rectangle. The red rectangle contains two objects which are approximately the same size. On the left is a green rectangle containing the text "Middle Panel is Red". On the right is a blue rectangle. The blue rectangle contains a button with the caption "Right Panel is Blue". When you click the close button on the Frame, the program terminates and returns control to the operating system. The program was tested using JDK 1.1.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; //=======================================================// public class SampProg146 { public static void main(String[] args){ //instantiate a Graphical User Interface object GUI gui = new GUI(); }//end main }//end class SampProg146 //=======================================================// class GUI { public GUI(){//constructor //Build three Panels with colored backgrounds each // containing a non-active component. Panel leftPanel = new Panel(); leftPanel.setBackground(Color.yellow); leftPanel.add(new TextField("Left Panel is Yellow")); Panel middlePanel = new Panel(); middlePanel.setBackground(Color.red); Label myLabel = new Label("Middle Panel is Red"); myLabel.setBackground(Color.green); middlePanel.add(myLabel); Panel rightPanel = new Panel(); rightPanel.setBackground(Color.blue); rightPanel.add(new Button("Right Panel is Blue")); //Instantiate a Frame object using a FlowLayout // manager and place the three Panel objects on the // Frame in a nested fashion Frame myFrame = new Frame( "Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); middlePanel.add(rightPanel); leftPanel.add(middlePanel); myFrame.add(leftPanel); myFrame.setSize(500,100); myFrame.setVisible(true); //Instantiate and register a window listener to // terminate the program when the Frame is closed. myFrame.addWindowListener(new Terminate()); }//end constructor }//end class GUI definition //=======================================================// class Terminate extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing }//end class Terminate //=======================================================// |
Q - Write a Java program that meets the following specifications.
/*File SampProg147.java Copyright 1997, R.G.Baldwin From lesson 130 Without viewing the following solution, write a Java program that replicates the functionality of the program named Container02.java with the following differences. 1. Use a crosshair cursor in place of a hand cursor 2. Don't use an abbreviated inner-class for the action listener on the getTitle button. Use an ordinary inner-class instead. The program was tested using JDK 1.1.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; import java.util.*; //=======================================================// public class SampProg147 { public static void main(String[] args){ //instantiate a Graphical User Interface object GUI gui = new GUI(); }//end main }//end class SampProg147 //=======================================================// class GUI { Frame myFrame; public GUI(){//constructor //Instantiate three Button objects with the // labels shown Button titleButton = new Button("GetTitle"); Button crosshairCursorButton = new Button("Crosshair Cursor"); Button defaultCursorButton = new Button( "Default Cursor"); //Instantiate a Frame object with title shown. // Specify FlowLayout mgr. myFrame = new Frame("Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); //Add the three Button objects to the Frame object. myFrame.add(titleButton); myFrame.add(crosshairCursorButton); myFrame.add(defaultCursorButton); //Set the Frame size and make it visible myFrame.setSize(250,200); myFrame.setVisible(true); //Instantiate and register ActionListener objects on // the three Button objects using the abbreviated // syntax for Inner Classes. //---------------------------------------------------// class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println(myFrame.getTitle()); }//end actionPerformed() }//end class MyActionListener titleButton.addActionListener(new MyActionListener()); //---------------------------------------------------// crosshairCursorButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ myFrame.setCursor( new Cursor(Cursor.CROSSHAIR_CURSOR)); }//end actionPerformed() }//end ActionListener );//end addActionListener() //---------------------------------------------------// defaultCursorButton.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ myFrame.setCursor( new Cursor(Cursor.DEFAULT_CURSOR)); }//end actionPerformed() }//end ActionListener );//end addActionListener() //---------------------------------------------------// //Instantiate and register a WindowListener object // on the Frame object to terminate the program when // the user clicks the "close" box on the Frame object. myFrame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing() }//end WindowAdapter );//end addWindowListener }//end constructor }//end class GUI definition |
These tutorials were developed by Richard Baldwin and are the copyrighted property of Richard Baldwin. You have permission to print one copy for your own use, but may not, without written permission from Richard Baldwin, redistribute the tutorial documents. The base material in these lessons is believed by the author to be in the public domain. If you use these lessons for any purpose, you are using them at your own risk, and this author assumes no responsibility or liability for any damages that you may incur.
Java, Sun, HotJava and various other related symbols and names are registered trademarks of Sun Microsystems, Inc. Macintosh is a registered trademark of Apple Computer, Inc. OS/2 is a registered trademark of International Business Machines Corporation. Microsoft, MS-DOS, Visual Basic, Windows, Windows NT, Internet Explorer and Visual J++ are registered trademarks of Microsoft Corporation. Netscape and JavaScript are trademarks of Netscape Communications Corporation. All other trademarks and service marks that may have been inadvertently used in these lessons without proper credit being given are the property of their respective owners. If you feel that your trademark or copyright has been compromised, please notify this author immediately, and an appropriate correction to the document will be issued.
© 1996, 1997, 1998, 1999 Richard G. Baldwin