Chapter 12
Basic
Graphical User Interface
Components
is a Container ,
is a Component.
contentPane
JFrame
JRootPane
contentPane
Java I--Copyright © 2000 Tom Hunter
Model-View-Controller
Architecture
Model-View-Controller
—ActionListeners,
—MouseListeners
Controller
Model
… and Multiple
View Controllers
plugged into it.
View
View
flexibility
—Lightweight Notification
> Minimal information is passed. This
kind of notification is used when a
model experiences frequent events.
—Stateful Notification
> The event contains more state
information than just the event source.
—Lightweight Notification
> Example: A JSliderBar, passes many
change events as the Grip is moved. Because
the potential exists for so many events, the
information in each is minimized.
—Stateful Notification
> Example: A JButton, passes just one
action event as the button is pressed, so
more information about the event is passed
with each.
Java I--Copyright © 2000 Tom Hunter
Model-View-Controller Architecture: Controller
Model Interface Used by… Notification
• Set alignment.
myLabel.setHorizontalAlignment( CENTER );
• You don’t need to repaint the label after you change the
alignment, because the change triggers a repaint().
myLabel.setIconTextGap( intGap );
• Integer values allow you set the gap between text and
image.
Java I--Copyright © 2000 Tom Hunter
GUI Design: JLabel
• This is an example of a JLabel that display an Icon, and
that has had its state enabled and disabled:
lbl.setEnabled( true )
• Also, you can make the label have a different icon only
when it is disabled.
b3.setActionCommand("enable");
b3.setActionCommand("enable");
Normal Rollover
button.setIcon(new ImageIcon("open_hand.gif"));
button.setRolloverIcon(new ImageIcon("punch.gif"));
Container c = getContentPane();
JRootPane root = getRootPane();
root.setDefaultButton( def );
c.add( def );
click.doClick( 2000 );
…
public void itemStateChanged( ItemEvent e )
{
if( e.getStateChange() == ItemEvent.SELECTED )
}
• When you create a menu, you may also add a menu item
that is a check box. For this purpose, you would use a:
JCheckBoxMenuItem
ans.setMnemonic( KeyEvent.VK_Y );
ans.setSelected( true );
c.add( ital );
c.add( bold ); The process of
c.add( undr );
collecting
ButtonGroup bg = new ButtonGroup(); JRadioButtons into a
bg.add( ital ); ButtonGroup is
bg.add( bold ); entirely separate from
bg.add( undr ); the creation of the
GUI.
Java I--Copyright © 2000 Tom Hunter
JMenuBar,
JMenu,
JMenuItem
This is a JMenuItem.
printData.add( printDataEntry );
action.add( enterNew );
action.add( printData );
action.add( exit );
bar.add( action );
—ItemSelectable
These listeners have items, of which
zero of more are selectable.
Java I--Copyright © 2000 Tom Hunter
Event Handling Model: Component Events
—input events or
—non-input events.
—FOCUS_LOST events or
—FOCUS_GAINED events.