Top Banner
Chapter 18 Building the user interface
72

Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Mar 26, 2015

Download

Documents

Nathan Rivera
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Chapter 18Building the user interface

Page 2: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

This chapter discusses

Java’s graphical user interface. Swing: an enhancement of a

library called the Abstract Window Toolkit (AWT).

Event-driven, window-based applications.

Swing as a case study of how large libraries are organized.

Page 3: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

The system interface

The interface and the model are two of the principal components of a software system.

The interface handles interaction with the external world.

When the external world is a person, the interface is called a user interface.

Page 4: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Algorithm-driven interfaces In an algorithm-driven approach,

the application determines exactly what information it needs from the environment, and when it needs it.

The application has access to several streams of data.

A stream is a sequence of bytes.

Page 5: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Algorithm-driven interfaces (cont.) A character stream is a set of ASCII

characters. Otherwise it is a binary stream. Input stream sources can be a user’s

keyboard, a file, another program, an external device, etc.

Output stream targets can be a user’s display screen, a file, another program, an external device, etc.

Page 6: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Java compiler stream

1 input stream 2 output streams

Page 7: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Nim game user interfaceEnter number of Sticks.3Player Player1 takes 2 leaving 1 sticks.Player Player2 takes 1 leaving 0 sticks.Game over. Player 1 won.To Play again, enter 1; to stop enter 01Enter number of sticks.

This game writes output and reads input.

Page 8: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Filters

Where input data comes from and where output data goes often is not important to the application.

Filters read input from a single stream called standard input, write output to an output stream called standard output, and write error messages to a stream called standard error.

Page 9: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Event-driven interfaces

The application is “active” in an algorithm-driven interface; it is “passive” in an event-driven system.

An event-driven system waits for something to happen (an event) in the environment.

event-driven: an input-output model in which the application waits for an event to occur, responds to the event, and waits for the next event.

Page 10: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Event-driven interfaces (cont.) An application with a window-based

interface provides a graphical “control panel” containing a range of options.

Page 11: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Event-driven interfaces (cont.) In a window-based system, we assume that

there is a native windowing system that actually detects events like mouse clicks, mouse movement, key strokes, etc., and manages the display.

Java applications interact with the native windowing system.

Page 12: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Swing

A graphical user interface is made up of components (widgets). windows buttons menus, etc.

Each components has attributes of location, size, shape, color, etc.

Components are defined in a class library contained in the package javax.swing

Page 13: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Page 14: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JComponent abstract class

Page 15: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JComponent abstract class (cont.)

Page 16: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JComponent abstract class (cont.) JComponent is a subclass of the AWT.public Color getForeground ();

public Color getBackground ();

public Point getLocation ();

public Dimension getSize ();

public void Color setForeground(Color fg);

public void Color setBackground(Color bg);

public void Point setLocation(Point p);

public void Dimension setSize(Dimension d;)

Page 17: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JComponent abstract class (cont.) Many of the methods are overloaded.

For instance, there are versions of setLocation and setSize that take two int arguments rather than a Point or Dimension.

Page 18: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JComponent abstract class (cont.) Color, Point, and Dimension are

AWT classes. Instances of Color are immutable. The class Color defines a number

of constant references. i.e. Color.red

Class Dimension encapsulates width and height.

Page 19: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Containers

Objects that contain components are containers.

JComponent is a subclass of Container. component: a distinct element of a

graphical user interface, such as a button, text field, etc.

container: a graphical user interface component that can contain other components.

Page 20: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Containers (cont.)

Page 21: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JPanel

Used as a place to put a collection of other components.

Jpanel p = new Jpanel();

p.add(new Jbutton(“on”));

p.add(new Jbutton(“off”));

Page 22: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Manipulating content Class Container defines an extensive set of

methods for manipulating its content.

public int getComponentCount()The number of Components in this Container.

public Component getComponent (int n)The Component with the specified index.require: 0 <= n < this.getComponentCount()

public void remove (Component comp)Remove the specified Component.

public void remove (int index);Remove the Component with the specified index.require: 0 <= index < this.getComponentCount()

Page 23: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Top-level containers

A top-level container is one that is not contained in any other container. i.e. JApplet, JDialog, JFrame, and JWindow.

Page 24: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JFrame

A window with a title and a border. JFrame is a subclass of

java.awt.Container, not JComponent. It delegates the responsibility of

managing its components to another object--JRootPane.

Page 25: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JRootPane

JRootPane is a JComponent whose principal responsibility is to manage the content of some other container.

It is a composite object, including a content pane. The content pane is usually a JPanel, and is the

working area of the JFrame, excluding title, border, menu.

Components are not added directly to the JFrame, but to the content pane.

Page 26: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JRootPane (cont.)

getContentPane returns a Container.

JFrame f = new JFrame(“A Frame”);

JButton b = new JButton(“Press”);

Container cp = f.getContentPane();

cp.add(b);

JApplet, JDialog, JWindow and JInternalFrame also use JRootPane to manage components.

Page 27: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Page 28: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Peers

JApplet, JDialog, JFrame, JWindow are heavyweight components.

Instances of subclasses of JComponent are lightweight components.

When a heavyweight component is created, the AWT also creates an associated native GUI component called a “peer.” i.e. Creation of JFrame, also creates a “frame peer.”

Page 29: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Peers (cont.)

Peers actually do the work of capturing user input and managing the screen area in which the component is displayed.

Lightweights are implemented completely by Java.

Lightweights are drawn on the space provided by their heavyweight parent containers.

Page 30: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Top-level frame

import javax.swing.*;

public class DisplayFrame {

public static void main (String[] args){

JFrame f = new JFrame (“A Frame”);

f.setSize(300,200);

f.setVisible(true);

}

}

Page 31: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding components

Adding components to the frame won’t cause the component to appear suddenly on the display.

A LayoutManager is an object responsible for positioning and sizing the components in a container.

A LayoutManager is specified in the interface java.awt.LayoutManager.

public LayoutManager getLayout();

public void setLayout (LayoutManager manager);

Page 32: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

LayoutManager implementations FlowLayout: left to right, top to bottom. BorderLayout: “north”, “south”, “east”, “west”,

“center.” GridLayout: two-dimensional grid. CardLayout: one at a time from a “deck” GridBagLayout: vertically and horizontally

according to constraints. BoxLayout: either a single horizontal row or

single vertical column. OverlayLayout: specified components align in the

same place; components are laid out on top of each other.

Page 33: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

FlowLayout

Page 34: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Page 35: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

BorderLayout

Page 36: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Page 37: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

GridLayout

Page 38: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

When the GridLayout is created, we specify that we want a grid of 3 rows and 2 columns. Since there is more than 6 components, GridLayout expands the number of columns, while keeping the number of rows at 3.

Page 39: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Container validity A Container is valid if it does not need

to be laid out. i.e. size is known to the system, and the layout manager knows about all its components.

A Container is invalid if its state is inconsistent with its appearance.

A Container to which a component has been added after it was last laid out is invalid.

Page 40: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Container validity (cont.) Any number of things can cause the

layout manager to lay out the Container. validate explicitly sets the

Container’s valid property to true, and instructs the layout manager to lay out the Container.

isValid returns the value of this property.

public boolean isValid();

public void validate();

Page 41: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Capturing and handling events event: the occurrence of an action,

typically external to the system, that the system is aware of and must respond to.

low-level events: pressing or releasing a key, moving the mouse, pressing a mouse button.

high-level events: selecting an item on the menu, pressing a button, entering text in a field. High-level events usually involve one or

more low-level events.

Page 42: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Some high-level events key event: a keyboard key pressed or

released. mouse event: the mouse is moved or

dragged, a button pressed or released, the mouse cursor enters or exits component.

component event: a component is hidden, shown, resized, or moved.

container event: a component is added to or removed from a container.

window event: a window is opened, closed, iconified, de-iconified, activated, deactivated.

Page 43: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Some high-level events (cont.) focus event: a component gains or

loses focus. action event: a high-level event occurs. adjustment event: a high-level event

occurs representing scrollbar motions. item event: a high-level event occurs

when user selects a checkbox, choice, or list item.

document event: a TextComponent’s content changes.

Page 44: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Java event classes

Page 45: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Java event classes (cont.) The source of an event is

determined with:public Object getSource(); An object that monitors when an event

occurs is called a listener. To be notified of an event, a listener

must register with the event’s source. The relation between a listener and an

event source is the observes relation.

Page 46: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Exampleimport java.awt.*;import javax.swing.*;import java.awt.event.*;

public class OnOffTest {public static void main (String[] args){ OnOffSwitch sw = new OnOffSwitch();}

}

class OnOffSwitch extends JFrame {public OnOffSwitch() { super(“On/Off Switch”); JButton button= new JButton(“On/Off”); Container cp = this.getContentPane(); button.setForground(Color.black); button.setBackground(Color.white); cp.add(button,BorderLayout.Center); this.setSize(300,200); this.setVisible(true);}

}

Page 47: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Example

Page 48: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

EventListener classes

Page 49: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding a listener

There is only one method specified in the interface ActionListener.

public void actionPerformed(ActionEvent e);

class Switcher implements ActionListener {

public void actionPerformed

(ActionEvent e) {…}

}

When the user presses the button, an ActionEvent is generated.

Page 50: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Exampleclass OnOffSwitch extends JFrame {

public OnOffSwitch() { super(“On/Off Switch”); JButton button= new JButton(“On/Off”); Switcher control = new Switcher(); button.addActionListener(control); Container cp = this.getContentPane(); button.setForground(Color.black); button.setBackground(Color.white); cp.add(button,BorderLayout.Center); this.setSize(300,200); this.setVisible(true);}

}

Page 51: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Example (cont.)public void actionPerformed(ActionEvent e){

Component source =

(Component)e.getSource();Color oldForeground =

source.getForegound();source.setForeground

(source.getBackground());source.setBackground(oldForeground);

}

Page 52: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Example (cont.)

Page 53: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Example (cont.)

Since the listener explicitly determines the source of the event, it could handle events from several sources without modification.

class OnOffSwitch extends JFrame {public OnOffSwitch() { super(“On/Off Switch”); JButton button1= new JButton(“On/Off”); JButton button2= new JButton(“On/Off”); Switcher control = new Switcher(); button1.addActionListener(control); button2.addActionListener(control); … }

}

Page 54: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding a window listener We would like to terminate the application

when the user selects the “Close” option from the top-level window menu.

Selecting “Close” generates a WindowEvent in the JFrame, specifically a “window closing” event.

The WindowListener interface is a bit more complicated than the ActionListener interface; it specifies seven methods.

Page 55: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding a window listener (cont.)void windowActiviated (WindowEvent e) Invoked when window becomes the active window.

void windowClosed (WindowEvent e) Invoked when window has been closed.

void windowClosing (WindowEvent e) Invoked when user attempts to close window.

void windowDeactiviated (WindowEvent e) Invoked when window becomes no-longer-active window.

void windowDeiconified (WindowEvent e) Invoked when window changes from minimized to normal.

void windowIconified (WindowEvent e) Invoked when window changes from normal to minimized.

void windowOpened (WindowEvent e) Invoked when window is first made visible.

Page 56: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding a window listener (cont.) To simplify the implementation, Java

provides “event adapter” classes.

class Terminator extends WindowAdapter {

public void windowClosing(WindowEvent e) {

Window w = e.getWindow();

w.dispose();

}

public void windowClosed(WindowEvent e) {

System.exit(0);

}

}

Page 57: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding a window listener (cont.) We must create a Terminator instance

and register it with the top-level JFrame.

public OnOffSwitch() { super(“On/Off Switch”); JButton button= new JButton(“On/Off”); Switcher control = new Switcher(); Terminator arnold = new Terminator(); button.addActionListener(control); this.addWindowListener(arnold); …}

Page 58: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Adding a window listener (cont.) Terminator is an ideal candidate for being

made an anonymous inner class.

this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {

e.getWindow().dispose(); } public void windowClosed(WindowEvent e) {

System.exit(0);

}

}

);

Page 59: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Page 60: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Some class features

Page 61: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Component Background color: public Color getBackground() public void setBackground(Color c) Foreground color: public Color getForeground() public void setForeground(Color c) Location: public Point getLocation() public void setLocation(int x, int y) public void setLocation(Point p) Location on screen: public Point getLocationOnScreen() Size: public Dimension getSize() public void setSize(int height,int width) public void setSize(Dimension d)

Page 62: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Component (cont.) Preferred size: public Dimension getPreferredSize()

Minimum size: public Dimension getMinimumSize()

Parent: public Container getParent()

Enable: public boolean isEnabled() public void setEnabled(boolean enabled) Valid: public boolean isValid() public void validate() public void invalidate()

Page 63: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Component (cont.) Visible and Showing: public boolean isVisible() public boolean isShowing() public void setVisible(boolean visible)

Font: public Font getFont() public void setFont(Font f)

Graphics: public Graphics getGraphics()

Listeners: public void addComponentListener(

ComponentListener listener) public void removeComponentListener(

ComponentListener listener)

Page 64: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Component (cont.)

Listeners (cont.):

public void addFocusListener( FocusListener

listener) public void removeFocusListener(

FocusListener listener) public void addKeyListener( KeyListener

listener) public void removeKeyListener( KeyListener

listener) public void addMouseListener( MouseListener

listener) public void removeMouseListener(

MouseListener listener) public void addMouseListener(

MouseMotionListener listener)

public void removeMouseListener( MouseMotionListener

listener)

Page 65: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Containers

Component Manipulation:

public int getComponentCount() public Component getComponent(int

position) public Component getComponentAt(int x,

int y) public Component getComponent(Point p)

Component Manipulation (cont.):

public Component add(Component component) public Component add(Component component,

int position) public Component add(Component component,

Object constraints) public void remove(Component component) public void remove(int position)

Page 66: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Containers (cont.)

Layout Manager:

public int getLayout() public void setLayout(LayoutManager

manager) Listeners:

public void addContainerListener( ContainerListener

listener) public void removeContainerListener(

ContainerListener listener)

Page 67: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Window public void pack() public void toFront() public void toBack() public void dispose()

Listeners:

public void addWindowListener( WindowListener

listener) public void removeWindowListener(

WindowListener listener)

Page 68: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Frame Title: public String getTitle() public void setTitle(String title)

Resizable: public boolean isResizable() public void setResizable(boolean visible)

Border: public Border getBorder() public void setBorder(Border border)

Ancestors: public JRootPane getRootPane() public Container getTopLevelAncestor()

Transparency: public void setOpaque(boolean isOpaque) public boolean isOpaque()

Page 69: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

JFrame

Components: public Container getContentPane () public JMenuBar getJMenuBar () public JRootPane get JRootPane () public void setContentPane(Container

contentPane) public void setJMenuBar(JMenuBar menubar) Default close operation: public int getDefaultCloseOperation () public void setDefaultCloseOperation (int

operation) One of the following must be passed to

setDefaultCloseOperation. WindowConstants.DO_NOTHING_ON_CLOSE WindowConstants.HIDE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE

Page 70: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

We’ve covered

Building a user interface. Event-driven, graphical user

interfaces. Java’s facilities for user interfaces. Events. Listeners.

Page 71: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Glossary

Page 72: Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.

Glossary (cont.)