GUI Lecture 10
GUI Lecture 10
GUI Revision
F27SB
GENERAL GUIDELINES
User Interface Design
• “User illusion”
– Users focus on intention behind system
– They are not aware of the distinction between
hardware, software, OS, GUI and program
– Interfaces should be designed to promote and
maintain this illusion
User Interface Design
North
South
Border layout
• After interrupt:
– information about current state of program
restored from dedicated memory area
– processing resumes at interrupted instruction
Models of interaction
• advantage
– does not waste time checking for external activity
– if not reliant on external activity then do
something else until event happens
• disadvantage
– event will break flow of processing
Buttons
• Icon for selection by mouse
public JButton(String s)
implements JComponent
• s is the text to be written on the button
• to change text:
setText(String text)
Buttons
ActionEvent
• event class for buttons
• raised when JButton is selected
ActionListener
• listener interface
actionPerformed
• listener method
Buttons
class MyProgram extends JFrame implements ActionListener {
...
private JButton button;
public MyProgram() {
button = new JButton(“Press me!”);
button.addActionListener(this);
}
...
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button) {
// run the appropriate code
} else ...
}
}
PROGRAM STRUCTURE
Guidlines
• Follow OOP principles
• The easier it is to maintain
• Have your program inherit from JFrame if its main functionality is to show
a window on the screen
• If it has action sources, e.g. buttons, have it implement
ActionListener
• This increases cohesion by having the class handle all its own events and
functionality
• Reduces coupling by not having an external class as action listener
Guidlines
• Reduce code duplication
• When creating a lot of labels and buttons that should have the same
look, consider creating a method to do this or create a new class that
inherits from JButton/JLabel.
• Use a method if you need the component only in that class
• Reduces coupling
• Use a class if you need the component throughout a larger project and in
several classes
• Increases coupling slightly but reduces code duplication massively.
Guidlines
Guidlines
DYNAMIC INTERFACE CHANGES
Dynamic interface changes
• Replace parts of the GUI with new elements.
• Remove the old part:
• remove(Container c);
• c.setVisible(false);
• Add the new part:
• add(Container c);
• c.setVisible(true);
• Set visibility of JFrame to trigger redraw (paintComponent):
setVisible(true);
Dynamic interface changes
STATE DIAGRAMS
State and Interaction
• An interactive system can be seen as a
sequence of events and associated actions
– Event: something that indicates
the need for change,
e.g. selecting a button
– Action: something that causes
change, e.g. a listener method
is invoked to respond to event
State and Interaction
• It is usually possible to identify distinct states
• Where a state can be characterised as:
– a configuration: the status of things that may
change, such as variables and components
– valid events/actions for current configuration
• i.e. how change is indicated/what may be
changed and how
State and Interaction
It is useful to think of the entire system as a set
of states. System execution can then be seen as
a series of state transitions:
• Starting from a particular state
1. event occurs
2. action is triggered
3. configuration is changed
4. new state is entered
State Transition Diagrams
Depict the states and transitions in a system
• state:
– box with its name in it
• transition:
– arc from a state to a state
– labelled with "event / action"
Can attach a guard to a state: [guard]
• guard must be true for transition to occur
State Transition Diagrams
A simple example: a light circuit
ON OFF
set switch on/
light goes on
PERSPECTIVES
Things To Consider: Perspectives
The user’s view of a system’s states is not
necessarily the same as a developer’s view:
– the user interprets system behaviour in terms of
their conceptual model of what system is for
– the developer knows about, and thinks about,
underlying programming constructs
– e.g. to use editor, user doesn’t need to know how
interface or file system are implemented
Things To Consider: Perspectives
A developer typically thinks of state in terms of
programming constructs:
– current configuration: variable values;
open files; JFrame Components
– events: Java Events
– actions: methods
• e.g. editor: developer thinks about character
positions in arrays of Strings, file I/O etc.
Form Follows Function
For effective system use, the user always needs to
know what state the system is in
• hidden mode: in some state but no way to tell
which one — avoid hidden modes!
• ensure user always knows current system state
– unambiguous display content
– explicit statement of mode
– e.g. MS Word always indicates current style, font, etc.
FURTHER SWING
Editable text
JTextArea extends
javax.swing.text.JTextComponent
• multiple lines of plain, unformatted text
implements MenuElement
JMenuBar()
• creates a new JMenuBar
setJMenuBar(JMenuBar menubar)
• places menubar at the top of a JFrame
Menus
Also a Swing class for menus:
jmenubar.add(jmenu)
• adds jmenu to jmenubar
Menus
And a Swing class for menu items:
JMenuItem extends AbstractButton
implements MenuElement
JMenuItem(String s)
• creates a new JMenu identified by s
jmenu.add(jmenuitem)
• Adds menu item jmenuitem to jmenu
jmenu.add(submenu)
• Adds menu submenu to jmenu
Menus
File chooser
We don't want to hard code the file paths, so use:
JFileChooser()
• create JFileChooser component for current
directory
• Does not support WindowListener
File chooser
int showOpenDialog(Component parent)
int showSaveDialog(Component parent)
• both return constants:
– APPROVE_OPTION for Open/Save buttons
– CANCEL_OPTION for Cancel button
File getSelectedFile()
• returns selected file from JFileChooser
• The parent can be null or the component
that should be blocked while the chooser is
open.
Dialogs
These dialogs can be created using: