Java GUI: AWT

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

124 εμφανίσεις

Java GUI: AWT

CIS 2615

Copyright © 2002
-
2008 Shah Mumin

All Rights Reserved

Part I

JFC: Java Foundation Classes

JFC is a collection of APIs for developing graphical components in Java. It
includes the following:


AWT (version 1.1 and beyond)


2D API


Swing Components


Accessibility API


AWT was original toolkit for developing graphical components. The JFC is
based on the AWT components.


With the introduction to Swing, lightweight version of many of the AWT
components (heavyweight) are created with the prefix J. For example,
JFrame for Frame, JButton for Button, etc.

JFC: Java Foundation Classes

Since Swing was an extension to AWT, all Swing components are organized
in javax.swing package compared to java.awt for all AWT components.


Swing also provides many components that AWT lacked.


Some of the original method names have been changed to allow uniform
naming for all components.


When a method name will no longer be supported in future newer versions,
it is said to be
deprecated
.

AWT: Components

The
java.awt.
Component

is one of the cornerstones of AWT programming.
It contains approximately half of the classes in AWT. AWT is built on the
Component

class.


Component

class


an abstract class for GUI components such as buttons,
menus, labels, lists, etc.


Container



An abstract class that extends
Component
. Classes derived
from the Container class can contain multiple components. Examples are
Panel, Applet, Window, Frame, etc.


LayoutManager



An interface for positing and sizing
Container
objects.
Java defines several default implementations. You can also create your
custom layout.


Graphics

class


An abstract class that defines methods for performing
graphical operations. Every component has an associated
Graphics
object.

AWT: Components Graphic

AWT: Component class

java.awt.Component

subclasses (in java.awt package):

Label
Displays text in a box
Button
A clickable button, can generate event
Canvas
Area for painting graphics
Checkbox
A button that provides on/off toggle values. Can
be used for both Check box and radio buttons
(when contained in a CheckBoxGroup)
Choice
Combo box where a list of items can be displayed
by clicking the button
List
A component that displays a list of selectable items
Scrollbar
A scrollable bar
TextComponent
Superclass for TextField and TextArea
TextArea
Multiple line text input box
TextField
One line user input text box
AWT: Container Class

java.awt.Container

subclasses (in java.awt package):

Applet
Superclass of all applets, an extension of Panel
Dialog
Can be modal or non-modal. Extends Window
FileDialog
Opens a regular file dialog
Frame
All applications are contained in a Frame. Extends
Window. It can have a menubar unlike an applet
Panel
A simple container of other components including Panels
Window
It is rarely used directly, but useful for spash screen
when an app starts. No menu or border. Parent of
Frame and Dialog
LayoutManager impl

java.awt.LayoutManager

interface implementations:

BorderLayout
Compoents are layed out in North/South,
East/West and Center
CardLayout
Deck of panels where panels are displayed one at a
time
FlowLayout
Component flow from left to right, top to bottom.
When there is no real estate to maneuvar on the
right, goes down. Widely used.
GridBagLayout
Each componet location is defined using grid.
Most difficult to implement.
GridLayout
Components are layed out in a grid. Component is
streched to fill the grid.
You can define your custom layout by extending
LayoutManager interface
AWT: Detail Example

Let’s look at all of the AWT components in a regular AWT applet (run the
ComponentApplet).


This example is taken from David Geary and Alan McCleillan’s Graphic
Java: mastering AWT.


Since Swing provides replacements of many of the AWT components, we
will keep out very limited to these components.


Instead, we will learn the Swing equivalent in next chapter.

AWT: Detail Example

Run
ComponentApplet.cmd

AWT: java.awt.Frame

The java.awt.
Frame

is container of all AWT applications. The Swing
provides the J replacement called JFrame.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

FrameTester
extends

Frame

{


public

FrameTester() {


//anonymous inner class inside the constructor to close the frame when someone


//clicks the x in the top right corner


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


}


public static void

main(
String
[] args) {


FrameTester

frame =
new

FrameTester
();


frame.setSize(
new

Dimension
(
300
,
200
));
//providing a Dimension object


frame.setSize(
250
,
250
);
//providing x and y coordinates


frame.setBackground(
Color
.white);
//default is gray


frame.setTitle(
"Test Frame"
);


frame.show();


}

}
//end of class

AWT: java.awt.Frame

Run
FrameTester.cmd

AWT: java.awt.Panel

A Panel is a Container object that can contain Components (Button,
List, etc) and Containers including other Panels.


It is contained in another container such as a Frame. Since an Applet
is an extension of the Panel class, we can directly add to it.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

PanelTester
extends

Frame

{


private

Panel

myPanel =
new

Panel
();


public

PanelTester() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}



//private components initialization


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


this
.add(myPanel);


}



public static void

main(
String
[] args) {


PanelTester

frame =
new

PanelTester
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class

AWT: java.awt.Panel

Run
PanelTester.cmd

java.awt.Canvas

The Canvas class is used to draw text and shapes on the screen. Following
is the simplest example of using Canvas.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

CanvasTester
extends

Frame

{



private

Panel

myPanel =
new

Panel
();


private

Canvas

myCanvas =
new

Canvas
();



public

CanvasTester() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});



try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}
//end of constructor


private void

jbInit()
throws

Exception

{


this
.setSize(
350
,
250
);
//initial size


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup canvas info


myCanvas.setBackground(
Color
.red);


myCanvas.setSize(
150
,
150
);


//add things to the Panel before adding to the frame


//if you dynamically add content to the


//panel after the panel is loaded


myPanel.add(myCanvas);


//call the Panel’s doLayout() method


//you can also directly add to the frame


this
.add(myPanel);


}



public static void

main(
String
[] args) {


CanvasTester

frame =
new

CanvasTester
();


frame.setSize(
new

Dimension
(
250
,
250
));


//can override size


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class

java.awt.Canvas

Run
CanvasTester.cmd

Canvas : Override

Although you can create an instance of Canvas and draw into it, it is
common to extend the Canvas and override the paint() method.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

CanvasTesterCanavsOverride
extends

Frame

{



private

Panel

myPanel =
new

Panel
();


private

Canvas

myCanvas =
new

ExampleCanvas
();



public

CanvasTesterCanavsOverride () {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});



try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}
//end of constructor

Canvas : Override

private void

jbInit()
throws

Exception

{


this
.setSize(
350
,
250
);
//initial size


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup canvas info


myCanvas.setBackground(
Color
.red);
//changing color


myCanvas.setSize(
150
,
150
);
//explicitly resizing


//add things to the Panel before adding to the frame


//if you dynamically add content to the panel after the panel is loaded


myPanel.add(myCanvas);


this
.add(myPanel);


}


class

ExampleCanvas
extends

Canvas

{


public void

paint(
Graphics

g) {


g.drawString(
"This is a test Canvas"
,
20
,
20
);
//coordinates are relative to the canvas


}


//override the <CODE>getPreferedSize()</CODE> method


//otherwise, the Canvas gets size(0,0) and might not show up


//at all depending on the layout manager.


//FlowLayout class calls this method to size the Canvas


public

Dimension

getPreferedSize() {


return new

Dimension
(150,150);
//initial size


}


}


public static void

main(
String
[] args) {


CanvasTesterCanavsOverride

frame =
new

CanvasTesterCanavsOverride
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
“Panel Tester”
);


frame.show();


}

}
//end of class

Canvas : Override

Run
CanvasTesterCanavsOverride.cmd

AWT : java.awt.Label

The Label component is used to display text in a rectangular region. A label
cannot receive focus, however can receive events such as MouseEvent.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

LabelTester
extends

Frame

{



private

Panel

myPanel =
new

Panel
();


private

Label

label1 =
new

Label
(
"Left"
);


private

Label

label2 =
new

Label
(
"Center"
);


private

Label

label3 =
new

Label
(
"Right"
);



public

LabelTester() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


try

{


jbInit();


}


catch

(
Exception

ex) {


ex.printStackTrace();


}


}

AWT : java.awt.Label


private void

jbInit()
throws

Exception

{


myPanel.setLayout(new
FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//define charactersictics of labels


label1.setAlignment(
Label
.LEFT);


label1.setBackground(
Color
.darkGray);


label1.setText(
"In the Left"
);


label2.setAlignment(
Label
.CENTER);


label2.setBackground(
Color
.yellow);


label2.setText(
"In the Center"
);


label3.setAlignment(
Label
.RIGHT);


label3.setBackground(
Color
.green);


label3.setText(
"In the Right"
);


//can add MouseListener to a label


label1.addMouseListener(new
MouseAdapter
() {


public void

mousePressed(
MouseEvent

e) {


label1.setBackground(
Color
.white);


}


public void

mouseReleased(
MouseEvent

e) {


label1.setBackground(
Color
.cyan);


}


public void

mouseClicked(
MouseEvent

e) {


label1.setBackground(
Color
.red);


}


});




//add labels to the panel


myPanel.add(label1);


myPanel.add(label2);


myPanel.add(label3);



this
.add(myPanel);


}



public static void

main(
String
[] args) {


LabelTester

frame =
new

LabelTester
();


frame.setSize(new Dimension(
250
,
250
));


frame.setTitle(
"Test Frame"
);


frame.show();


}

}
//end of class

AWT : java.awt.Label

Run
LabelTester.cmd

AWT : java.awt.Button

Buttons are used to generate some action. For example, you allow your user
to submit a web page by clicking the submit button of a form.
Java.awt.Button has the same use in Java.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

ButtonTester
extends

Frame

{



private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private
Button

testButton =
new

Button
();



public

ButtonTester() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons


exitButton.setSize(
100
,
25
);


exitButton.setLabel(
"Exit"
);


testButton.setSize(
100
,
25
);


testButton.setLabel(
"Tester"
);


testButton.setEnabled(
false
);
//disable, gray out


this
.myPanel.add(exitButton);
//optional keyword this


myPanel.add(testButton);


//flows one after another in FlowLayout


//you must add the button in the order you want to see


this
.add(myPanel);


}



public static void

main(
String
[] args) {


ButtonTester

frame =
new

ButtonTester
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class


AWT : java.awt.Button

Run
ButtonTester.cmd

java.awt.event.ActionEvent

Now, we can add an action behind the button so that we can capture the
button click that generates an
ActionEvent
.


In order to capture the click, you can implement the
ActionListener

interface which defines the
actionPerformed(ActionEvent)

method. You can
capture the events in a few different ways. The easiest way is to:




Implement the ActionListener interface



Register the button with the listener



In the actionPerformed() method (must be defined as part of
ActionListener interface implementation), see if your button was clicked

java.awt.event.ActionEvent

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

ButtonTesterAction
extends

Frame



implements

ActionListener

{



private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private
Button

testButton =
new

Button
();



public

ButtonTesterAction() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});



try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}



private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons


exitButton.setSize(
100
,
25
);


exitButton.setLabel(
"Exit"
);





testButton.setSize(
100
,
25
);


testButton.setLabel(
"Tester"
);


//must register the button to the ActionEvent


exitButton.addActionListener(
this
);


testButton.addActionListener(
this
);


//we can pass this to the listener since this class implements


//ActionListener


this
.myPanel.add(exitButton);


myPanel.add(testButton);


this
.add(myPanel);


}



public void

actionPerformed(
ActionEvent

evt) {


String

arg = evt.getActionCommand();


if
(arg.equals(
"Exit"
)) {//must exactly match the String


System
.exit(
0
);


}



//alternative way to use the name of your button to minimize


//dependancy on the text of the button


if
(evt.getSource() == testButton) {


System
.exit(0);


}


}



public static void

main(
String
[] args) {


ButtonTesterAction
frame =
new

ButtonTesterAction
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class

java.awt.event.ActionEvent

Run
ButtonTesterAction.cmd

ActionEvent: Extending

You can create a custom inner class to handle the button click. Sometimes, it
might be useful when you want to do complex action after a button click

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

ButtonTesterCustomAction
extends

Frame

{



private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private
Button

testButton =
new

Button
();



public

ButtonTesterCustomAction() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});



try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}
//end of constructor

ActionEvent: Extending


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons


exitButton.setSize(
100
,
25
);


exitButton.setLabel(
"Exit"
);


testButton.setSize(
100
,
25
);


testButton.setLabel(
"Tester"
);


//must register the button to the ActionEvent


exitButton.addActionListener(
new

ExitActionAdapter
(
this
));


testButton.addActionListener(
new

TestActionAdapter
(
this
));


//we are passing a reference to this class so that the called


//class can call us back with action event


this
.myPanel.add(exitButton);


myPanel.add(testButton);


this
.add(myPanel);


}



public void

exitButton_actionPerformed(
ActionEvent

e) {


System
.exit(
0
);


}



public void

testButton_actionPerformed(
ActionEvent

e) {


System
.exit(
0
);


}






public static void

main(
String
[] args) {


ButtonTesterCustomAction
frame =
new

ButtonTesterCustomAction
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class


class

ExitActionAdapter
implements

ActionListener
{


private

ButtonTesterCustomAction

caller;


public

ExitActionAdapter(
ButtonTesterCustomAction

caller) {


this
.caller = caller;


}


public void

actionPerformed(
ActionEvent

e) {


caller.exitButton_actionPerformed(e);


}

}


class

TestActionAdapter
implements

ActionListener
{


private

ButtonTesterCustomAction

caller;


public

TestActionAdapter(
ButtonTesterCustomAction

caller)
{


this
.caller = caller;


}


public void

actionPerformed(
ActionEvent

e) {


caller.testButton_actionPerformed(e);


}

}

ActionEvent: Extending

Run
ButtonTesterCustomAction.cmd

ActionEvent: Extending (Inner Class)

If you create the custom action class inside your class itself, you can directly
refer to it any outer class methods.

Unlike the previous example, you do not have to pass an instance of the
original class to call back the caller.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

ButtonTesteCustomActionInside
extends

Frame

{


private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private
Button

testButton =
new

Button
();


public

ButtonTesterCustomActionInside() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}
//end of constructor

ActionEvent: Extending (Inner Class)


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons


exitButton.setSize(
100
,
25
);


exitButton.setLabel(
"Exit"
);


testButton.setSize(
100
,
25
);


testButton.setLabel(
"Tester"
);


//must register the button to the ActionEvent


exitButton.addActionListener(
new

ExitActionAdapter
());


testButton.addActionListener(
new

TestActionAdapter
());


//we are passing a reference to this class so that the called


//class can call us back with action event


this
.myPanel.add(exitButton);


myPanel.add(testButton);


this
.add(myPanel);


}



public void

exitButton_actionPerformed(
ActionEvent

e) {


System
.exit(
0
);


}



public void

testButton_actionPerformed(
ActionEvent

e) {


System
.exit(
0
);


}






class

ExitActionAdapter
implements

ActionListener
{


public void

actionPerformed(
ActionEvent

e) {


exitButton_actionPerformed(e);


}


}



class

TestActionAdapter
implements

ActionListener
{


public void

actionPerformed(
ActionEvent

e) {


testButton_actionPerformed(e);


}


}



public static void

main(
String
[] args) {


ButtonTesterCustomActionInside
frame =
new

ButtonTesterCustomActionInside
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class


ActionEvent: Extending (Inner Class)

Run
ButtonTesterCustomAction.cmd

ActionEvent: Anonymous Inner Class

You can create an anonymous inner class with your button to handle the
button click for your specific button:

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

ButtonTesterAnonAction
extends

Frame

{



private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private
Button

testButton =
new

Button
();



public

ButtonTesterAnonAction() {


this
.addWindowListener(new
WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});



try

{


jbInit();


}


catch

(
Exception
ex) {


ex.printStackTrace();


}


}
//end of constructor

ActionEvent: Anonymous Inner Class


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons


exitButton.setSize(
100
,
25
);


exitButton.setLabel(
"Exit"
);


testButton.setSize(
100
,
25
);


testButton.setLabel(
"Tester"
);


exitButton.addActionListener(
new

ActionListener
() {


public void

actionPerformed(
ActionEvent

e) {


System
.exit(0);


}


});


testButton.addActionListener(
new

ActionListener
() {


public void

actionPerformed(
ActionEvent

e) {


System
.exit(0);


}


});



this
.myPanel.add(exitButton);


myPanel.add(testButton);


this
.add(myPanel);


}


public static void

main(
String
[] args) {


ButtonTesterAction
frame =
new

ButtonTesterAction
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Panel Tester"
);


frame.show();


}

}
//end of class

ActionEvent: Anonymous Inner Class

Run
ButtonTesterAnonAction.cmd

AWT: java.awt.TextComponent

AWT provides two components for displaying editable text: TextField
(single line) and TextArea (multiple lines).


Both of the components extend java.awt.TextComponent class.


Text components, by default, accept focus, and as a result fire key and focus
events in addition to the events fired by all components.

AWT: java.awt.TextField

TextField is a Component that provides single line of text. Many of the
events can be done both in TextField and TextArea objects.

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

TextFieldTester
extends

Frame


implements

ActionListener
,
TextListener

{


private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private

Button

testButton =
new

Button
();


private

TextField

tf =
new

TextField
(
”enter value"
);
//can pass String


private

TextField

tf2 =
new

TextField
(
10
);
//number of columns



public

TextFieldTester() {


this
.addWindowListener(
new

WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


try

{


jbInit();


}


catch

(
Exception

ex) {


ex.printStackTrace();


}


}
//end of constructor

AWT: java.awt.TextField


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons


testButton.setSize(
100
,
25
);


testButton.setLabel("Test Text");


exitButton.setSize(
100
,
25
);


exitButton.setLabel(
"Exit"
);


exitButton.addActionListener(
this
);


testButton.addActionListener(
this
);


this
.myPanel.add(testButton);


this
.myPanel.add(exitButton);


this
.add(myPanel);



//add text field


tf.setText(
"012345678901234567890"
);
//setting text in a field


//when selecting, start counting at zero. The selection range includes


//lower end of the range, but no upper.


//If there is no selection, both getSelectionStart() and


//getSelectionEnd() returns caret.


tf.select(
3
,
7
);
//select portion of the text


tf.addTextListener(
this
);


//when use press enter key, you can pick it up


tf.addActionListener(
new
ActionListener
() {


public void

actionPerformed(ActionEvent e) {


System
.out.println("User pressed enter key, process data...");


}


});


tf.addFocusListener(
new

FocusAdapter
() {


public void

focusLost(
FocusEvent

e) {


System
.out.println("Focus is lost from the text field....");


}


});


this
.add(tf);


tf2.setEchoChar(
'*'
);


AWT: java.awt.TextField


//catch in key stroke. It will allow to track what the user is


//entering. For example, we can consume key stroke when the


//data is not numeric in a numeric field


tf2.addKeyListener(
new

KeyAdapter
() {


public void

keyPressed(
KeyEvent

e) {


System
.out.println(
"Key stroke detected...."
);


}


});


this
.add(tf2);


}


//addTextListener implementation


public void

textValueChanged(
TextEvent

e) {


System
.out.println(
"Text value changed...."
);


}


public void

actionPerformed(
ActionEvent

evt) {


String

arg = evt.getActionCommand();


if
(arg.equals(
"Exit"
)) {


System
.exit(
0
);


}


else if
(arg.equals(
"Test Text"
)) {


int

start = tf.getSelectionStart();


int
end = tf.getSelectionEnd();


String

selectedText = tf.getSelectedText();
//get selected text


System
.out.println(
"start="

+ start +
" end="

+ end);


System
.out.println(
"selectedText="

+ selectedText);


System
.out.println(
"Text in tf2="

+ tf2.getText());
//getText() to get text of TextField


}


}


public static void

main(
String
[] args) {


TextFieldTester

frame =
new

TextFieldTester
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Test Frame"
);


frame.show();


}

}
//end of class

AWT: java.awt.TextField

Run
TextFieldTester.cmd

AWT: java.awt.TextArea

TextArea provides multiple line text entry box unlike TextField where
single line entry is allowed.


You can also define vertical and/or horizontal scrollbars with your text
area.


All of the events that can be attached to a TextField can also be attached to a
TextArea except the ActionEvent.

AWT: java.awt.TextArea

package

com.javaclass.awt;


import

java.awt.*;

import

java.awt.event.*;


public class

TextAreaTester
extends

Frame


implements

ActionListener
,
TextListener

{



private

Panel

myPanel =
new

Panel
();


private

Button

exitButton =
new

Button
();


private

Button

testButton =
new

Button
();


private

TextArea

ta;



public

TextAreaTester() {


this
.addWindowListener(
new

WindowAdapter
() {


public void

windowClosing(
WindowEvent

e) {


System
.exit(
0
);


}


});


try

{


jbInit();


}


catch

(
Exception

ex) {


ex.printStackTrace();


}


}


private void

jbInit()
throws

Exception

{


myPanel.setLayout(
new

FlowLayout
());


myPanel.setBackground(
Color
.cyan);


//setup buttons

AWT: java.awt.TextArea


testButton.setSize(
100
,
25
);


testButton.setLabel(
"Test Text"
);


exitButton.setSize(
100
,
25
);


exitButton.setLabel("Exit");


exitButton.addActionListener(
this
);


testButton.addActionListener(
this
);


this
.myPanel.add(testButton);


this
.myPanel.add(exitButton);


this
.add(
"Center"
, myPanel);



//you can also just show vertical, horizontal or no scrollbar


//””: no initial text


//10: number of rows


//30: number of columns


ta =
new

TextArea
(
""
,
10
,
30
,
TextArea
.SCROLLBARS_BOTH);


//use setText() to update a TextArea


//you can make it read
-
only by setting setEditable(false)


//can use append() to append text to the end


ta.setText(
"So the songbirds left
\
n"

+


" no void
\
n"

+


" no empty hours
\
n"

+


" when they fled
\
n"

+


" because the hour itself
\
n"

+


" had died before them.
\
n"

+


" Morning no longer existed.
\
n
\
n"

+


"
--
Chinua Achebe: The Anthills of Savannah "
);


this
.add(ta);


ta.addTextListener(
this
);

AWT: java.awt.TextArea


ta.addFocusListener(
new

FocusAdapter
() {


public void

focusLost(
FocusEvent

e) {


System
.out.println("Focus is lost from the text field....");


}


});


ta.addKeyListener(
new

KeyAdapter
() {


public void

keyPressed(
KeyEvent

e) {


System
.out.println(
"Key stroke detected...."
);


}


});


}


//addTextListener implementation


public void

textValueChanged(
TextEvent

e) {


System
.out.println(
"Text value changed...."
);


}


public void

actionPerformed(
ActionEvent

evt) {


String

arg = evt.getActionCommand();


if
(arg.equals(
"Exit"
)) {


System
.exit(
0
);


}


else if
(arg.equals(
"Test Text"
)) {


System
.out.println(
"Test Text button clicked…”
);


}


}


public static void

main(
String
[] args) {


TextAreaTester

frame =
new

TextAreaTester
();


frame.setSize(
new

Dimension
(
250
,
250
));


frame.setTitle(
"Test Frame"
);


frame.show();


}

}
//end of class

AWT: java.awt.TextArea

Run
TextAreaTester.cmd