View RSS Feed

penguinCoder

AWT Component Cheat Sheet

Rate this Entry
by , 10-30-2012 at 02:15 AM (2413 Views)
AWT Components Cheat Sheet

Importing Libraries
There are two main libraries that involve the AWT tools. This package contains the core AWT graphics classes. Contains component classes for buttons, text fields, labels, frames, panels, dialogs, and scroll panes. It also contains the layout managers such as flow layout, border layout, and grid layout. Also included are the cusrom graphics classes such as graphics, color and font.
Java Code:
import java.awt.*;

Common AWT Components
  • TextField
  • Button
  • Label
  • List
  • Choice
  • CheckBox


Name:  AWT_Components.png
Views: 541
Size:  58.9 KB


Label

Declare and allocate a Label instance.
Basic w/ No String
Java Code:
Label varName = new Label();
Basic w/ String:
Java Code:
Label varName = new Label("This is a label");
With alignment:
Java Code:
Label varName = new Label("This is a label", Label.CENTER);
Add the label to a container:
Java Code:
containerName.add(varName);
Modify the label's text string:
Java Code:
varName.setText("This is the new label");
Retrieve the label's text string:
Java Code:
varName.getText();
Anonymous Instance
This allows for an easier method, but you cannot modify the label in the future portion of the program if you use this method.
Java Code:
componentName.add( new Label ("This is the label", Label.RIGHT) );
This is the same as doing:
Java Code:
Label varName = new Label("This is the label", Label.RIGHT);
componenetName.add(varName);

Button

Declare and allocate a button instance:
Java Code:
Button varName = new Button("This is a button");
Add the button to a container:
Java Code:
containerName.add(varName);
Modify the button's label:
Java Code:
varName.setLabel("This is the new button label");
Retrieve the button's label:
Java Code:
varName.getLabel();
Anonymous Instance
Java Code:
add(Button ("Button Label") );

Text Field

Declare and allocate a TextField instance.
Given initial text and column width:
Java Code:
TextField varName = new TextField("Initial Text", 20);
Given just initial text:
Java Code:
TextField varName = new TextField("Initial Text");
Given true (editable) or false (non-editable)
Java Code:
TextField varName = new TextField(boolean);
Add the TextField to a container:
Java Code:
containerName.add(varName);
Modify the TextField's label:
Java Code:
varName.setText("New initial text");
Retrieve the TextField's label:
Java Code:
varName.getText();
Modifying Editable(true) or Non-Editable(false):
Java Code:
varName.setEditable(boolean)
Retrieving Integer from TextField
Java Code:
int number = Integer.parseInt( varName.getText() );
Inputting an Integer into TextField
Java Code:
varName.setText(number + "");

Things that Pertain to Below Methods
Declare and allocate a Font instance:
Java Code:
Font textFont = new Font("SansSerif", Font.BOLD, 14);
Available Default Colors:
Red, Magenta, Blue, Cyan, Green, Yellow, White, Gray, and Black.


List

Declare and allocate a List instance.
Empty List instance with 4 default visible rows:
Java Code:
List varName = new List();
Empty List instance with defined number of visible rows:
Java Code:
List varName = new List(rows#);
Empty List instance with defined number of visible rows and specified MultiSelect switch:
Java Code:
List varName = new List(rows#, boolean);
Add String into Next Available Slot:
Java Code:
varName.add("This is the next item");
Add String into Specified Slot:
Java Code:
varName.add("This is an item", index#);
Sets Background Color of List:
Java Code:
varName.setBackground(colorName);
Sets the Font for the Entire List:
Java Code:
varName.setFont(FontText);
Sets the Font Color of the Entire List:
Java Code:
varName.setForeground(colorName);
Set Default Selected Item:
Java Code:
varName.Select(index#);
Add the List to a container:
Java Code:
containerName.add(varName);
Set Event Handler for the List:
Java Code:
varName.addItemListener(ItemListenerName);

Choice

Declare and allocate a Choice instance:
Java Code:
Choice varName = new Choice ();
Add String into Specified Slot:
Java Code:
varName.insert("This is an item", index#);
Return a Specified Item:
Java Code:
varName.getSelectedIndex(index#);
Sets Background Color of Choice:
Java Code:
varName.setBackground(colorName);
Sets the Font for the Entire Choice:
Java Code:
varName.setFont(FontText);
Sets the Font Color of the Entire Choice:
Java Code:
varName.setForeground(colorName);
Set Default Selected Item:
Java Code:
varName.Select(index#);
Set Event Handler for the Choice:
Java Code:
varName.addItemListener(ItemListenerName);
Add the Choice to a container:
Java Code:
containerName.add(varName);

Checkbox

Declare and allocate a Checkbox instance.
Blank Checkbox instance:
Java Code:
Checkbox varName = new Checkbox();
Checkbox instance with specified text:
Java Code:
Checkbox varName = new Checkbox("This is a check box");
Checkbox instance with specified text and default selected(true) or non-selected(false) state:
Java Code:
Checkbox varName = new Checkbox("This is a check box", boolean);
Checkbox instance with specified text and default selected(true) or non-selected(false) state that is part of a group of Checkboxes, and will apear as Radio Buttons:
Java Code:
Checkbox varName = new Checkbox("This is a check box", groupName, boolean);
Modify the label of a Checkbox:
Java Code:
varName.setLabel("This is the new label for the check box");
Sets Background Color of Checkbox:
Java Code:
varName.setBackground(colorName);
Sets the Font for the Checkbox:
Java Code:
varName.setFont(FontText);
Set Default Selected(true) or Un-selected(false):
Java Code:
varName.setSelected(boolean);
Set Event Handler for the Choice:
Java Code:
varName.addItemListener(ItemListenerName);
Returns the State, Checked(true) or Not Checked(false):
Java Code:
varName.isSelected();
Add the Checkbox to a container:
Java Code:
containerName.add(varName);

Submit "AWT Component Cheat Sheet" to Facebook Submit "AWT Component Cheat Sheet" to Digg Submit "AWT Component Cheat Sheet" to del.icio.us Submit "AWT Component Cheat Sheet" to StumbleUpon Submit "AWT Component Cheat Sheet" to Google

Updated 10-30-2012 at 06:02 AM by penguinCoder

Categories
Cheat Sheet

Comments