Chapter 3 Widgets for a GUI General Component






















- Slides: 22

Chapter 3: Widgets for a GUI • General Component methods • Useful widgets classes – Text classes • Label • Text. Field • Text. Area – Active widgets • • Button Checkbox Choice List

Building a GUI • Graphical User Interfaces composed of widgets (buttons, text fields, etc. ) • Various useful classes defined in AWT • Part of Component class (abstract class)

Component methods (inherited by all): Point location() - x, y position Dimension size() - width, height of object Rectangle bounds() - both location, size info void move (int x, int y) - set x, y position void resize (int w, int h) - set w, h of object void resize (Dimension d) - similar void reshape (int x, int y, int w, int h) - set rectangle Dimension minimum. Size() - determined by system, smallest size needed Dimension preferred. Size() - similar to minimum

Component methods (cont): Color get. Background() - return background color Color get. Foreground() - return foreground color void set. Background(Color c) - set background color void set. Foreground(Color c) - set foreground color Font get. Font() - return current font void set. Font(Font f) - set current font boolean is. Showing() - true if Component on screen boolean is. Visible() - true if object should be visible void hide() - turns component invisible void show() - makes component visible

Component methods (cont): Graphics get. Graphics() - returns the Graphics object associated with the current component, useful for doing graphical operations on this component void paint(Graphics g) - method that “shows” component, should not be called by user (overridden) void repaint() - informs the computer that this component needs to be repainted void repaint(int x, int y, int width, int height) - similar to repaint() but indicates only the portion falling in the rectangle needs to be redone void update(Graphics g) - how component redone (generally box erased, repainted), useful to avoid flicker, should not be called by user (overridden)

Text Classes • Label - simple text statements, generally one line with no editing expected • Text. Component - abstract class with methods used by – Text. Field - one line text, with some editing – Text. Area - generally for more than one line of text with some editing

Label class constructors: Label() - no text, center alignment Label(String label) - center-aligned label with text Label(String label, int alignment) - label with text aligned based on arg, vals: Label. LEFT, CENTER, RIGHT methods: String get. Text() - return string associated with Label void set. Text(String label) - set string int get. Alignment() - return Label’s current alignment void set. Alignment(int alignment) - set alignment

Text. Component class abstract class (parent of Text. Field, Text. Area) methods: String get. Text() - return the text associated with object void set. Text(String text) - set text of object boolean is. Editable() - flag indicating text can be edited void set. Editable(boolean can. Edit) - set editable flag String get. Selected. Text() - return highlighted text int get. Selection. Start() - location where selection starts int get. Selection. End() - location where selection ends void select (int start, int end) - select text start-end void select. All() - select all of text

Text. Field class - one line, editing constructors: Text. Field() - empty (no text), default (system) width Text. Field(int columns) - no text, columns wide Text. Field(String text) - set text, use it for width Text. Field(String text, int columns) - set text, width methods: int get. Columns() - return column width of object void set. Columns(int c) - set column width char get. Echo. Char() - return echo char (if any) void set. Echo. Char(char c) - set echo char boolean echo. Char. Is. Set() - check if echo char set Dimension minimum. Size() - minimum needed size Dimension preferred. Size() - preferred size

Text. Area class - > 1 line, editing look and feel depends on system (may include scrollbars) constructors: Text. Area() - empty (no text), default (system) size Text. Area(int rows, int cols) - no text, rows, cols size Text. Area(String text) - set text, default sized Text. Field(String text, int rows, int cols) - set text, size methods: int get. Columns() - column width of object int get. Rows() - row height of object

Text. Area class (cont) methods: void append. Text(String str) - add string to the end of the current text void insert. Text(String str, int pos) - insert string at position pos void replace. Text(String str, int start, int end) - insert string replacing chars from start to end Dimension minimum. Size() - minimum required size Dimension preferred. Size() - preferred size

import java. applet. *; import java. awt. *; public class Test. Class extends Applet { Label my. Label 1 = new Label("Test label 1", Label. LEFT); Label my. Label 2 = new Label(); Text. Field my. Text. Field 1 = new Text. Field("Init text", 20); Text. Field my. Text. Field 2 = new Text. Field("Start text"); Text. Area my. Text. Area = new Text. Area("Initial text", 3, 10); } public void init () { resize(500, 400); my. Label 1. set. Text("TLab 1"); my. Label 2. set. Text("TLab 2"); my. Text. Field 1. set. Foreground(Color. green); my. Text. Field 2. set. Echo. Character('*'); my. Text. Area. insert. Text("silly ", 8); add(my. Label 1); add(my. Text. Field 1); add(my. Text. Area); add(my. Label 2); add(my. Text. Field 2); }

Active Widgets • Various classes provide mechanisms for users to indicate info: – Button - labeled object that can be clicked – Checkbox - labeled object, can be checked/un – Choice - object providing multiple choices with one showing – List - object providing multiple choices with all showing

Button class constructors: Button() - unlabeled button Button(String label) - button with label methods: String get. Label() - return label of object void set. Label(String label) - set label of Button We will learn later how to check if the mouse has been used to access button

Checkbox class constructors: Checkbox() - no label, initially unchecked Checkbox(String label) - labeled, initially unchecked Checkbox(String label, Checkbox. Group group, boolean state) * methods: String get. Label() - return label of object void set. Label(String label) - set label of Checkbox boolean get. State() - determine if checked/unchecked void set. State(boolean state) - set the state of the Checkbox. Group get. Checkbox. Group() - returns group *

Checkbox. Group class Checkbox objects added using Checkbox methods constructors: Check. Box. Group() - empty group methods: Checkbox get. Current() - Checkbox of group that is checked void set. Current(Checkbox c) - set c as checked member

import java. applet. *; import java. awt. *; public class Test. Button extends Applet { Button my. Button = new Button("AButton"); Checkbox my. Check. Box 1 = new Checkbox("CB 1"); Checkbox my. Check. Box 2 = new Checkbox("CB 2"); Checkbox. Group my. Check. Box. Group = new Checkbox. Group(); Checkbox my. CBG 1 = new Checkbox("Big"); Checkbox my. CBG 2 = new Checkbox("Bigger"); Checkbox my. CBG 3 = new Checkbox("Biggest"); } public void init () { resize(500, 400); my. Check. Box 2. set. State(true); my. CBG 1. set. Checkbox. Group(my. Check. Box. Group); my. CBG 2. set. Checkbox. Group(my. Check. Box. Group); my. CBG 3. set. Checkbox. Group(my. Check. Box. Group); my. Check. Box. Group. set. Current(my. CBG 1); add(my. Check. Box 1); add(my. Button); add(my. CBG 1); add(my. CBG 2); add(my. CBG 3); add(my. Check. Box 2); }

Choice class constructor: Choice() - initially empty set of choices methods: void add. Item(String item) - add the named item to the choices int get. Selected. Index() - return num of selected item String get. Selected. Item() - return string associated with item String get. Item(int index) - returns string of index item void select(int index) - select the index item (starting at 0) void select(String item) - select item with string name int count. Items() - return number of items

List class constructors: List() - default-size list in single-selection mode List(int rows, boolean mult. Sel) - list with rows, if mult. Sel true can make multiple selections methods: void add. Item(String item) - add the named item to the choices void add. Item(String item, int index) - add item at loc index void del. Item(int index) - remove the index item void del. Items(int start, int end) - remove items from start to end void clear() - remove all items void replace. Item(String item, int index) - replace item at index int count. Items() - count of the number of items in List

methods: void get. Selected. Index() - num of selected index -1 if multiple or none boolean is. Selected(int index) - true if index selected void select(int index) - set index as selected void deselect(int index) - unselect index void make. Visible(int index) - scroll so index visible int get. Visible. Index() - index of last make. Visible call boolean allows. Multiple. Selections() - true if list allows multiple selections void set. Multiple. Selections(boolean m) - set multiple selection flag to m

methods: int get. Rows() - number of rows in object Dimension minimum. Size() - min size needed Dimension minimum. Size(int rows) - min size for rows Dimension preferred. Size() - preferred size Dimension preferred. Size(int rows) - preferred by rows

import java. applet. *; import java. awt. *; public class Test. Lists extends Applet { Choice my. Choice = new Choice(); List my. List = new List(4, true); } public void init () { resize(500, 400); my. Choice. add. Item("Small"); my. Choice. add. Item("Medium"); my. Choice. add. Item("Large"); my. Choice. select("Medium"); my. List. add. Item("Left"); my. List. add. Item("Right"); my. List. add. Item("Straight"); my. List. select(0); my. List. select(2); add(my. Choice); add(my. List); }