More Low Level GUI Components
Review Questions for GUIs:
1. Write Java code that creates a panel and makes the layout for the
panel BorderLayout.
2. Write Java code that creates a button. The text on the button should
initially read "off", but when it is clicked it should alternate
between "on" and "off".
3. Add the button you created in #2 to the south region of the panel
you created in #1.
4. Assume that you have a JButton button already created, and that this
button's text is the number of times it has been clicked (currently 0).
Write Java code that handles clicks on this button.
Text Fields
- Allows user to enter a small amount of text
- When the user hits return to indicate that the text field is
complete, an action event is signaled
- Class: JTextField. The constructor takes the number of columns in
the field
- Example: JTextField textField = new JTextField(25); // a text
field with 25 columns
- Use an ActionListener object to handle the event when text is
entered in the text field. You must implement the actionPerformed()
method.
Example:
JTextField textfield = new JTextField(25);
textfield.addActionListener(this); // our class will implement the
ActionListener
// interface
...
frame.getContentPane().add(textfield);
public void actionPerformed(ActionEvent e)
{
String text = textField.getText();
}
Example: Write a program that
processes strings entered in a text field. Update a button's text to
contain the most recently entered string.
Labels
- Class: JLabel
- Use a JLabel to display text and images
- Useful methods:
- void setText(String s)
- String getText()
- Constructors:
- JLabel() // creates a label with no image and with an empty
string for the title
- JLabel(Icon image) // creates a label instance with the
specified image
- JLabel(String text) // creates a label with the specified text
Example: Write an application
which contains a button and a label. The button's text should contain
an integer equal to the number of button clicks, and the label should
contain a label of the form "Number of button clicks: 3".
Check Boxes
- Class: JCheckBox
- Used for yes or no input from the user
- User checks the box by clicking it and unchecks it by clicking it
again
- A check box generates an item event. Use the addItemListener()
method to add a listener to a check box.
- Implement this method in the ItemListener interface:
- public void
itemStateChanged(ItemEvent e)
- The itemStateChanged() method is invoked when an item is selected
or deselected by user.
- If you have multiple check boxes, you may want to have your class
implement the ItemListener interface - you can determine which check
box triggered the item event using the getItemSelectable() method
- Use the ItemEvent method getStateChange() to determine if the
check box is SELECTED or DESELECTED.
javax.swing.JCheckBox
JCheckBox(String label) // the parameter is the label on the
checkbox
JCheckBox(String label, boolean selected) // selected is the initial
state of the checkbox. true = selected, false = not selected
boolean isSelected() // returns state of the checkbox (true =
selected)
void setSelected(boolean state) // sets checkbox to a new state
java.awt.event.ItemEvent
getItemSelectable() // returns the originator of the event
getStateChange() // returns the type of state change (selected or
deselected)
Example:
class GUIEx implements ItemListener
{
...
pepperoniButton.addItemListener(this);
...
public void itemStateChanged(ItemEvent e)
{
Object src = e.getItemSelectable();
if(src == pepperoniButton) { ... // put pepperoni in the
list of included
ingredients}
else if(src == cheeseButton) ...
}
if(e.getStateChange() == ItemEvent.DESELECTED) {// remove item from the
ingredient list}
Example: Write a program with 3
checkboxes. The 3 checkboxes should contain the text "Box 1", "Box 2",
and "Box 3". Include a label for each box which contains "Box n is
selected" or "Box n is not selected", depending on the state of each
box.
Exercise: Write a program that
a
person might use while ordering a pizza. Include 5 checkboxes for 5
different ingredients: pepperoni, cheese, peppers, pineapple, and
onion. Use a label to indicate which toppings will be used for the
pizza.
Radio Buttons
- Checkboxes: If you have 3 boxes, any or all of them can be
checked.
- Radio Buttons: Only one button in a group of buttons can be
selected at any time.
- Constructor: JRadioButton(String label, boolean state) // label =
label on check box, state indicates whether button is initially checked
or not
- When one button in a group is selected/checked, the previously
selected box/button is automatically unchecked.
- Ex: Use radio buttons for user to select pizza size - it only
makes sense to have one option checked.
To create associated radio buttons, e.g. small, medium and large
buttons for pizza size:
1. Construct one object of type ButtonGroup for each group of buttons.
2. Add objects of type JRadioButton to the button group.
3. The button group object deselects the previously selected button
when a new button is clicked.
4. The ButtonGroup object forms a relationship between the states of
the buttons which are added to it. It does NOT replace the need to put
the radio buttons on a panel, content pane, etc. A ButtonGroup object
is not a GUI component, so do not try to add it to a container.
5. A radio button generates an action event when clicked, so implement
actionPerformed() in the ActionListener interface to handle this event.
Example:
JRadioButton smallButton = new JRadioButton("small", false); //
false --> not initially selected
JRadioButton medButton = new JRadioButton("medium", true); // text for
button is "medium"
JRadioButton largeButton = new JRadioButton("large", false);
ButtonGroup group = new ButtonGroup(); // this is how we tie the
3 buttons together, so only one option can be chosen
group.add(smallButton);
group.add(medButton);
group.add(largeButton);
java.util.EventObject
(ancestor of ActionEvent class)
Object getSource() // returns the object on which the event initially
occurred.
Example: Write a program that uses radio buttons to allow the user to
choose a pizza size. Indicate the current size choice on a label.
Borders
- Separate groups of radio buttons from each other using borders
- Place a border on a panel, and then add a group of radio buttons
(or other components) to the panel
- A border can be added to any component that inherits from
JComponent
- Call a static method of BorderFactory to create a border
- Border styles:
- Lowered bevel
- Raised bevel
- Etched
- Line
Example:
JPanel buttonPanel = new JPanel();
buttonPanel.add(smallButton);
buttonPanel.add(mediumButton);
buttonPanel.add(largeButton);
Border etched = BorderFactory.createRaisedBevelBorder();
buttonPanel.setBorder(etched);
Methods
from javax.swing.BorderFactory
static Border createEtchedBorder() // creates a line border with
a 3D effect
static Border createLoweredBevelBorder() // creates a border that
gives the effect of a lowered surface
static Border createRaisedBevelBorder() // creates a border that
gives the effect of a raised surface
static Border createLineBorder(Color color) // creates a simple line
border
Example:
Border border = BorderFactory.createLineBorder(Color.blue);
panel.setBorder(border);
Exercise: Add a border to the
panel containing the buttons that represent the pizza's size in our
last example.