AWT Component Cheat Sheet
by
, 10-30-2012 at 03:15 AM (5833 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
Label
Declare and allocate a Label instance.
Basic w/ No String
Basic w/ String:Java Code:Label varName = new Label();
With alignment:Java Code:Label varName = new Label("This is a label");
Add the label to a container:Java Code:Label varName = new Label("This is a label", Label.CENTER);
Modify the label's text string:Java Code:containerName.add(varName);
Retrieve the label's text string:Java Code:varName.setText("This is the new label");
Anonymous InstanceJava Code:varName.getText();
This allows for an easier method, but you cannot modify the label in the future portion of the program if you use this method.
This is the same as doing:Java Code:componentName.add( new Label ("This is the label", Label.RIGHT) );
Java Code:Label varName = new Label("This is the label", Label.RIGHT); componenetName.add(varName);
Button
Declare and allocate a button instance:
Add the button to a container:Java Code:Button varName = new Button("This is a button");
Modify the button's label:Java Code:containerName.add(varName);
Retrieve the button's label:Java Code:varName.setLabel("This is the new button label");
Anonymous InstanceJava Code:varName.getLabel();
Java Code:add(Button ("Button Label") );
Text Field
Declare and allocate a TextField instance.
Given initial text and column width:
Given just initial text:Java Code:TextField varName = new TextField("Initial Text", 20);
Given true (editable) or false (non-editable)Java Code:TextField varName = new TextField("Initial Text");
Add the TextField to a container:Java Code:TextField varName = new TextField(boolean);
Modify the TextField's label:Java Code:containerName.add(varName);
Retrieve the TextField's label:Java Code:varName.setText("New initial text");
Modifying Editable(true) or Non-Editable(false):Java Code:varName.getText();
Retrieving Integer from TextFieldJava Code:varName.setEditable(boolean)
Inputting an Integer into TextFieldJava Code:int number = Integer.parseInt( varName.getText() );
Java Code:varName.setText(number + "");
Things that Pertain to Below Methods
Declare and allocate a Font instance:
Available Default Colors:Java Code:Font textFont = new Font("SansSerif", Font.BOLD, 14);
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:
Empty List instance with defined number of visible rows:Java Code:List varName = new List();
Empty List instance with defined number of visible rows and specified MultiSelect switch:Java Code:List varName = new List(rows#);
Add String into Next Available Slot:Java Code:List varName = new List(rows#, boolean);
Add String into Specified Slot:Java Code:varName.add("This is the next item");
Sets Background Color of List:Java Code:varName.add("This is an item", index#);
Sets the Font for the Entire List:Java Code:varName.setBackground(colorName);
Sets the Font Color of the Entire List:Java Code:varName.setFont(FontText);
Set Default Selected Item:Java Code:varName.setForeground(colorName);
Add the List to a container:Java Code:varName.Select(index#);
Set Event Handler for the List:Java Code:containerName.add(varName);
Java Code:varName.addItemListener(ItemListenerName);
Choice
Declare and allocate a Choice instance:
Add String into Specified Slot:Java Code:Choice varName = new Choice ();
Return a Specified Item:Java Code:varName.insert("This is an item", index#);
Sets Background Color of Choice:Java Code:varName.getSelectedIndex(index#);
Sets the Font for the Entire Choice:Java Code:varName.setBackground(colorName);
Sets the Font Color of the Entire Choice:Java Code:varName.setFont(FontText);
Set Default Selected Item:Java Code:varName.setForeground(colorName);
Set Event Handler for the Choice:Java Code:varName.Select(index#);
Add the Choice to a container:Java Code:varName.addItemListener(ItemListenerName);
Java Code:containerName.add(varName);
Checkbox
Declare and allocate a Checkbox instance.
Blank Checkbox instance:
Checkbox instance with specified text:Java Code:Checkbox varName = new Checkbox();
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");
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", boolean);
Modify the label of a Checkbox:Java Code:Checkbox varName = new Checkbox("This is a check box", groupName, boolean);
Sets Background Color of Checkbox:Java Code:varName.setLabel("This is the new label for the check box");
Sets the Font for the Checkbox:Java Code:varName.setBackground(colorName);
Set Default Selected(true) or Un-selected(false):Java Code:varName.setFont(FontText);
Set Event Handler for the Choice:Java Code:varName.setSelected(boolean);
Returns the State, Checked(true) or Not Checked(false):Java Code:varName.addItemListener(ItemListenerName);
Add the Checkbox to a container:Java Code:varName.isSelected();
Java Code:containerName.add(varName);