All Kinds of AWTful things going on here...

This document was written by CS 290W TA Joshua Kay and was last modified

The thing about Java, there are a billion things already written for you - Buttons, Labels, all kinds of GUI components. So, how do we know what is or isn't written for us? A complete reference is located at Sun's Java Platform Documentation. This is the official Java online reference for all implementations of the JDK.

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.


Button

The Source
Note the constructor takes a Single Parameter - a String - which is then placed on top of the component. You do not need to add a String in order to create the Button, however. Both of the following are valid:
Let's show you some of the other things about Buttons. The Button class extends the Component Class. If you recall, any Applet we write extends the Applet class. This means that the Button class is a subclass of the Component Class, and can therefore use all of its methods. Let's show you some, using the following code...
import java.awt.*; 
import java.applet.*;
public class ButtonMethods extends Applet 
{
 Button one=new Button("Ima Button");
 
 public void init()
 {
  add(one);
 }
}
Button Methods...
getLabel()
String getOne=one.getLabel(); //The String getOne will contain the label of Button one ("Ima Button")
setLabel(String s);
one.setLabel("Buttons Rule!"); //The Button one will read "Buttons Rule!")

Methods extending from Component

setForeground(Color c);
one.setForeground(Color.red); //The text will be red
setBackground(Color c);
one.setBackground(Color.black); //The background will be black


Label

The Source
The constructor here also takes a single argument... let's look at some more, shall we?
Let's look at that last one again... How is Label.RIGHT an Integer? Well, in the Label class, there are three (3) constants - LEFT,CENTER, and RIGHT - these all represent integers which represent their respective alignments. So, you could have also had Label.CENTER or Label.LEFT.
import java.awt.*; 
import java.applet.*;
public class LabelMethods extends Applet 
{
 Label lab =
  new Label("Extracurricular Activities");
 
 public void init()
 {
  add(lab);
 }
}
Label Methods...
getText()
String whatsit=lab.getText(); //The String whatsit will contain the text from the Label lab ("Extracurricular Activities")
setText (String s);
lab.setText("Labels Rule!"); //The Label lab will read "Labels Rule!"

Methods Extending from Component

setForeground(Color c);
lab.setForeground(Color.red); //The text will be red
setBackground(Color c);
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.)
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.)
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... 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 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... 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...

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: 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.