Here, we are demonstrating several simple components of the Abstract
Windowing Toolkit (AWT). Button, Label, Checkbox, CheckboxGroup, Choice,
TextField, TextArea, List, and Scrollbar, are all represented here.
lab.setForeground(Color.black); //The background will be black
Note that these last two methods are the same as the last two we showed for Button... Why? Well, look at the following diagram
/-----Choice
/-----Button
Component------TextArea
\-----Label
\----TextField
\----etcetera, etc.
All of these, and many more extend Component, or rather, are subclasses of component. Then, it stands to reason that any method Component has, its subclasses will probably also have!
Remember when we were talking about frogs. We have a frog class,
which has methods, such as jump(), eatFlies(), ribbit(). Any time we
create a new frog, we automatically have all of these methods
available to us!
TextArea
The Source
The constructor here, again, takes a single argument... Let's look at
all of the constructors. (In the ones below that do NOT specify
number of rows and columns, a platform-dependent default is used.)
- TextArea noParams=new TextArea(); //no parameters
- TextArea rowsCols=new TextArea(5,50); //ints represent rows,cols
- TextArea writeMe=new TextArea("Hey, TextArea"); //takes a string
- TextArea total=new TextArea("Hey, TextArea",5,50); //takes a string, and also two ints (rows,cols)
While we're at it, let's add a few methods of the TextArea class. For these examples, let's say we have the following code...
import java.awt.*;
import java.applet.*;
public class demoTextArea extends Applet
{
TextArea writeMe =
new TextArea("Hey there");
public void init()
{
add(writeMe);
}
}
TextArea extends TextComponent, which extends Component...
Component.TextComponent.TextArea, get it?
TextArea Methods (which actually are TextComponent Methods)
- setEditable(boolean t)
- writeMe.setEditable(false); //this would set writeMe to being uneditable
writeMe.setEditable(true); //this would set writeMe to being editable
- getText()
- String getWriteMe=writeMe.getText(); //the String
getWriteMe will contain the text "Hey there"
- setText(String s)
- writeMe.setText("The text now says this"); //The text
will say 'The text now says this'.
Now remember, TextArea extends TextComponent which extends Component - so our TextArea can use the Component methods, too - such as setForeground, setBackground, etc...
TextField
The Source
The constructor here, again, takes a single argument... Let's look at
all of the constructors. (In the ones below that do NOT specify
number of characters, a platform-dependent default is used.)
- TextField noParams=new TextField(); //no parameters
- TextField fifty=new TextField(50);
//int represents number of characters
- TextField writeMe=new TextField("Hey, TextArea"); //takes a string
- TextField total=new TextField("Hey, TextArea",50); //takes a string, and also a number of characters
While we're at it, let's add a few methods of the TextField class. For these examples, let's say we have the following code...
import java.awt.*;
import java.applet.*;
public class demoTextField extends Applet
{
TextField writeMe =
new TextField("Hey there");
public void init()
{
add(writeMe);
}
}
TextField also extends TextComponent, which extends Component. Hence,
the methods below are the same we just showed for TextArea.
TextField Methods (which actually are TextComponent Methods)
- setEditable(boolean t)
- writeMe.setEditable(false); //this would set writeMe to being uneditable
writeMe.setEditable(true); //this would set writeMe to being editable
- getText()
- String getWriteMe=writeMe.getText(); //the String
getWriteMe will contain the text "Hey there"
- setText(String s)
- writeMe.setText("The text now says this"); //The text
will say 'The text now says this'.
Now remember, TextField also extends TextComponent which extends Component - so our TextField can use the Component methods, just like we showed for TextArea - such as setForeground, setBackground, etc...
Checkbox
The Source
Constructors...
- Checkbox c=new Checkbox(); //creates Checkbox with no label, no group, and unchecked
- Checkbox c=new Checkbox("Candy"); //creates Checkbox with label "Candy", no group, and unchecked
- Checkbox c=new Checkbox("Candy",null,true); //creates Checkbox with label "Candy" and true. This means that the Checkbox starts off by being checked. The middle argument, for which we have written null - is only used when creating a CheckboxGroup.
Now, you can use the methods which extend from Component, like setForeground(Color c), and setBackground(Color c). It also has getLabel() and setLabel(String s) - which are identical to how the ones for Button work. What else?
import java.awt.*;
import java.applet.*;
public class CheckIt extends Applet
{
Checkbox check=new Checkbox("Check");
public void init()
{
add(check);
}
}
Checkbox methods
- boolean getState()
- if (check.getState()= =true) { ... } --- getState returns either true or false (checked or unchecked)
- setState()
- check.setState(true);
check.setState(false); --- changes the Checkbox to either true or false
CheckboxGroup (also called Radio buttons)
The Source
For a CheckboxGroup, there is only one constructor: CheckboxGroup().
In order to add to this group, you need to use the three parameter
constructor while creating Checkboxes. Remember the third Constructor
above, where we said the middle argument was null? Well, instead of null, use the name of the CheckboxGroup.
import java.awt.*;
import java.applet.*;
public class ChkGroup extends Applet
{
CheckboxGroup one=new CheckboxGroup();
Checkbox chk1=new Checkbox("we",one,true);
Checkbox chk2=new Checkbox("are",one,false);
Checkbox chk3=new Checkbox("the",one,false);
Checkbox chk4=
new Checkbox("world",one,false);
public void init()
{
add(chk1);
add(chk2);
add(chk3);
add(chk4);
}
}
Now, CheckboxGroup does not extend Component - it extends Object. So you can't say CheckboxGroup.setForeground - but you CAN set the Foreground for each individual Checkbox within the CheckboxGroup.
Choice Menu
The Source
As we have seen from the source, there is addItem(String s). Another few methods for the following code...
import java.awt.*;
import java.applet.*;
public class ChoiceTest extends Applet
{
Choice c=new Choice();
public void init()
{
c.addItem("boilers");
c.addItem("spartans");
c.addItem("wolverines");
c.addItem("buckeyes");
c.addItem("nittany lions");
add(c);
}
}
Choice Methods
- int countItems()
- int x=c.countItems(); //x will be the number of items in the choice menu
- String getItem(int index)
- String s=c.getItem(1); //s will be "spartans" remember arrays begin with zero (0)
- String getSelectedItem()
- String s=c.getSelectedItem(); //s will contain whichever item had been selected
- int getSelectedIndex()
- int x=c.getSelectedIndex(); //x will be the index number of the selected item
- void select(int index)
- c.select(2); //wolverines will be selected
- void select(String s)
- c.select("boilers"); //boilers will be selected
Scrolling List
The Source
List constructors...
- List(); // no lines, no multiple selection capability
- List(int rows, boolean moreThanOne); // how many lines show at once, can you select more than one
List l = new List(3,true);
Using this example...
import java.awt.*;
import java.applet.*;
public class ListTest extends Applet
{
List l=new List();
public void init()
{
l.addItem("Pac10");
l.addItem("Big10");
l.addItem("ACC");
l.addItem("WAC");
l.addItem("MAC");
add(l);
}
}
List Methods
- int countItems()
- int x=l.countItems(); //x will be the number of items in the scrolling list
- String getItem(int index)
- String s=l.getItem(1); //s will be "Big10" remember arrays begin with zero (0)
- String getSelectedItem()
- String s=l.getSelectedItem(); //s will be the String that is currently selected
- String[] getSelectedItems()
- String[] conf=l.getSelectedItems(); //conf will be an array holding all selected Items
- int getSelectedIndex()
- int x=l.getSelectedIndex(); //x will be the index of the current selection
- int getSelectedIndexes()
- int[] x=l.getSelectedIndexes(); //x will be an array holding the indexes of all current selections
Scrollbar
The Source
Scrollbar constructors...
- Scrollbar s=new Scrollbar(); --- default scrollbar
- Scrollbar s=new Scrollbar(Scrollbar.HORIZONTAL); --- horizontal scrollbar
- Scrollbar s=new Scrollbar(Scrollbar.VERTICAL); --- vertical scrollbar
- Scrollbar s=new Scrollbar(int1, int2, int3, int4, int5);
- int1 - orientation - either scrollbar.HORIZONTAL or scrollbar.VERTICAL
- int2 - initial value of the scrollbar - needs to be in between the minimum and maximum values
- int3 - overall width (or height, depending upon orientation) of the scrollbar's box
- int4 - minimum value
- int5 - maximum value
Other Scrollbar Methods
- public int getValue()
- int x=s.getValue(); //x will be the current value of the Scrollbar
- public void setValue(int value)
- s.setValue(627); //the current value of the Scrollbar is changed to 627
Color
Now, Color is obviously an important part of any programming language. We introduced it a little bit, when we used Color.red, etc. But what is Color, in Java?
Well, it turns out that Color is a class from the java.awt package. Let's look at the constructors...
- Color(int r, int g, int b); //r,g,b are int values for red, green, blue 0-255
- Color(int rgb); //rgb is an integer value containing rgb value
- Color(float r, float g, float b); //r,g,b are float values for red, green, blue 0-1.0
So how does this work? Well... Let's look at some code...
import java.awt.*;
import java.applet.*;
public class ColorTest extends Applet
{
Label lab =
new Label("These words will be white!");
public void init()
{
lab.setForeground(Color.white);
lab.setBackground(Color.black);
add(lab);
}
}
This is how we have been doing it, right? The Color class has 13 predefined Member Constants - which are all pre-made Colors. These are white, lightGray, gray, darkGray, black, red, pink, orange, yellow, green, magenta, cyan, blue. And they can all be referred to as in the code above... Ex:
- Color.darkGray
- Color.orange
- Color.magenta
- etc...
But what I want to know, is what happened to Fuchsia? Or tan? How about flesh? Even the Crayola 64-pack with the crayon sharpener on the back of the box, had flesh... Or even turquoise?
Well, that's where the Constructors for NEW Colors comes... Look at the following code
import java.awt.*;
import java.applet.*;
public class ColorTest extends Applet
{
Color white=new Color(255,255,255);
Color black=new Color(0,0,0);
Label lab =
new Label("These words will be white!");
public void init()
{
lab.setForeground(white);
lab.setBackground(black);
add(lab);
}
}
Now, this does the same thing as the first program did! Wow! That means that we will be able to access up to 255*255*255, or 16,581,375 colors with ANY java applet we want. Now, yeah, we know that most monitors can only offer a close approximation - and who ever needed more than the crayola 64 pack, anyway?
Now, for the methods... We'll use the program above...
Color methods
- int getRed()
- int x=white.getRed(); //returns the r, or red component of this color
- int getGreen()
- int x=white.getGreen(); //returns the g, or green component of this color
- int getBlue()
- int x=white.getBlue(); //returns the b, or blue component of this color
- int getRGB()
- int x=white.getRGB(); //returns the RGB of our white color
These are all pretty cool, and really useful. For example what if you were creating a Paint Shop Applet, and needed to emulate the eyedropper tool? But what about manipulating Colors, AFTER they have been created...
- Color brighter()
- Color brightBlack=black.brighter(); //brightBlack will be 1.4 times brighter than black was
- Color darker()
- Color darkWhite=white.darker(); //darkWhite will be 1.4 times darker than white was
Now, there are a whole slew of other options, with Color() - converting from RGB to HSB, and back, checking two Colors to see if they are the same, etc - but we aren't going to get into those here.