Components and Containers


Components and Containers

Containers

Containers hold and organize your Components, but they also contain code for event handling and many 'niceties' such as controlling the cursor image and the application's icon.
Containers:

Component

Components are generally the stuff that the user interacts with. The meat and potatoes of your application. (as discussed in the Introduction).

List of common Components


To view this applet you need a Java-enabled browser

Lets walk through Sun's example.

public class ComponentTest extends Applet {
    Panel 	center;
The following code does
  1. Sets the background to gray
  2. Sets the font to plain 12pt Helvetica font
  3. Sets layout to BorderLayout(i.e "North, South, Center, East, West")
  4. Creates a label "North" that has center alignment
    public void init() {
	setBackground(Color.lightGray);
	setFont(new Font("Helvetica", Font.PLAIN, 12));
	setLayout(new BorderLayout());
	Label l  = new Label("North");
	l.setAlignment(Label.CENTER);
  1. Adds a TextField(i.e Edit control) to the lower porton of the container, which is the applet
  2. Adds the label to the upper portion of the container
  3. Creates a ScrollPanel and sets its layout, then centers it within the container
	add("South", new TextField("South"));
	add("North", l);
	center = new ScrollPanel();
	center.setLayout(new BorderLayout());
	add("Center", center);
  1. Creates a new ScrollPanel that is centered within an existing panel(i.e center)
  2. Create a label, Buttons, SillyWidget(see below), a TextField and two scrollbars. All of which are laid out in a "Flow"
	Panel innerPanel = new ScrollPanel();
	center.add("Center", innerPanel);
	innerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	innerPanel.add(new Label("List of Widgets"));
	innerPanel.add(new Button("Arthur"));
	innerPanel.add(new Button("Sami"));
	innerPanel.add(new SillyWidget());
	innerPanel.add(new TextField("quite random"));
	innerPanel.add(new Scrollbar(Scrollbar.VERTICAL));
	innerPanel.add(new Scrollbar(Scrollbar.HORIZONTAL));
  1. Creation of a new List(i.e Listbox of strings) of 4 items and is not multi-selectable
  2. Resize using the reshape method available to every Component.
  3. Adds strings to the List(i.e "Arthur", "Sami", etc.)
  4. Sets 1(i.e Arthur) to be selected and hilighted
	List li = new List(4, false);

	li.reshape(0, 0, 100, 100);
	li.addItem("Arthur");
	li.addItem("Sami");
	li.addItem("James");
	li.addItem("Chris");
	li.addItem("Mary");
	li.addItem("Lisa");
	li.addItem("Kathy");
	li.select(1);
  1. Adds the List to the innerPanel which was centered within the first ScrollPanel
  2. Creates a TextArea, of 10 rows by 5 columns
  3. Adds some text and sets the foreground color(i.e text color in this case)
  4. Then adds the TextArea to the panel
	innerPanel.add(li);
	TextArea tx = new TextArea(10, 5);
	tx.setText("this is some random text");
	tx.setForeground(Color.red);

	innerPanel.add(tx);
  1. Creation of choice. A choice is similar to a List, but takes a smaller space on the screen as it displays only one selection until the use clicks on it. Note: Once you add something to a choice, you cannot remove it
  2. Adds the choice to the innerPanel
	Choice	opt = new Choice();

	opt.addItem("Arthur");
	opt.addItem("Sami");
	opt.addItem("Chris");
	opt.addItem("Jim");

	innerPanel.add(opt);
  1. Creation of a CheckBoxGroup. A group of checkbox by themselves are independant of each other,
  2. but in a CheckboxGroup only one can be selected at a time.
  3. Creation of Checkboxes that are added to the innerPanel
  4. Creation of two scrollbars.
	CheckboxGroup g = new CheckboxGroup();
	innerPanel.add(new Checkbox("one", g, false));
	innerPanel.add(new Checkbox("two", g, true));
	innerPanel.add(new Checkbox("three", g, false));
	center.add("East", new Scrollbar(Scrollbar.VERTICAL));
	center.add("South", new Scrollbar(Scrollbar.HORIZONTAL));

	add("East", new Button("East"));
	add("West", new Button("West"));
    }
  1. A Synchronized method that handles user input. In this case handleEvent just returns false
  2. because it wants the AWT to handle the events and use the default handling routines
    public synchronized boolean handleEvent(Event e) 
	{
		return false;
	}
    }
Definition of the ScrollPanel used above
class ScrollPanel extends Panel {
    public ScrollPanel() {
	reshape(0, 0, 100, 100);
    }

// Overriden paint method that draws a rectange around the panel
    public void paint(Graphics g) {
	Rectangle r = bounds();

	g.drawRect(0, 0, r.width, r.height);
    }
}


The SillyWidget should look like this:

class SillyWidget extends Canvas {
    Color	c1 = Color.pink;
    Color	c2 = Color.lightGray;
    long	entered = 0;
    long	inComponent = 0;
    Font	font = new Font("Courier", Font.BOLD, 12);
    FontMetrics fm;

    public SillyWidget() {
	reshape(0, 0, 100, 100);
    }

// Called when the mouse enters the Canvas
// This widget is just change the colors and forcing a repaint when the
// mouse enters 
    public boolean mouseEnter(Event e, int x, int y) {
	Color c = c1;
	entered = e.when;
	c1 = c2;
	c2 = c;
	repaint();
	return true;
    }

// Called when the mouse exits the Canvas
// This widget is just change the colors and forcing a repaint when the
// mouse exits

    public boolean mouseExit(Event e, int x, int y) {
	Color c = c1;

	inComponent = e.when - entered;
	c1 = c2;
	c2 = c;
	repaint();
	return true;
    }

// Called when the Canvas needs repaint and merely draws a bunch of rectangles
    public void paint(Graphics g) {
	Rectangle	r = bounds();

	g.setColor(c1);
	g.fillRoundRect(0, 0, r.width, r.height, 20, 20);
	g.setColor(c2);
	g.fillRoundRect(5, 5, r.width - 10, r.height - 10, 20, 20);
	g.setColor(c1);
	g.fillRoundRect(15, 15, r.width - 30, r.height - 30, 20, 20);
	g.setColor(Color.darkGray);
	g.setFont(font);
	fm = g.getFontMetrics(font);
	String s = ""+inComponent+" ms.";
	int width = fm.stringWidth(s);
	System.out.println("width = " + width);
	g.drawString(s,
		     (r.width - width) / 2,
		     (r.height - fm.getHeight()) / 2);
    }
}
	


[Q & A's]

Q: Where can I find documentation and usage of Containers/Components?
A: You can find the here. You can also look through the example programs in the src/java/awt/test directory.

Q: Can I find a quick and dirty list of constructors for them?
A: Here is a quick list:
Components:

Containers:

[Next] [Prev] [Home]
Nelson Yu
nelson@cs.ualberta.ca
Last modified: Jan 2 1996