Building GUIs in Java II A pictures worth
Building GUIs in Java II A picture's worth a thousand words CS 102 -02 Lecture 8 -1 May 18, 1998 CS 102 -02 Lecture 8 -1
Agenda • Choices, choices • Check this out • Single and double lists May 18, 1998 CS 102 -02 Lecture 8 -1
Interface Sends a Message • Component choices say something about what you expect • Layout/tab order suggests a sequence May 18, 1998 CS 102 -02 Lecture 8 -1
A Menu of Choices • Choice component gives a drop-down list – Clicking the arrow draws a list of choices • No way to enter a new choice (Combo. Box) – List all the alternatives May 18, 1998 CS 102 -02 Lecture 8 -1
Building a Choice • Click 'n choose set. Layout(null); set. Size(278, 173); set. Font(new Font("Dialog", Font. PLAIN, 24)); destruction = new java. awt. Choice(); destruction. add. Item("Asteroid"); destruction. add. Item("Earthquake"); destruction. add. Item("Fire"); destruction. add. Item("Flood"); destruction. add. Item("Godzilla"); destruction. add. Item("Twister"); add(destruction); destruction. set. Bounds(24, 48, 180, 48); May 18, 1998 CS 102 -02 Lecture 8 -1
Variations on a Theme destruction. set. Font(new Font("Dialog", Font. ITALIC, 24)); destruction. set. Foreground(new Color(16711680)); destruction. set. Background(new Color(255)); May 18, 1998 CS 102 -02 Lecture 8 -1
Common Choice Methods I • add(String) Adds an item to this Choice menu. • get. Item(int) Gets the string at the specified index in this Choice menu. • get. Item. Count() Returns the number of items in this Choice menu. • get. Selected. Index() Returns the index of the currently selected item. • get. Selected. Item() Gets a representation of the current choice as a string. • get. Selected. Objects() Returns an array (length 1) containing the currently selected item. May 18, 1998 CS 102 -02 Lecture 8 -1
Common Choice Methods I • insert(String, int) Inserts the item into this choice at the specified position. • remove(int), remove(String) Removes an item • remove. All() Removes all items from the choice menu. • select(int) Sets the selected item in this Choice menu to be the item at the specified position. • select(String) Sets the selected item in this Choice menu to be the item whose name is equal to the specified string. May 18, 1998 CS 102 -02 Lecture 8 -1
A Choice Applet public class My. Choice extends Applet { private Choice fonts; private Text. Field t; public void init() { // Create a Choice button and add items to it fonts = new Choice(); fonts. add( "Monospaced" ); // Courier fonts. add( "Sans. Serif" ); // Helvetica fonts. add( "Serif" ); // Times t = new Text. Field( fonts. get. Item( 0 ), 30 ); t. set. Editable( false ); t. set. Font( new Font( fonts. get. Item( 0 ), Font. PLAIN, 12 ) ); fonts. add. Item. Listener( new Font. Name. Handler( t ) ); fonts. add. Item. Listener( new Set. Text. Field. Handler( t ) ); } } add( fonts ); add( t ); May 18, 1998 CS 102 -02 Lecture 8 -1
The Handler class Set. Text. Field. Handler implements Item. Listener { private Text. Field field; public Set. Text. Field. Handler( Text. Field t ) { field = t; } public void item. State. Changed( Item. Event e ) { Choice choice = (Choice) e. get. Item. Selectable(); field. set. Text( "Index: " + choice. get. Selected. Index() + "; Font: " + e. get. Item() ); } } May 18, 1998 CS 102 -02 Lecture 8 -1
Get Your Red Hot Checkboxes Here • Checkboxes offer a graphical yes/no – Use one Checkbox, and you get a Checkbox – Add multiple Checkboxes to a Checkbox. Group and you get radio buttons May 18, 1998 CS 102 -02 Lecture 8 -1
Building Solitary Checkboxes • Checkbox() – Creates a check box with no label. • Checkbox(String) – Creates a check box with the specified label. • Checkbox(String, boolean) – Creates a check box with the specified label. May 18, 1998 CS 102 -02 Lecture 8 -1
Radio Buttons Galore • Use a Checkbox. Group to treat related Checkboxes as radio buttons – Create a Checkbox. Group – Create a Checkbox – Add the Checkbox to the group • Checkbox. Group is NOT a Component May 18, 1998 CS 102 -02 Lecture 8 -1
Radio Buttons from Checkboxes test. Group = new Checkbox. Group(); adventure. Radio. Button = new java. awt. Checkbox("Adventure", test. Group, false); adventure. Radio. Button. set. Background(new Color(12632256)); add(adventure. Radio. Button); action. Radio. Button = new java. awt. Checkbox("Body count action", test. Group, false); action. Radio. Button. set. Background(new Color(12632256)); add(action. Radio. Button); comedy. Radio. Button = new java. awt. Checkbox("Buddy-cop comedy", test. Group, false); comedy. Radio. Button. set. Background(new Color(12632256)); add(comedy. Radio. Button); drama. Radio. Button = new java. awt. Checkbox("Tear-jerking drama", test. Group, false); drama. Radio. Button. set. Background(new Color(12632256)); add(drama. Radio. Button); May 18, 1998 CS 102 -02 Lecture 8 -1
What You See May 18, 1998 CS 102 -02 Lecture 8 -1
Using Checkboxes & Radio Buttons • If you can phrase the choice as a yes/no question, use a Checkbox • If only one alternative from many is possible, use radio buttons – Initial selection? – Is no selection a valid choice? (How about 'Other' with a Text. Field? ) – Not as in Figure 10. 15 May 18, 1998 CS 102 -02 Lecture 8 -1
Lists • Scrollable list of text items – Similar to a Choice, but is always "dropped down" – Single and multiple selection lists May 18, 1998 CS 102 -02 Lecture 8 -1
List Styles May 18, 1998 CS 102 -02 Lecture 8 -1
Building Lists • List() – Creates a new scrolling list. • List(int) – Creates a new scrolling list initialized with the specified number of visible lines. • List(int, boolean) – Creates a new scrolling list initialized to display the specified number of rows. May 18, 1998 CS 102 -02 Lecture 8 -1
Handy List Methods • get. Item(int) – Gets the item associated with the specified index. • get. Item. Count() – Gets the number of items in the list. • get. Items() – Gets the items in the list. • get. Rows() – Get the number of visible lines in this list. • get. Selected. Index() – Gets the index of the selected item on the list, • get. Selected. Item() – Get the selected item on this scrolling list. May 18, 1998 CS 102 -02 Lecture 8 -1
List Events • Item is selected or deselected, AWT sends Item. Event to the list • Double-clicks on an item in a scrolling list, AWT sends Action. Event to the list following the item event • Generates an action event when the user presses the return key while an item in the list is selected. May 18, 1998 CS 102 -02 Lecture 8 -1
Multiple Selection Lists • Allow for multiple selections simultaneously – Method of multiple selection varies across platforms • Event generation – Use an external gesture (such as clicking on a button) to trigger the action, rather than double-clicking or the Return key May 18, 1998 CS 102 -02 Lecture 8 -1
Multiple Selection Methods • get. Selected. Indexes() – Gets the selected indexes on the list. • get. Selected. Items() – Get the selected items on this scrolling list. • is. Index. Selected(int) – Determines if the specified item in this scrolling list is selected. • is. Multiple. Mode() – Determines whether this list allows multiple selections. May 18, 1998 CS 102 -02 Lecture 8 -1
Using Multiple Selections in Java public class My. List 2 extends Applet implements Action. Listener { private List color. List, copy. List; private Button copy; private String color. Names[] = { "Black", "Blue", "Cyan", "Dark Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow" }; public void init() { // create a list with 5 items visible // allow multiple selections color. List = new List( 5, true ); // add items to the list for ( int i = 0; i < color. Names. length; i++ ) color. List. add( color. Names[ i ] ); add( color. List ); // create copy button copy = new Button( "Copy >>>" ); copy. add. Action. Listener( this ); add( copy ); } // create a list with 5 items visible // do not allow multiple selections copy. List = new List( 5, false ); add( copy. List ); May 18, 1998 CS 102 -02 Lecture 8 -1
Using Multiple Selections in Java public void action. Performed( Action. Event e ) { String colors[]; // get the selected states colors = color. List. get. Selected. Items(); } // copy them to copy. List for ( int i = 0; i < colors. length; i++ ) copy. List. add( colors[ i ] ); May 18, 1998 CS 102 -02 Lecture 8 -1
- Slides: 25