We have learned how to handle events in both JDK 1.0.2 and JDK 1.1 and we have learned how to use the layout managers in JDK 1.1 (which isn't greatly different from using layout managers in JDK 1.0.2). 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. We have discussed container classes and non-text classes. As of this writing, it looks as if the remaining categories will be:
The TextField class can be used to produce a component that will accept one line of user text as input. It can also be used to display one line of text in a "non-editable" fashion with a border.
The TextArea class can be used to produce a component that will accept multiple lines of user text as input. It can also be used to display multiple lines of scrollable text in a "non-editable" fashion.
TextField and TextArea extend TextComponent which extends Component.
The Label class can be used to display one line of text. This component is inherently non-editable and that attribute cannot be modified by the program.
The TextField class extends the TextComponent class which extends the Component class. Thus, the many different methods which are defined in theTextComponent, Component and Object classes are also available to objects of the TextField class.
Before going on, we need to take a look at the TextComponent class which is so important to both the TextField and TextArea classes.
According to the JDK 1.1 documentation, the description of the TextComponent class, which is the superclass of both the TextField class and the TextArea class is:
"A TextComponent is a component that allows the editing of some text." |
Therefore, it is not possible to instantiate an object of the TextComponent class.
However, the TextComponent class is the repository for a large number of methods which are inherited not only by TextField but also by other class that subclasses TextComponent.
Some of the methods of the TextComponent class which are particularly interesting include the following:
|
Now back to our discussion of the TextField class. As of 3/13/97, the TextField class has no fields and has the following public constructors:
|
|
The program places a TextField object in a Frame object. The String "Initial text" is initially displayed in the TextField object at program startup.
An ActionListener object is instantiated and registered on the TextField object. An action event occurs when the user presses the Enter key while the TextField object has the focus. The purpose of the ActionListener object in this program is to extract and display the text in the TextField object in two different ways:
Code in the overridden actionPerformed() method also uses the getText() method of the TextComponent class to access and display all of the text in the TextField object.
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.3 running under Win95.
The first interesting code fragment instantiates a TextField object with an initial text string. Then an ActionListener object is instantiated and registered on the TextField object.
TextField myTextField = new TextField("Initial Text"); myTextField.addActionListener( new MyActionListener(myTextField)); |
The next interesting code fragment is the overridden actionPerformed() method which uses the getSelectedText() method from the TextField class and the getText() method from the TextComponent class to obtain and display the selected portion of the text followed by all the text.
public void actionPerformed(ActionEvent e){ System.out.println( "Selected text is: " + aTextField.getSelectedText()); System.out.println( "All text is: " + aTextField.getText()); }//end actionPerformed() |
/*File TextField01.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 The program places a TextField object in a Frame object. The String "Initial text" is initially displayed in the TextField object at program startup. An ActionListener object is instantiated and registered on the TextField object. An action event occurs when the user presses the Enter key while the TextField object has the focus. The purpose of the ActionListener object is to extract and display the text in the TextField object in two different ways: 1. Display all the text in the TextField object. 2. Display only that portion of the text that has been selected by by the user using any of the available ways of selecting text. When the user presses the Enter key while the TextField object has the focus, an event is trapped in the overridden actionPerformed() method of the ActionListener object. Code in the overridden method uses the getSelectedText() method of the TextField class to access and display the text that has been selected by the user. Code in the overridden actionPerformed() method also uses the getText() method to access and display all of the text in the TextField object. 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.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; //=======================================================// public class TextField01 { public static void main(String[] args){ GUI gui = new GUI();//instantiate a GUI }//end main }//end class TextField01 //=======================================================// class GUI { public GUI(){//constructor //Instantiate a TextField object and place an initial // String in it. TextField myTextField = new TextField("Initial Text"); //Instantiate and register an ActionListener object on // the TextField object. myTextField.addActionListener( new MyActionListener(myTextField)); //Place the TextField object in a Frame object Frame myFrame = new Frame( "Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.add(myTextField); myFrame.setSize(250,150); 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 to listen for ActionListener events on the // TextField object class MyActionListener implements ActionListener{ TextField aTextField; MyActionListener(TextField inTextField){//constructor aTextField = inTextField;//save reference to TextField }//end constructor //Override the actionPerformed() method of the // ActionListener interface. public void actionPerformed(ActionEvent e){ System.out.println( "Selected text is: " + aTextField.getSelectedText()); System.out.println( "All text is: " + aTextField.getText()); }//end actionPerformed() }//end class MyActionListener //=======================================================// class Terminate extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing }//end class Terminate //=======================================================// |
"A TextArea object is a multi-line area that displays text. It can be set to allow editing or read-only modes." |
As of 3/13/97, the TextArea class has four fields, all of which are constants, and all of which have to do with the establishment of horizontal and vertical scrollbars for a TextArea object. A list of these symbolic constants will be provided later.
Also as of 3/13/97, the TextArea class has the following public constructors:
|
|
|
The program places a TextArea object in a Frame object. The TextArea object is specified to have a vertical Scrollbar.
An initial string consisting of ten separate lines of text is placed in the TextArea object when it is instantiated.
A TextListener object is instantiated and registered on the TextArea object. A TextEvent occurs whenever there is a change in the value of the text contained in the TextArea object.
The purpose of the TextListener object in this program is to display all of the text in the TextArea object whenever its value changes.
When the text value changes in the TextArea object, an event is trapped by the overridden textValueChanged() method of the TextListener object. Code in the overridden textValueChanged() method uses the getText() method of the TextComponent class to access and display all of the text in the TextArea object.
Note that this program is processing text at a very low level. An event occurs every time an individual character changes.
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.
The first interesting code fragment is the code used to instantiate the object of the TextArea class with a vertical scrollbar and then place ten separate lines of text in the object. Note that the append() method of the TextArea class is used to place the text in the object. Any of several other methods could also have been used for that purpose.
TextArea myTextArea = new TextArea("", 5, 20, TextArea.SCROLLBARS_VERTICAL_ONLY); for(int cnt = 0; cnt < 10; cnt++) myTextArea.append("Line " + cnt + ""); |
myTextArea.addTextListener(new MyTextListener(myTextArea)); |
public void textValueChanged(TextEvent e){ System.out.println(aTextArea.getText()); }//end textValueChanged() |
/*File TextArea01.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 The program places a TextArea object in a Frame object. The TextArea object is specified to have a vertical Scrollbar. An initial string consisting of 10 separate lines of text is placed in the TextArea object when it is instantiated. A TextListener object is instantiated and registered on the TextArea object. A TextEvent occurs whenever there is a change in the value of the text contained in the TextArea object. The purpose of the TextListener object is to display the text in the TextArea object whenever its value changes. When the text value changes in the TextArea object, an event is trapped by the overridden textValueChanged() method of the TextListener object. Code in the overridden textValueChanged() method uses the getText() method of the TextComponent class to access and display all of the text in the TextArea object. Note that this program is processing text at a very low level - every time a character changes. 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.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; //=======================================================// public class TextArea01 { public static void main(String[] args){ GUI gui = new GUI();//instantiate a GUI }//end main }//end class TextArea01 //=======================================================// class GUI { public GUI(){//constructor //Instantiate a TextArea object with a vertical // scrollbar and initialize the object with ten lines // of text. TextArea myTextArea = new TextArea( "", 5, 20, TextArea.SCROLLBARS_VERTICAL_ONLY); for(int cnt = 0; cnt < 10; cnt++) myTextArea.append("Line " + cnt + "\n"); //Instantiate and register a TextListener object on the // TextArea object. myTextArea.addTextListener( new MyTextListener(myTextArea)); //Place the TextArea object in a Frame object Frame myFrame = new Frame( "Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.add(myTextArea); myFrame.setSize(250,150); 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 to listen for TextListener events on the // TextArea object class MyTextListener implements TextListener{ TextArea aTextArea; MyTextListener(TextArea inTextArea){//constructor aTextArea = inTextArea;//save a reference }//end constructor //Override the textValueChanged() method of the // TextListener interface. public void textValueChanged(TextEvent e){ System.out.println(aTextArea.getText()); }//end textValueChanged() }//end class MyTextListener //=======================================================// class Terminate extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);//terminate the program }//end windowClosing }//end class Terminate //=======================================================// |
The Label class extends the Component class.
As of 3/13/97, the Label class has the following fields which are all constants. The names of the fields are self-explanatory.
|
|
As of 3/13/97, the Label class has six methods mostly involved with getting and setting text and dealing with alignment. For our purposes in this lesson, the most interesting subset of the available methods follows:
|
The program does not provide any event handling because events are not ordinarily generated by labels.
Remember however that since the Label class is a subclass of the Component class, any of the low-level event listeners defined in that class apply to Label objects also.
Therefore, Label objects can be the sources of low-level events, and event listener objects can be instantiated and registered on those Label objects.
You might want to go back and review some of the lessons on event handling in JDK 1.1 where it was shown that contrary to normal operation, objects of the Label class can generate low-level events such as mouse events, key events, and focus events.
The program places a Label object in a Frame object. The string "Initial Text" is placed in the Label object when it is instantiated.
The Label object behaves as a non-editable single line of text and doesn't generate events (at least there are no listener objects registered to listen for events).
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.
Label myLabel = new Label("Initial Text"); |
/*File label3.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 The program places a Label object in a Frame object. The string "Initial Text" is placed in the Label object when it is instantiated. The label object behaves as a read-only single line of text and doesn't generate events. 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.3 running under Win95. **********************************************************/ import java.awt.*; import java.awt.event.*; //=======================================================// public class label3 { public static void main(String[] args){ GUI gui = new GUI();//instantiate a GUI }//end main }//end class label3 //=======================================================// class GUI { public GUI(){//constructor //Instantiate a Label object with an initial text // string. Label myLabel = new Label("Initial Text"); //Place the Label object in a Frame object Frame myFrame = new Frame( "Copyright 1997, R.G.Baldwin"); myFrame.setLayout(new FlowLayout()); myFrame.add(myLabel); myFrame.setSize(250,150); 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 //=======================================================// |
It is this author's belief that by the time you reach this point in your study of Java programming, you should be working at a sufficiently independent level that such review information should no longer be necessary.
In particular, you should be working at a level that allows you to create your own review questions and programs, particularly tuned to the work that you are doing in Java.
Therefore, review questions and programs will rarely be provided for the remaining lessons in this set of Java programming tutorials.
-end-
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