swing - 2 session 13. swing - 2 / 2 of 38 objectives (1) discuss trees and tables discuss progress...

38
Swing - 2 Session 13

Upload: gabriel-casey

Post on 29-Jan-2016

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2

Session 13

Page 2: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 2 of 38

Objectives (1)

Discuss trees and tables Discuss progress bars Discuss MVC architecture

Describe menus

Page 3: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 3 of 38

Objectives (2)

KeyStroke handling, Action Objects, Nested Containers

Virtual Desktops, Compound Borders Drag and Drop Java 2D Customized Dialogs, Standard Dialog classes

Discuss features of Swing such as:

Page 4: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 4 of 38

A Swing menu consists of a menubar, menuitems and menus

Menubar is the root of all menus and menuitems

Menus show a list of items that indicate various tasks

Select or click an option and another list or sub-menu opens up

Menus

Page 5: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 5 of 38

JFC Menus components

Page 6: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 6 of 38

Hierarchy

JMenuBar JPopupMenu JAbstractButton JSeperator

JMenuItem

JMenu JCheckBoxMenuItem JRadioButtonMenuItem

JComponent

Container

Component

Object

Page 7: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 7 of 38

JMenuBar

Consists of a number of JMenus with each JMenu represented as a string within the JMenuBar

JMenuBar requires: ‘SingleSelectionModel’ –

keeps track of the menu currently selected ‘LookAndFeel’ class –

Responsible for drawing the menu bar and responding to events that occur in it

JMenuBar is a component that can be added to a container through a JFrame, JWindow or JInternalFrame’s rootpane.

Page 8: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 8 of 38

JMenu

JMenu has two additional classes: JPopupMenu –

Used to display the JMenu’s menu items LookAndFeel –

Responsible for drawing the menu in the menubar and for responding to all events that occur in it

JMenu is seen as a text string under JMenuBar while it acts as a popup menu when the user clicks on it

Page 9: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 9 of 38

JMenu - Example

Output

Page 10: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 10 of 38

JPopupMenu

Used for the “pull-right” menu It is also used as a shortcut menu, which is

activated by the right click of the mouse The constructors used to create JPopupMenu

are: public JPopupMenu() – creates a JPopupMenu public JPopupMenu(String label) – creates a

JPopupMenu with the specified title

Displays expanded form of menu

Page 11: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 11 of 38

Functions of JPopupMenu

Determines whether the mouse event is the popup trigger for the platform

public boolean isPopupTrigger()

Displays the popup menu at the position (x,y) in the coordinate space of the component “c”

public void show(Component c, int x, int y)

Creates a new menu item with the specified text and appends it to the end of the menu

public JMenuItem add(String s)

Appends the specified menu item at the end of the menu

public JMenuItem add(JMenuItem menuItem)

PurposeMethod

Page 12: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 12 of 38

JPopupMenu – Example (1)

Page 13: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 13 of 38

JPopupMenu – Example (2)

Page 14: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 14 of 38

JPopupMenu – Example (3)

Page 15: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 15 of 38

JPopupMenu – Example (4)

Output

Page 16: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 16 of 38

JMenuItem A component that appears as a text string

possibly with an icon in a JMenu or JPopupMenu

Dialog Box

JMenuItem dissapears

Page 17: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 17 of 38

JMenuItem - Example

Output

Page 18: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 18 of 38

JCheckBoxMenuItem

Checkboxes are created using JCheckBox class

May have a text string and/or an icon When a JCheckBoxMenuItem is clicked and

released, the state of a menu item changes to selected or deselected

Contains checkboxes as its items

Page 19: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 19 of 38

JRadioButtonMenuItem

May have a text string and/or an icon Clicking a selected radio button does not

change its state Clicking an unselected radio button deselects

the earlier selected one

Similar to checkboxes except that only one radio button can be selected at any point of time

Page 20: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 20 of 38

Example (1)

Page 21: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 21 of 38

Example (2)

Page 22: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 22 of 38

Example (3)

Output

Page 23: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 23 of 38

Trees

Windows Explorer has a tree like structure depicting files and folders

Windows Explorer structures can be created in Java using JTree

Every row in the hierarchy is termed as a node By default, the tree displays the root node A node having child nodes is called a branch

node else it is called as a leaf node

A Tree depicts information in a hierarchical, vertical manner

Page 24: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 24 of 38

JTree – Example (1)

Page 25: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 25 of 38

JTree – Example (2)Output

When the nodes are clicked

Page 26: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 26 of 38

Tables

Useful in storing numerical data Certain computer applications require tables

to display data allowing the user to edit them JTable class in Swing enables us to create

tables JTable does not store data, it only provides a

view of it

Volumes of data are better maintained in a tabular format than as a list

Page 27: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 27 of 38

JTable - Example

Output

Page 28: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 28 of 38

Progress Bars

A better way to do the same is by using charts or bar graphs

Certain programs provide a graphical indication of a task that is currently under progress

Swing uses JProgressBar class to implement the same

Monitoring the progress of any activity can be done by using percentage figures

Page 29: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 29 of 38

Progress Bars – Example (1)

Page 30: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 30 of 38

Progress Bars – Example (2)

Output

Page 31: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 31 of 38

MVC

To achieve this, Swing components work within the MVC model

MVC model stands for: Model View Controller

Swing helps to specify the look and feel, such as the Java look and feel, Windows look and feel

Page 32: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 32 of 38

Interaction between MVC

Model

Controller

View

View reads the contents

Informs view of state change

Informs the view to update visuals

Up

date

th

e c

on

ten

ts

Page 33: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 33 of 38

Characteristics common to Swing Components

Visual appearance - determines what the component should look like

Behavior - decides how the component should respond to events

Content - decides on the state of the component

Page 34: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 34 of 38

MVC classes

View – Responsible for showing the component on the basis of the state

Controller – Handles the event Interaction between the classes are

controlled by the MVC pattern Each class has necessary methods defined

within them

Model – Stores the data or the state of the object

Page 35: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 35 of 38

MVC - Example

ControllerEvent notification

ModelView

Calls necessary files to make changes

Informs about the change of state

Redraws the component

Page 36: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 36 of 38

Swing features (1)

Since the fundamental class JComponent contains a RootPane, any component can be nested within another

KeyStrokeHandling, ActionObjects, Nested Containers Keystrokes can be captured by creating a

KeyStroke object

Action interface extends ActionListener specifying an enabled property as well as properties for text descriptions

Page 37: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 37 of 38

Swing Features (2)

Facilitates transfer of data between Java applications as well as to native applications

Virtual Desktops and Compound Borders JDesktopPane and JInternalFrame classes can be

used to create a virtual desktop Compound borders are created by combining

various border styles

Drag and Drop

Page 38: Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus

Swing - 2 / 38 of 38

Swing Features (3)

Using Java 2D API, one can: Create lines of any thickness Use different gradients and textures to fill shapes Move, rotate, scale and trim text and graphics Combine overlapping text and graphics

Customized Dialogs and Standard Dialog classes Makes use of JOptionPane class Standard dialog classes available are:

JFileChooser JColorChooser

Java 2D