Graphic Components II
Download
Report
Transcript Graphic Components II
Building GUIs in Java II
A picture's worth a thousand words
CS 102-02
Lecture 8-1
May 18, 1998
CS102-02
Lecture 8-1
Agenda
• Choices, choices
• Check this out
• Single and double lists
May 18, 1998
CS102-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
CS102-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
(ComboBox)
– List all the alternatives
May 18, 1998
CS102-02
Lecture 8-1
Building a Choice
• Click 'n choose
setLayout(null);
setSize(278,173);
setFont(new Font("Dialog",
Font.PLAIN, 24));
destruction =
new java.awt.Choice();
destruction.addItem("Asteroid");
destruction.addItem("Earthquake");
destruction.addItem("Fire");
destruction.addItem("Flood");
destruction.addItem("Godzilla");
destruction.addItem("Twister");
add(destruction);
destruction.setBounds(24,48,180,48);
May 18, 1998
CS102-02
Lecture 8-1
Variations on a Theme
destruction.setFont(new Font("Dialog", Font.ITALIC, 24));
destruction.setForeground(new Color(16711680));
destruction.setBackground(new Color(255));
May 18, 1998
CS102-02
Lecture 8-1
Common Choice Methods I
• add(String)
Adds an item to this Choice menu.
• getItem(int)
Gets the string at the specified index in this
Choice menu.
• getItemCount()
Returns the number of items in this Choice menu.
• getSelectedIndex()
Returns the index of the currently selected item.
• getSelectedItem()
Gets a representation of the current choice as a
string.
• getSelectedObjects()
Returns an array (length 1) containing the
currently selected item.
May 18, 1998
CS102-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
• removeAll()
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
CS102-02
Lecture 8-1
A Choice Applet
public class MyChoice extends Applet {
private Choice fonts;
private TextField t;
public void init() {
// Create a Choice button and add items to it
fonts = new Choice();
fonts.add( "Monospaced" ); // Courier
fonts.add( "SansSerif" );
// Helvetica
fonts.add( "Serif" );
// Times
t = new TextField( fonts.getItem( 0 ), 30 );
t.setEditable( false );
t.setFont( new Font( fonts.getItem( 0 ),
Font.PLAIN, 12 ) );
fonts.addItemListener( new
FontNameHandler( t ) );
fonts.addItemListener( new
SetTextFieldHandler( t ) );
}
}
add( fonts );
add( t );
May 18, 1998
CS102-02
Lecture 8-1
The Handler
class SetTextFieldHandler implements ItemListener {
private TextField field;
public SetTextFieldHandler( TextField t ) {
field = t;
}
public void itemStateChanged( ItemEvent e ) {
Choice choice = (Choice) e.getItemSelectable();
}
field.setText( "Index: " +
choice.getSelectedIndex() +
"; Font: " + e.getItem() );
}
May 18, 1998
CS102-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
CheckboxGroup and you get radio buttons
May 18, 1998
CS102-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
CS102-02
Lecture 8-1
Radio Buttons Galore
• Use a CheckboxGroup to treat related
Checkboxes as radio buttons
– Create a CheckboxGroup
– Create a Checkbox
– Add the Checkbox to the group
• CheckboxGroup is NOT a Component
May 18, 1998
CS102-02
Lecture 8-1
Radio Buttons from Checkboxes
testGroup = new CheckboxGroup();
adventureRadioButton = new
java.awt.Checkbox("Adventure", testGroup, false);
adventureRadioButton.setBackground(new Color(12632256));
add(adventureRadioButton);
actionRadioButton = new
java.awt.Checkbox("Body count action", testGroup, false);
actionRadioButton.setBackground(new Color(12632256));
add(actionRadioButton);
comedyRadioButton = new
java.awt.Checkbox("Buddy-cop comedy", testGroup, false);
comedyRadioButton.setBackground(new Color(12632256));
add(comedyRadioButton);
dramaRadioButton = new
java.awt.Checkbox("Tear-jerking drama", testGroup, false);
dramaRadioButton.setBackground(new Color(12632256));
add(dramaRadioButton);
May 18, 1998
CS102-02
Lecture 8-1
What You See
May 18, 1998
CS102-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 TextField?)
– Not as in Figure 10.15
May 18, 1998
CS102-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
CS102-02
Lecture 8-1
List Styles
May 18, 1998
CS102-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
CS102-02
Lecture 8-1
Handy List Methods
• getItem(int)
– Gets the item associated with the specified index.
• getItemCount()
– Gets the number of items in the list.
• getItems()
– Gets the items in the list.
• getRows()
– Get the number of visible lines in this list.
• getSelectedIndex()
– Gets the index of the selected item on the list,
• getSelectedItem()
– Get the selected item on this scrolling list.
May 18, 1998
CS102-02
Lecture 8-1
List Events
• Item is selected or deselected, AWT
sends ItemEvent to the list
• Double-clicks on an item in a scrolling
list, AWT sends ActionEvent 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
CS102-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
CS102-02
Lecture 8-1
Multiple Selection Methods
• getSelectedIndexes()
– Gets the selected indexes on the list.
• getSelectedItems()
– Get the selected items on this scrolling list.
• isIndexSelected(int)
– Determines if the specified item in this scrolling list
is selected.
• isMultipleMode()
– Determines whether this list allows multiple
selections.
May 18, 1998
CS102-02
Lecture 8-1
Using Multiple Selections in Java
public class MyList2 extends Applet implements
ActionListener {
private List colorList, copyList;
private Button copy;
private String colorNames[] = { "Black", "Blue",
"Cyan", "Dark Gray", "Gray", "Green", "Light Gray",
"Magenta", "Orange", "Pink", "Red","White", "Yellow" };
public void init() {
// create a list with 5 items visible
// allow multiple selections
colorList = new List( 5, true );
// add items to the list
for ( int i = 0; i < colorNames.length; i++ )
colorList.add( colorNames[ i ] );
add( colorList );
// create copy button
copy = new Button( "Copy >>>" );
copy.addActionListener( this );
add( copy );
}
// create a list with 5 items visible
// do not allow multiple selections
copyList = new List( 5, false );
add( copyList );
May 18, 1998
CS102-02
Lecture 8-1
Using Multiple Selections in Java
public void actionPerformed( ActionEvent e ) {
String colors[];
// get the selected states
colors = colorList.getSelectedItems();
}
// copy them to copyList
for ( int i = 0; i < colors.length; i++ )
copyList.add( colors[ i ] );
May 18, 1998
CS102-02
Lecture 8-1