54
Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They depend on the native system. javax.swing Component Container Panel Window Frame Dialog Object java.lang JApplet java.awt java.applet Applet JDialog JFrame JWindow JComponent Class extends Key Abstract Class implements Interface Package

Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Embed Size (px)

Citation preview

Page 1: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Swing Hierarchy

Swing classes derived from JComponent will be lightweight,

written entirely in Java.

The top-level Swing windows are

heavyweight. They depend on the native

system.

javax.swing

Component

Container

Panel

WindowFrame

Dialog

Object java.lang

JApplet

java.awt

java.applet Applet

JDialog

JFrame

JWindow

JComponent

Class

extends

Key

Abstract Class

implements

Interface

Package

Page 2: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Swing Programs

• Four basic types of Top Level Window– JFrame, a top level window decorated like a native

window– JWindow, an undecorated stand-alone window (splash-

screen)– JApplet, an embeddable applet– JDialog, a popup dialog window

• Each program type is implemented as a framework class

Page 3: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Swing Hierarchy (Part II)

Swing components names start with ‘J’.

JButton

JMenuItem

Class

extends

Key

Abstract Class

implements

Interface

Package

Objectjava.lang

JComponent

java.awt

Component

Container

avax.swing

AbstractButton

JToggleButton

JMenu

JLabel

JTextComponent

JTextField

JTextArea

JPasswordField

JCheckbox

JRadioButton

JPopupMenu

JPanel

JMenuBar

JScrollPane

JList

JOptionPane

Page 4: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Event Classes

Components Events DescriptionButton, JButton ActionEvent User clicked buttonCheckBox, JCheckBox ItemEvent User toggled a checkboxCheckboxMenuItem, JCheckboxMenuItem ItemEvent User toggled a checkboxChoice, JPopupMenu ItemEvent User selected a choiceComponent, JComponent ComponentEvent Component was moved or resized  FocusEvent Component acquired or lost focus  KeyEvent User typed a key  MouseEvent User manipulated the mouseContainer, JContainer ContainerEvent Component added/removed from containerList, JList ActionEvent User double-clicked a list item  ItemEvent User clicked a list itemMenu, JMenu ActionEvent User selected menu itemScrollbar, JScrollbar AdjustmentEvent User moved scrollbarTextComponent, JTextComponent TextEvent User edited textTextField, JTextField ActionEvent User typed Enter keyWindow, JWindow WindowEvent User manipulated window

AWT events for each type of component.AWT events for each type of component.

Page 5: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

New Swing Event Classes

Component Events DescriptionJPopupMenu PopupMenuEvent User selected a choiceJComponent AncestorEvent An event occurred in an ancestorJList ListSelectionEvent User double-clicked a list item  ListDataEvent List's contents were changedJMenu MenuEvent User selected menu itemJTextComponent CaretEvent Mouse clicked in text  UndoableEditEvent An undoable edit has occurredJTable TableModelEvent Items added/removed from table  TableColumnModelEvent A table column was movedJTree TreeModelEvent Items added/removed from tree  TreeSelectionEvent User selected a tree node  TreeExpansionEvent User changed tree nodeJWindow WindowEvent User manipulated window

Newly defined Swing eventsNewly defined Swing events..

Page 6: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

GUI Design: Choosing Basic Components Swing objects for input, output, control, Swing objects for input, output, control,

guidance:guidance: GuidanceGuidance: A : A JLabelJLabel displays a short string of displays a short string of

text or an image. It can serve as a prompt. text or an image. It can serve as a prompt. InputInput: A : A JTextFieldJTextField allows editing of a single allows editing of a single

line of text. It can get the user’s input.line of text. It can get the user’s input. OutputOutput: A : A JTextAreaJTextArea allows editing of allows editing of

multiple lines of text. We’ll use it to display multiple lines of text. We’ll use it to display results.results.

ControlControl: A : A JButtonJButton is an action control. By is an action control. By implementing the implementing the ActionListenerActionListener interface we interface we will handle the user's action eventswill handle the user's action events..

Page 7: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Types of Layout Managers

Manager Descriptionjava.awt.BorderLayout Arranges elements along the north, south, east, west, and in the center of the container.java.swing.BoxLayout Arranges elements in a single row or single column.java.awt.CardLayout Arranges elements like a stack of cards, with one visible at a time.java.awt.FlowLayout Arranges elements left to right across the

container.java.awt.GridBagLayout Arranges elements in a grid of variable sized cells (complicated).java.awt.GridLayout Arranges elements into a two-dimensional grid of equally sized cells.java.swing.OverlayLayout Arranges elements on top of each other.

Page 8: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Default Layout Managers

Container Layout ManagerJApplet BorderLayout (on its content pane)JBox BoxLayoutJDialog BorderLayout (on its content pane)JFrame BorderLayout (on its content pane)JPanel FlowLayoutJWindow BorderLayout (on its content pane)

In AWT, the default layout for applets was

FlowLayout.

Top-level windows use BorderLayout

Page 9: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

• JTextField method setFont (inherited by JTextField indirectly from class Component) sets the font of the JTextField to a new Font (package java.awt).

• String passed to the JCheckBox constructor is the checkbox label that appears to the right of the JCheckBox by default.

• When the user clicks a JCheckBox, an ItemEvent occurs. – Handled by an ItemListener object, which must implement method

itemStateChanged. • An ItemListener is registered with method addItemListener.• JCheckBox method isSelected returns true if a JCheckBox is

selected.

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 10: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 11: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 12: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 13: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 14: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 15: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 16: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 17: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 18: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 19: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 20: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 21: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 22: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 23: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 24: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson

Education, Inc. All rights reserved.

Page 25: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Painting

• When a GUI needs to change its visual appearance it performs a paint operation

• Swing components generally repaint themselves as needed

• Painting code executes on the event-dispatching thread– If painting takes a long time, no events will be

handled during that time

Page 26: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Example

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

public class Painting extends JPanel { public Painting() {}

public void paintComponent(Graphics g) {super.paintComponent(g);g.setColor( Color.yellow ); g.fillOval( 10,10,50,50 );g.setColor( Color.black ); g.drawOval( 10,10,50,50 );

}

public static void main( String args[] ) {JFrame win = new JFrame( "Painting" );win.setSize(100, 100);win.getContentPane().add( new Painting() );win.setvisible(true);

}}

Page 27: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

The Graphics Object

• The Graphics object both a context for painting and methods for performing the painting.

• The graphics context consists of state such as the current painting color, the current font, and the current painting area– The color and font are initialized to the foreground color

and font of the component just before the invocation of paintComponent

Page 28: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

The Coordinate System

• Each component has its own integer coordinate system– Ranging from (0, 0) to (width - 1, height - 1)– Each unit represents the size of one pixel

Page 29: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Color

• Combinations of Red, Green, Blue• Each [0, 255]

• Java: new Color(255, 150, 0)

Hokie Orange

Page 30: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Font

• Color and font:• g2.setColor( new Color(r,g,b) );• g2.setFont( new Font(…) );

• new font(“Serif”, Font.BOLD, Font.Italic, 18);

Page 31: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

Re-Paint

• Screen is like a painter’s canvas• All windows paint on the same surface!• Windows don’t “remember” whats under them

• Need to re-paint when portions are newly exposed

• Receive Repaint events• Open, resize, bring to front• When other windows in front move, resize, close

Page 32: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

MVC Paradigm

• The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller.

Users interact with a controller (e.g., buttons), and make changes to the model (e.g., data), which is then reflected in the view (e.g., graph).

A --> 25 %B --> 60 %C --> 15 %

Model

View(s)

Draw Graph

Control

Page 33: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

MVC Relationships

Model View

Controller

MVC Relationships

The Controller has references to both the view and the model; the controller is the base of the triad.

The Model might have a reference to the view, to allow for notification of changes. The model does not know who controls it, however.

The View has a reference to the model, for gathering data. View also has reference to the Controller, to notify of successful updates. (References are minimal, perhaps to a base class, to allow swapping of controls.)

Page 34: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

• Many event-listener interfaces contain multiple methods.

• An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface.

• You extend an adapter class to inherit the default implementation of every method and override only the method(s) you need for event handling.

Page 35: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 36: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

• Interface MouseWheelListener enables applications to respond to the rotation of a mouse wheel.

• Method mouseWheelMoved receives a MouseWheelEvent as its argument.

• Class MouseWheelEvent (a subclass of Mouse-Event) contains methods that enable the event handler to obtain information about the amount of wheel rotation.

Page 37: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 38: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 39: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 40: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 41: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 42: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 43: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 44: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 45: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 46: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 47: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 48: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 49: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

• Class Point (package java.awt) represents an x-y coordinate. – We use objects of this class to store the coordinates of each mouse

drag event.

• Class Graphics is used to draw. • MouseEvent method getPoint obtains the Point

where the event occurred. • Method repaint (inherited from Component) indicates

that a Component should be refreshed on the screen as soon as possible.

Page 50: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 51: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

• Graphics method fillOval draws a solid oval. – Four parameters represent a rectangular area (called the bounding

box) in which the oval is displayed. – The first two are the upper-left x-coordinate and the upper-left y-

coordinate of the rectangular area. – The last two represent the rectangular area’s width and height.

• Method fillOval draws the oval so it touches the middle of each side of the rectangular area.

Page 52: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 53: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 54: Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They

(C) 2010 Pearson Education, Inc. All rights reserved.