swing set

67
Swing Set By Theparit Peerasathien

Upload: perry-avery

Post on 03-Jan-2016

15 views

Category:

Documents


0 download

DESCRIPTION

Swing Set. By Theparit Peerasathien. Features of the Java Foundation Classes. New GUI called swing set Graphic API called 2D graphics Accessibility API Drag and Drop API. Heavyweights & Lightweights component. Heavyweight components (peer) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Swing Set

Swing Set

By

Theparit Peerasathien

Page 2: Swing Set

Features of the Java Foundation Classes

New GUI called swing setGraphic API called 2D graphicsAccessibility APIDrag and Drop API

Page 3: Swing Set

Heavyweights & Lightweights component

Heavyweight components (peer)Associated with its own  native screen

resource Connect with JVM & APISlow

ExampleTop-level swing: JFrame, JDialog, JApplet

Page 4: Swing Set

Heavyweights & Lightweights component

Lightweight componentsborrows" the screen resource of an ancestor

  no native resource of its own

ExampleAll Swing set except Top-level swing

Page 5: Swing Set

How to use Components

Top-level component is a root of every containment hierarchy

All Swing programs have at least one Top-level component

Add Lightweight component to content Panes

Page 6: Swing Set

Swing Set

Top-Level Containrs General-Purpose Containers Special-Purpose ContainersBasic Controls Uneditable Information Displays Interactive Displays of Highly Formatt

ed Information

Page 7: Swing Set

Top-level Components

Types of top-level containersJFramesJDialogsJAppletsJWindow

Page 8: Swing Set

Top-Level Component

Add() components to Content panesetLayout() content paneCalling by

public Container getContentPane();Feature

support for adding a menu bar need to use a content pane

.

Page 9: Swing Set

JFrame

Window with border, title and buttons

Making frames JFrame frame = new JFrame();

Or a extend JFrame class (often better code this way).

Style defined withUIManager.setLookAndFeel(looknfeel);

SwingUtilities.updateComponentTreeUI(frame);

frame.pack();

Page 10: Swing Set

Sample code

Page 11: Swing Set

Specifying Window Decorations

Page 12: Swing Set

Specifying Window Decorations

Page 13: Swing Set

Responding to Window-Closing Events

By default, when the user closes a frame onscreen, the frame is only hidden. register a window listener that handles wind

ow-closing events Use setDefaultCloseOperation method

DO_NOTHING_ON_CLOSE HIDE_ON_CLOSE DISPOSE_ON_CLOSE EXIT_ON_CLOSE

Page 14: Swing Set

JApplet

a subclass of java.applet.Applet Features

top-level Swing container Swing applet has a root pane - support for

adding a menu bar has a single content pane.

Page 15: Swing Set

JApplet

add components to a Swing applet's content pane, not directly to the applet

set the layout manager on a Swing applet's content pane, not directly on the applet

default layout manager for a Swing applet's content pane is BorderLayout

should not put painting code directly in a JApplet object

Page 16: Swing Set

JApplet

import javax.swing.JApplet;import java.awt.*;

public class JAppletTest extends JApplet { public void int() { Container cp = getCntentPane(); cp.setLayout (new FlowLayput); cp.add(new JLabel(“Hello”); }

}

Page 17: Swing Set

JDialog

windows that are more limited than frames To create simple, standard dialogs, you us

e the JOptionPane class. The ProgressMonitor class can put up a di

alog that shows the progress of an operation

JColorChooser and JFileChooser supply standard dialogs for Color chooser and File Chooser

Page 18: Swing Set

JOptionPane

can create and customize several different kinds of dialogs

provides support for laying out standard dialogs, providing icons, specifying the dialog's title and text, and customizing the button text

Page 19: Swing Set

Sample JDialog

//default title and icon JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.");

//custom title, warning icon JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane warning", JOptionPane.WARNING_MESSAGE);

//custom title, custom icon JOptionPane.showMessageDialog(frame, "Eggs aren't supposed to be green.", "Inane custom dialog", JOptionPane.INFORMATION_MESSAGE, icon);

Page 20: Swing Set

JDialog

Page 21: Swing Set

Swing Set

Top-Level Containrs General-Purpose Containers Special-Purpose ContainersBasic Controls Uneditable Information Displays Interactive Displays of Highly Formatt

ed Information

Page 22: Swing Set

JComponents

Except top-level container, All Swing components names begin with “J” descend from the JComponent class

Example JPanel, JButton, JTableExcept JFrame JDialog (Top-level

container)

Page 23: Swing Set

JComponent Features

Tool tips

- When the cursor pauses over the component, the specified string is displayed in a small window

Painting and Border allows you to specify the border that a component displays

around its edges Look and feel

has a corresponding ComponentUI Custom properties

- associate one or more properties (name/object pairs) with any JComponent

Page 24: Swing Set

JComponent Features

Support for Layout setter methods — setPreferredSize, setMinimumSize, setMaxi

mumSize, setAlignmentX, and setAlignmentY Support drag and Drop

provides API to set a component's transfer handler, which is the basis for Swing's drag and drop support

Double buffering Double buffering smooths on-screen painting

Key Bindings components react when the user presses a key on the

keyboard Example when a button has the focus, typing the Space key is

equivalent to a mouse click on the the button

Page 25: Swing Set

General-Purpose Containers

Example PanelScroll paneSplit paneTabbed paneTool bar

Page 26: Swing Set

Sample

Page 27: Swing Set

Panel

JPanel class provides general-purpose containers for lightweight components

By default, panels don't paint anything except for their background

customize their painting In many look and feels (but not GTK+), p

anels are opaque by default You can change a panel's transparency

by invoking setOpaque

Page 28: Swing Set

Setting the Layout Manager

By default, a panel's layout manager is an instance of FlowLayout

Example JPanel p = new JPanel(new BorderLayout())

;

JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));

Page 29: Swing Set

Adding Components

FlowLayoutaFlowPanel.add(aComponent); aFlowPa

nel.add(anotherComponent);

BorderLayoutaBorderPanel.add(aComponent, BorderL

ayout.CENTER); aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END);

Page 30: Swing Set

Example

Page 31: Swing Set

Swing Set

Top-Level Containrs General-Purpose Containers Special-Purpose ContainersBasic Controls Uneditable Information Displays Interactive Displays of Highly Formatt

ed Information

Page 32: Swing Set

Special-Purpose Containers

Intermediate containers that play specific roles in the UI.

Example Internal FrameLayered PaneRoot Pane

Page 33: Swing Set

Special-Purpose Containers

Page 34: Swing Set

Internal Frames

JInternalFrame class you can display a JFrame-like window within another window

add internal frames to a desktop pane The desktop pane is an instance of JDeskt

opPane

Page 35: Swing Set

Example

...//In the constructor of InternalFrameDemo, a JFrame subclass: desktop = new JDesktopPane(); createFrame(); //Create first window setContentPane(desktop); //Make dragging a little faster but perhaps uglier. desktop.setDragMode(JD

esktopPane.OUTLINE_DRAG_MODE);

protected void createFrame() { MyInternalFrame frame = new MyInternalFrame(); frame.setVisible(true); desktop.add(frame); try { frame.setSelected(true); } catch (java.beans.PropertyVetoException e) {}

Page 36: Swing Set

Internal Frames vs. Regular Frames

internal frames is similar in many ways to the code for using regular Swing frames

internal frames have root panes also provides other API, such as pack

Internal frames aren't windows or top-level containers You can programatically iconify or maximize an interna

l frame. You can also specify what icon goes in the internal frame's title bar. You can even specify whether the internal frame has the window decorations to support resizing, iconifying, closing, and maximizing.

Page 37: Swing Set

Layered Panes

Swing container that provides a third dimension for positioning components

specify its depth as an integer. The higher the number, the higher the depth.

If components overlap, components at a higher depth are drawn on top of components at a lower depth

Page 38: Swing Set

Layered Panes

Page 39: Swing Set

Example Code

Page 40: Swing Set

Adding Components and Setting Component Depth

Page 41: Swing Set

Position Depth

Positions are specified with an int between -1 and (n - 1), where n is number of component

the smaller the position number, the higher the component within its depth exception (-1) is deepest too

Page 42: Swing Set

Component's position change

Page 43: Swing Set

Layer pane

Page 44: Swing Set

Root Panes

don't directly create a JRootPane object instantiate JInternalFrame or one of the

top-level Swing containersUse Top-Level Container

getting the content pane setting its layout manager adding Swing components

Page 45: Swing Set

four parts of root pane

The glass pane completely transparent unless you implement the glass pane's paint

Component method so that it does something, and it intercepts input events for the root pane.

The layered pane Serves to position its contents, which consist of the content pane and

the optional menu bar. Can also hold other components in a specified Z order..

The content pane The container of the root pane's visible components, excluding the m

enu bar.. The optional menu bar

The home for the root pane's container's menus. If the container has a menu bar, you generally use the container's setJMenuBar method to put the menu bar in the appropriate place.

Page 46: Swing Set

The Glass Pane

is useful when you want to be able to catch events or paint over an area that already contains one or more components

It contains a check box that lets you set whether the glass pane is "visible" — whether it can get events and paint itself onscreen.

When the glass pane is visible, it blocks all input events from reaching the components

Page 47: Swing Set

Sample Code

Page 48: Swing Set

Swing Set

Top-Level Containrs General-Purpose Containers Special-Purpose ContainersBasic Controls Uneditable Information Displays Interactive Displays of Highly Formatt

ed Information

Page 49: Swing Set

Basic Control Component

Atomic components that exist primarily to get input from the user

show simple state

Page 50: Swing Set

Basic Control Component

Page 51: Swing Set

Swing Set

Top-Level Containrs General-Purpose Containers Special-Purpose ContainersBasic Controls Uneditable Information Displays Interactive Displays of Highly Formatt

ed Information

Page 52: Swing Set

Uneditable Information Displays

Atomic components that exist solely to give the user information

Page 53: Swing Set

Swing Set

Top-Level Containrs General-Purpose Containers Special-Purpose ContainersBasic Controls Uneditable Information Displays Interactive Displays of Highly Formatt

ed Information

Page 54: Swing Set

Interactive Displays of Highly Formatted Information

Atomic components that display highly formatted information

Page 55: Swing Set

Swing Set – Getting Start

Page 56: Swing Set

Model Delegate Architecture

MVC architecture is a well-known design for GUI objects

Classic MVC architecture divides each component into three parts: a model, a view, and a controller

Page 57: Swing Set

Model Delegate Architecture

The model passes its data to view for rendering

The View determines which events are passed to the controller

The Controller updates the model bases on the events received

Page 58: Swing Set

Swing's MVC design

the model part of a component is treated as a separate element just as the MVC design does

But Swing collapses the view and controller parts of each component into a single UI (user-interface) object

Page 59: Swing Set

Why ?

The view and controller parts of a traditional MVC-based component require a tight coupling that is sometimes difficult to achieve in practical terms

For example: Traditional MVC architecture makes it very hard to create a generic controller that doesn't know at design time what kind of view will eventually be used to display it.

Page 60: Swing Set

How the Swing program works

making lightweight and heavyweight components work together

Add Lightweight components to Heavyweight component

To obtain a content pane, you call a Container method named getContentPane().

Page 61: Swing Set

Creating GUI components

Page 62: Swing Set

Laying out an interface

Page 63: Swing Set

Laying out an interface

the first code sequence constructs a JToggleButton object, sets its initial label and its position, and then adds an ActionListener to monitor and handle events

The second sequence uses Swing's BoxLayout manager to lay out the SwingingApplet program's GUI interface. It obtains a Container object named contentPane by calling  the getContentPane()

To set the center alignment of its two buttons, the program calls the setAlignmentX() methods. The applet doesn't have to call setAlignmentY()

Page 64: Swing Set

Event: The heart of the program

Page 65: Swing Set

Event: The heart of the program

The actionPerformed() method is an event handler built around an if . . . else loop

It repeatedly checks to see if the user has clicked the Swing!/Stop! toggle button

Page 66: Swing Set

Sample Major Change in Swing JDK 5.0

Improve default look and feel of Swing If you use the Java look and feel your applicati

on will automatically get a new look, called Ocean

Swing Skins Look and Feel Alloy Look and Feel and L2F's Skinnable Look

and Feel

Implement context popup menus for components added a mechanism to take care of registering the

appropriate listeners and key bindings to JPopupMenu

Page 67: Swing Set

THE END