Professional Documents
Culture Documents
AWT Components: Button (Java - Awt.button)
AWT Components: Button (Java - Awt.button)
AWT Components
A component is an object with a graphical representation that can be displayed on the screen
and that can interact with the user. The Component class is the abstract parent of the
nonmenu-related AWT components.
Button (java.awt.Button)
To create a Button object, simply create an instance of the Button class by calling one of
the constructors. The most commonly used constructor of the Button class takes a
String argument, that gives the Button object a text title. The two constructors are:
When a user presses on a Button object an event is generated. The label of the button that
is pressed can be obtained.
Checkboxes (java.awt.Checkbox)
Checkboxes ave two states, on and off. The state of the button is returned as the Object
argument, when a Checkbox event occurs. To find out the state of a checkbox object we
can use getState() that returns a true or false value. We can also get the label of the
checkbox using getLabel() that returns a String object.
Is a group of checkboxes, where only one of the items in the group can be selected at any one
time.
Labels (java.awt.Label)
TextFields (java.awt.TextField)
Are areas where the user can enter text. They are useful for displaying and receiving text
messages. We can make this textfield read-only or editable. We can use the
setEditable(false) to set a textfield read-only. There are numerous ways that we can
construct a Textfield object:
To show these components in action we can write a short piece of code that displays these
components, but they do not have any events behind them - they work visually but do not
have any true program function. Figure 9.8, “A Component Applet” shows an example applet
that details all the previous components.
1
2
3 // The Component Applet that displays several components
4
5 import java.applet.Applet;
6 import java.awt.*;
7
8 public class ComponentApplet extends Applet
9 {
10 public void init()
11 {
12 Button b = new Button("Test Button");
13 this.add(b);
14
15 Checkbox cb = new Checkbox("Test Checkbox");
16 this.add(cb);
17
18 CheckboxGroup cbg = new CheckboxGroup();
19 this.add(new Checkbox("CB Item 1", cbg, false));
20 this.add(new Checkbox("CB Item 2", cbg, false));
21 this.add(new Checkbox("CB Item 3", cbg, true));
22
23 Choice choice = new Choice();
24 choice.addItem("Choice Item 1");
25 choice.addItem("Choice Item 2");
26 choice.addItem("Choice Item 3");
27 this.add(choice);
28
29 Label l = new Label("Test Label");
30 this.add(l);
31
32 TextField t = new TextField("Test TextField",30);
33 this.add(t);
34 }
35 }
36
37
Layout Policies
Chapter 9. Java™ Graphical User Interface(GUI)
Components
Layout Policies
If we want to position textboxes, buttons and checkboxes on a window like in Figure 9.1,
“Example GUI: Font Dialog”, we need a way to organize these components in the window. A
layout policy allows us a way to describe the order that we want to display these components,
allowing a structure for placing these components on the applet. In the last example we used
a FlowLayout, as it is the default layout, that just adds the components to the applet in the
order in which you use the add() method.
It is possible to change the layout of the components using a layout manager, the most
common layouts are:
FlowLayout
GridLayout
BorderLayout
FlowLayout (java.awt.FlowLayout)
You can see this applet running here - FlowLayoutApplet.html and the code segment is
here - FlowLayoutApplet.java
1
2
3 import java.awt.*;
4 import java.applet.Applet;
5
6 public class FlowLayoutApplet extends Applet
7 {
8 Button button1, button2, button3, button4, button5;
9
10 public void init()
11 {
12 button1 = new Button("Button 1");
13 button2 = new Button("Button 2");
14 button3 = new Button("Button 3");
15 button4 = new Button("Button 4");
16 button5 = new Button("Button 5");
17 add(button1);
18 add(button2);
19 add(button3);
20 add(button4);
21 add(button5);
22 }
23 }
24
25
GridLayout (java.awt.GridLayout)
A GridLayout lays out the components in a rectangular grid. The container that you apply
the layout to, divides the area into equal sized squares and each component that you add is
sized to fit that square. If you look at Figure 9.10, “A Grid Layout Applet” you will notice
that the buttons have been scaled larger to fit the grid area exactly. This can be difficult to get
used to.
You must specify the number of rows and columns in the grid. So to construct a 3x2 grid as
in Figure 9.10, “A Grid Layout Applet” I used new GridLayout(3,2) to construct the 3x2
GridLayout object. If you place a 0 for either the row or the column value to the constructor
then you will have unlimited rows or columns. For example, new GridLayout(0,2) will
construct a GridLayout object with 2 columns, but an unlimited number of rows.
The order that you use the add() method is still important as it adds the components to the
grid from left-to-right top-to-bottom.
1
2
3 import java.awt.*;
4 import java.applet.Applet;
5
6 public class GridLayoutApplet extends Applet
7 {
8 Button button1, button2, button3, button4, button5;
9
10 public void init()
11 {
12 this.setLayout(new GridLayout(3,2));
13 button1 = new Button("Button 1");
14 button2 = new Button("Button 2");
15 button3 = new Button("Button 3");
16 button4 = new Button("Button 4");
17 button5 = new Button("Button 5");
18 add(button1);
19 add(button2);
20 add(button3);
21 add(button4);
22 add(button5);
23 }
24 }
25
26
BorderLayout (java.awt.BorderLayout)
A BorderLayout arranges a container into five regions, North, South, East, West and
Center. Each region can have either one or zero components. If you do not place a component
in the region then it will not be displayed. When we are adding components to a container we
could write something like:
When you add a component to the BorderLayout the components are added according to
the components preferred size and the BorderLayout's preferred size. For Example as in
Figure 9.11, “A Border Layout Applet”, a button added to the North will have the preferred
height of the Button object, but it will have the preferred width of the BorderLayout
container area. If you look at Figure 9.11, “A Border Layout Applet” you will also notice that
the "Button 3" object has its preferred width of the Button object, but it has the preferred
height of the "West" region of the BorderLayout container area.
Figure 9.11. A Border Layout Applet
You can see this applet running here - BorderLayoutApplet.html and the code segment is
here - BorderLayout.java
1
2
3 import java.awt.*;
4 import java.applet.Applet;
5
6 public class BorderLayoutApplet extends Applet
7 {
8 Button button1, button2, button3, button4, button5;
9
10 public void init()
11 {
12 this.setLayout(new BorderLayout());
13 button1 = new Button("Button 1");
14 button2 = new Button("Button 2");
15 button3 = new Button("Button 3");
16 button4 = new Button("Button 4");
17 button5 = new Button("Button 5");
18 add("North",button1);
19 add("South",button2);
20 add("East",button3);
21 add("West",button4);
22 add("Center",button5);
23 }
24 }
25
26
There is one other Layout Manager that is commonly used - the GridBagLayout. It allows
components to be aligned vertically and horizonatally without requiring the components to be
the same size. You can see more information on this Layout Manager in the Java API
documentation under java.awt.GridBagLayout.
//or
A Layout Exercise
Chapter 9. Java™ Graphical User Interface(GUI)
Components
A Layout Exercise
Task: Write the Graphical User Interface for the applet as shown in Figure 9.12, “Layout
Exercise GUI”
Solution: Do not look at the solution until you have had a good attempt at it yourself.
LayoutExercise.java
List (java.awt.List)
List components objects are useful for holding a large number of data items. They are
quite similar to the Choice component, however they can allow for the multiple selection
of items. The list automatically adds a scrollbar if it is required. The list constructor takes two
arguments, the int number of items to be displayed and a boolean to allow/disallow
multiple item selection.
1
2
3 List aList = new List(3, true);
4 aList.addItem("Some Item 1");
5 aList.addItem("Some Item 2");
6 aList.addItem("Some Item 3");
7 aList.addItem("Some Item 4");
8 //etc.
9
10
Clicking on an item selects it and pressing again on the same item deselects it. The selection
works the same way as Windows explorer file selection. i.e. if you hold the SHIFT key and
select with the mouse you can select from one item in The getSelectedItem() (or
getSelectedItems() for multiple selections) method can be used to retrieve the currently
selected item.
TextArea (java.awt.TextArea)
Are used for creating areas of screen input where we want to provide large amounts of text
data, either for displaying a text file, or implementing a text editor. TextArea components
do not generate events. They can be set read-only or editable by using the setEditable()
method.
Scrollbar (java.awt.Scrollbar)
Scrollbar objects are used to develop sliders that choose values in a certain range of data.
We can use a Scrollbar to control an input value, scroll an image, and numerous other
applications. The constructor of a Scrollbar takes five parameters:
Use the getValue() method to read the current value of the Scrollbar object. Use the
setValue() method to set the slider position in the scrollbar.
At the moment we have displayed components, adding them to applications, and they work to
the extent that you can press the buttons, select from the list etc., however they do not
currently interact with your own code. If we want to use these components we must be aware
of how the java.awt.event package classes work. Note that with events the structure is
quite different from Java 1.1.x (Java 1) to Java 1.2+.x (Java 2). There are many reasons for
this, but the main reason is that there were serious problems with the event structure of Java
1, especially in relation to threaded applications. We are only examining the event structure
of Java 2+.
The event structure for the components we have just discussed is very similar to the event
structure that we discussed for the Mouse using the MouseListener interface in the section
called “Using the Mouse in Applets”.
ActionEvent (java.awt.event.ActionEvent)
Where this refers to an object that is capabable of handling the ActionEvent object that is
passed to it. So in this case the class in which the Button b is defined must implement that
ActionListener interface. So for a full example, use the following application:
1
2 // A Button Events Application
3
4 import java.applet.*;
5 import java.awt.*;
6 import java.awt.event.*;
7
8 public class ButtonEvents extends Applet implements ActionListener
9 {
10
11 private Button button1, button2, button3;
12 private TextField status;
13
14 public void init()
15 {
16 status = new TextField(20);
17
18 this.button1 = new Button("Button 1");
19 this.button2 = new Button("Button 2");
20 this.button3 = new Button("Button 3");
21
22 this.button1.addActionListener(this);
23 this.button2.addActionListener(this);
24 this.button3.addActionListener(this);
25
26 this.add(status);
27 this.add(button1);
28 this.add(button2);
29 this.add(button3);
30 }
31
32 public void actionPerformed(ActionEvent e)
33 {
34 if (e.getActionCommand().equals("Button 1"))
35 {
36 status.setText("Button 1 Pressed");
37 }
38 else if (e.getActionCommand().equals("Button 2"))
39 {
40 status.setText("Button 2 Pressed");
41 }
42 else
43 {
44 status.setText("Button 3 Pressed");
45 }
46 }
47 }
48
49
The java.awt.event package must be included to allow for the use of the
ActionEvent and associated classes.
The applet must implement ActionListener to handle ActionEvents. Once our
class uses the statement implements ActionListener then we must write a method
public void actionPerformed(ActionEvent e). If we do not write this method
then the code will not compile.
We must use the method addActionListener() to the component to state that this
component generates events and to direct these events to a suitable object. In this
applet we use the keyword this which represents this object of the applet. So we are
sending the event to this object. We are only allowed to do that because this object
implements ActionListener. If it did not then an error would be generated at
compile time.
Once the event is sent to this object then the actionPerformed() method is called
and it is passed an ActionEvent object - in this case called e. We can then
interrogate this ActionEvent object to determine its source. In this applet we have
three different buttons that could have caused this object to be generated. One way of
interrogating the ActionEvent object is to use its getActionCommand() method
that returns the action command as a String object. In the API documentation it
states that the action command for a Button object is the text on the surface of the
button by default. We then use the equals() method of the String class that
allows us to compare the action command to a String such as "Button 1". If the
"Button 1" String is a perfect match then we set the text of the TextField to
be "Button 1 Pressed" using the setText() method of the TextField object
status.
The ActionEvent class and the ActionListener interface mechanism can be used to
deal with events on buttons and on other components also. The ActionEvent class deals
with actions that can occur. Other events can be created by other components - how do we
know which events are created by which components? Well you have to read the API
documentation for that component. For the components we have looked at in this section:
Figure 9.17. Component Events
Button objects generate an ActionEvent object when the mouse is pressed on the
Button object. You can add a listener to the object by using the
addActionListener() method that requires a listener object as a parameter. The
Listener object must implement the ActionListener interface that requires an
actionPerformed() method to be written.
Checkbox objects generates an ItemEvent object. You can add a listener to the
object using the addItemListener() method that requires a listener object as a
parameter. The listener must implement the ItemListener interface and is
therefore required to write an itemStateChanged() method.
A CheckboxGroup object is really a group of Checkbox objects and does not
generate any events as a group, rather the individual objects generate the events as for
Checkbox objects. If you need to find out the individual Checkbox object that was
chosen use the getSelectedCheckbox() method.
Choice objects generates an ItemEvent object. You can add a listener to the
object using the addItemListener() method that requires a listener object as a
parameter. The listener must implement the ItemListener interface and is
therefore required to write an itemStateChanged() method.
Label objects do not generate events.
TextField objects generate KeyEvent objects when a key is pressed in the box.
This KeyEvent object can be a key pressed, a key released or a key typed. You can
use the addKeyListener() to add a listener to the TextField object. This
method requires an object that has implemented the KeyListener interface that
requires the object to implement the keyPressed(), keyReleased() and
keyTyped() methods. A TextField object can also generate an ActionEvent
object when the enter key is pressed in the text area. This ActionEvent has the
same format as for the Button component above.
List objects generate an ItemEvent object when items are selected or deslected
in the list. You can add a listener to the object using the addItemListener()
method that requires a listener object as a parameter. The listener must implement the
ItemListener interface and is therefore required to write an
itemStateChanged() method. The List object also generates an ActionEvent
object if the mouse is double-clicked or enter is pressed on an item in the list.
TextArea objects do not generate events.
Scrollbar objects generate an AdjustmentEvent object when the Scrollbar's
bubble is moved. You can add a listener to the Scrollbar object by using the
addAdjustmentListener() method that requires an object that implements the
AdjustmentListener interface requiring the adjustmentValueChanged() method
to be written.
The Component class can generate many event types. The Component is the parent of all
visible AWT components, providing the template for all AWT components. It can have:
The TextComponent class can generate TextEvent objects. It has the Component class
as its parent and so can generate the same events as above for Component. A TextEvent
is generated when the object's text is changed.
A Scrollbar Example
Task: Write the application as shown in Figure 9.18, “A Scrollbar Example Applet” where
the Scrollbar component updates the text in the TextField component when it is
moved. The Reset Button component should set the value of the TextField object to 0.
The Scrollbar component should have the range 0 to 100.
Solution: You can see this Scrollbar example applet running here - ScrollbarExample.html
and the code segment is here - ScrollbarExample.java
1
2 // A Scrollbar Event Applet
3
4 import java.applet.*;
5 import java.awt.*;
6 import java.awt.event.*;
7
8 public class ScrollbarExample extends Applet implements
AdjustmentListener,
9 ActionListener
10 {
11
12 private Scrollbar scrollbar;
13 private TextField status;
14 private Button resetButton;
15
16 public void init()
17 {
18 this.setLayout(new GridLayout(2,1));
19 Panel topPanel = new Panel();
20
21 this.status = new TextField(20);
22 this.scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10,
23 0, 110);
24 this.resetButton = new Button("Reset");
25
26 this.resetButton.addActionListener(this);
27 this.scrollbar.addAdjustmentListener(this);
28
29 topPanel.add(status);
30 topPanel.add(resetButton);
31
32 this.add(topPanel);
33 this.add(scrollbar);
34
35 this.updateText();
36 }
37
38 public void actionPerformed(ActionEvent e)
39 {
40 if (e.getActionCommand().equals("Reset"))
41 {
42 this.scrollbar.setValue(0);
43 this.updateText();
44 }
45 }
46
47 public void adjustmentValueChanged(AdjustmentEvent e)
48 {
49 if (e.getSource().equals(scrollbar))
50 {
51 this.updateText();
52 }
53 }
54
55 private void updateText()
56 {
57 status.setText("Scroll Value = " + scrollbar.getValue());
58 }
59 }
60
Task: Write an applet that allows you to choose a system font to allow you to test how it will
look. The applet should look like Figure 9.19, “The FontChooser Applet Exercise” where the
Choice component allows you to select the font, the Scrollbar component allows you to
choose the size and the List allows you to style of the font. The font should be displayed
on the left using a Label component. You can see this applet running at
FontChooserApplet.html.
Hints: I have not shown you how to work with Font objects before, but it is relatively
straightforward for this applet. We will need to use Java's logical fonts for this applet as the
component we use to display the text is likely a Label object and this does not support
fonts other than logical fonts, such as physical fonts. So to create a font object use:
1
2 Label myLabel;
3 Font f = new Font("SansSerif", Font.BOLD + Font.ITALIC, 12);
4 myLabel.setFont(f);
5
Solution: Do not look at the solution until you have had a good attempt at coding it yourself.
If you get stuck look at my solution at: FontChooserApplet.java.
Advanced Note: If you need to use fonts other than the logical fonts again. You can get the
list of fonts available to you by using:
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
It will not work in this case as a Label component can only have logical font types.
As you have seen in this section there are many components that we can use. Previously in
the section called “A Simple Applet Example - Hello World!” we used the Applet
paint() to draw text on the screen. We also used this method to draw lines, set colours etc.
If want to combine components with drawing graphics you could just add a paint()
method to the applet and draw. This can however have unpredictable results, where a
component could be drawn over a line, or vice-versa.
Java provides us with the Canvas class that acts like any other component, but also allows
you to draw graphics directly to it - just like an artist's canvas. The other advantage of using a
component is that you can take advantage of the layout managers to control how the
applet/application will place the component as it is moved or resized. The Canvas
component specifies its co-ordinate system at (0,0) for the top left-hand corner.
Canvas components can handle mouse events like the Applet class. You must subclass
Canvas to add the behaviour you require, modifing the paint() method. So, for
example:
1
2
3 import java.awt.*;
4 import java.applet.*;
5
6 public class CanvasApplet extends Applet
7 {
8 MyCanvas c = new MyCanvas();
9
10 public void init()
11 {
12 this.add(this.c);
13 }
14 }
15
16
17 class MyCanvas extends Canvas
18 {
19 public void paint(Graphics g)
20 {
21 g.drawString("Test", 10, 10);
22 }
23 }
24
25
This provides your own Canvas class and you can create as many objects of this as you
require - each one only displaying "Test" at the (x,y) location (10,10). In this example above I
have added the MyCanvas class to the same .java file as CanvasApplet called
CanvasApplet.java. This is fine as it is not a public class and so cannot contain a valid
main() method. I have not written a constructor in the MyCanvas as it contains the
MyCanvas() one (with no parameters) by default and it is sufficient for this applet.
Here is an example of an applet that uses the Canvas class. It combines a BorderLayout
with a Button component and a modified Canvas component. The applet generates 200
random circles with different colours every time the "randomize!" button is pressed.
Figure 9.20, “The Canvas Applet Example” displays the applet running, and you can also run
it yourself at CanvasApplet.html.
1
2
3 //The Canvas Applet that generates random colour circles
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.applet.*;
8
9
10 public class CanvasApplet extends Applet implements ActionListener
11 {
12 MyCanvas c;
13
14 public void init()
15 {
16 Button b = new Button("Randomize!");
17 b.addActionListener(this);
18
19 c = new MyCanvas();
20
21 this.setLayout(new BorderLayout());
22 this.add("North", b);
23 this.add("Center",this.c);
24 }
25
26 public void actionPerformed(ActionEvent e)
27 {
28 c.drawCircles();
29 }
30
31 }
32
33
34 class MyCanvas extends Canvas
35 {
36 Color tempColor;
37
38 public void drawCircles()
39 {
40 this.repaint();
41 }
42
43 public void paint(Graphics g)
44 {
45 for(int i=0; i<200; i++)
46 {
47 tempColor = new Color((float)Math.random(),
48 (float)Math.random(),
49 (float)Math.random());
50 g.setColor(tempColor);
51 g.drawOval((int)(Math.random()*200),
52 (int)(Math.random()*200), 20, 20);
53 }
54 }
55 }
56
57
In this piece of code I have written two classes CanvasApplet and MyCanvas. In the
CanvasApplet class an object is created of the MyCanvas class and this is added to the
"Center" of the BorderLayout and added the Button object to the "North". When the
button "Randomize!" is pressed the drawCircles() method of our MyCanvas is called.
The MyCanvas class extends Canvas and overwrites the paint() method. The
drawCircles() just calls this.repaint() which calls the paint() method indirectly,
without requiring a Graphics object. To create the Color object we used the constructor: