Outline 0024 Spring 2010 17 :: 2 More information about Swing - - PowerPoint PPT Presentation

outline
SMART_READER_LITE
LIVE PREVIEW

Outline 0024 Spring 2010 17 :: 2 More information about Swing - - PowerPoint PPT Presentation

Outline 0024 Spring 2010 17 :: 2 More information about Swing 0024 Spring 2010 17 :: 3 Java Foundation Classes import java.awt.*; import java.awt.event.*; import javax.swing.*; import


slide-1
SLIDE 1
slide-2
SLIDE 2

– 17 :: 2 – 0024 Spring 2010

Outline

slide-3
SLIDE 3

– 17 :: 3 – 0024 Spring 2010

More information about Swing


slide-4
SLIDE 4

– 17 :: 4 – 0024 Spring 2010

Java Foundation Classes

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

slide-5
SLIDE 5

– 17 :: 5 – 0024 Spring 2010

Swing vs. AWT

(c) OʼReilly 1999

slide-6
SLIDE 6

– 17 :: 6 – 0024 Spring 2010

Swing

slide-7
SLIDE 7

– 17 :: 7 – 0024 Spring 2010

Top-level containers

http://java.sun.com/docs/books/tutorial/ui/features/components.html

slide-8
SLIDE 8

– 17 :: 8 – 0024 Spring 2010

Containers

containers

slide-9
SLIDE 9

– 17 :: 9 – 0024 Spring 2010

General-purpose containers

slide-10
SLIDE 10

– 17 :: 10 – 0024 Spring 2010

Basic controls

slide-11
SLIDE 11

– 17 :: 11 – 0024 Spring 2010

Essential Swing components

AWT Swing

slide-12
SLIDE 12

– 17 :: 12 – 0024 Spring 2010

Example 1

pack() causes a window to be sized to fit the preferred size and layouts of its sub- components

slide-13
SLIDE 13

– 17 :: 13 – 0024 Spring 2010

Example 2

In this example how a custom frame is created

slide-14
SLIDE 14

– 17 :: 14 – 0024 Spring 2010

Build from bottom up

JPanel JButton JFrame JLabel

slide-15
SLIDE 15

– 17 :: 15 – 0024 Spring 2010

Layout managers

Left to right, Top to bottom c n s e w FlowLayout GridLayout BorderLayout none, 
 programmer 
 sets x,y,w,h null One at a time CardLayout GridBagLayout JButton Organizing Layout of components in a container

slide-16
SLIDE 16

– 17 :: 16 – 0024 Spring 2010

Combinations

JButton JButton JTextArea

slide-17
SLIDE 17

– 17 :: 17 – 0024 Spring 2010

Combinations

n JPanel: BorderLayout c JFrame JPanel: FlowLayout JButton JButton JTextArea

slide-18
SLIDE 18

– 17 :: 18 – 0024 Spring 2010

Code: null layout

press me

slide-19
SLIDE 19

– 17 :: 19 – 0024 Spring 2010

Code: FlowLayout

press me then me

slide-20
SLIDE 20

– 17 :: 20 – 0024 Spring 2010

import java.awt.*; import javax.swing.*; public class Main { public static void main(String[] args) { JFrame f = new JFrame("title"); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton("press me"); JButton b2 = new JButton("then me"); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } }

slide-21
SLIDE 21

– 17 :: 21 – 0024 Spring 2010

Box Layout

Component.RIGHT_ALIGNMENT Component.CENTER_ALIGNMENT Component.LEFT_ALIGNMENT

myPane.setLayout(new BoxLayout(myPane, BoxLayout.PAGE_AXIS));

slide-22
SLIDE 22

– 17 :: 22 – 0024 Spring 2010

A simple Swing program - Events

  • Objects communicate by “firing” and “handling” events
  • Events are sent from a single source object to one or

more registered listener objects

slide-23
SLIDE 23

– 17 :: 23 – 0024 Spring 2010

Types of event listeners

Act that results in event Listener type

User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener

slide-24
SLIDE 24

– 17 :: 24 – 0024 Spring 2010

Handling events I

JPanel JButton Listener

  • JFrame

JLabel

slide-25
SLIDE 25

– 17 :: 25 – 0024 Spring 2010

Event handling II

slide-26
SLIDE 26

– 17 :: 26 – 0024 Spring 2010

Example using local class

slide-27
SLIDE 27

– 17 :: 27 – 0024 Spring 2010

JDialog

slide-28
SLIDE 28

– 17 :: 28 – 0024 Spring 2010

Dialog Example

The JOptionPane class can be used to create simple modal dialogs (icons, title, text and buttons can be customized).

slide-29
SLIDE 29

– 17 :: 29 – 0024 Spring 2010

Swing and Threads

slide-30
SLIDE 30

– 17 :: 30 – 0024 Spring 2010

Safe Swing code

Safe to update state of Swing components Unsafe to update state of Swing components

slide-31
SLIDE 31

– 17 :: 31 – 0024 Spring 2010

Event Dispatching

javax.swing.SwingUtilities.invokeLater( new Runnable(){ public void run(){ // Access to components } } );

slide-32
SLIDE 32

– 17 :: 32 – 0024 Spring 2010

Handle a job in the background

slide-33
SLIDE 33

– 17 :: 33 – 0024 Spring 2010

Model/View/Controller [MVC]

http://www.itu.dk/courses/VOP/E2005/VOP2005E/8_mvc_krasner_and_pope.pdf

slide-34
SLIDE 34

– 17 :: 34 – 0024 Spring 2010

Model/View/Controller

 Model

 complete, self-contained representation of

  • bject managed by the application


e.g., spreadsheet document

 provides a number of services to

manipulate the data
 e.g., recalculate, save

 computation and persistence issues

 Try to separate the model and its services

so that it is Swing-free

slide-35
SLIDE 35

– 17 :: 35 – 0024 Spring 2010

Model/View/Controller

 View

 tracks what is needed for a particular

perspective of the data
 e.g., bar chart view

 presentation issues

 Controller

 gets input from the user, and uses appropriate

information from the view to modify the model
 e.g., get slider value, trigger chart modify

 interaction issues

 In practice, views and controllers are

implemented with Swing components and listeners

slide-36
SLIDE 36

– 17 :: 36 – 0024 Spring 2010

Model/View/Controller

slide-37
SLIDE 37

– 17 :: 37 – 0024 Spring 2010

MVC

slide-38
SLIDE 38

– 17 :: 38 – 0024 Spring 2010

Pluggable “Look and Feel”

slide-39
SLIDE 39

– 17 :: 39 – 0024 Spring 2010

Pluggable Look-and-Feel

http://java.sun.com/docs/books/tutorial/ui/overview/demo.html

slide-40
SLIDE 40

– 17 :: 40 – 0024 Spring 2010

Extra part

slide-41
SLIDE 41

– 17 :: 41 – 0024 Spring 2010

Applets

JApplet contentPane JButton

slide-42
SLIDE 42

– 17 :: 42 – 0024 Spring 2010

Applet Methods

slide-43
SLIDE 43

– 17 :: 43 – 0024 Spring 2010

Application + Applet

JApplet contentPane JPanel JFrame JButton

  • r

Browser Command line

slide-44
SLIDE 44

– 17 :: 44 – 0024 Spring 2010

Applet Security

slide-45
SLIDE 45

– 17 :: 45 – 0024 Spring 2010

Java 2D API

slide-46
SLIDE 46

– 17 :: 46 – 0024 Spring 2010

Graphics

JButton

slide-47
SLIDE 47

– 17 :: 47 – 0024 Spring 2010

Coordinate System

(0,0) (width,0) (0,height) (width, height)

slide-48
SLIDE 48

– 17 :: 48 – 0024 Spring 2010

Painting Components

JButton JPanel

slide-49
SLIDE 49

– 17 :: 49 – 0024 Spring 2010

Painting in Java

slide-50
SLIDE 50

– 17 :: 50 – 0024 Spring 2010

Graphics Primitives

label

http://java.sun.com/docs/books/tutorial/2d/geometry/primitives.html

slide-51
SLIDE 51

– 17 :: 51 – 0024 Spring 2010

Graphics Attributes

slide-52
SLIDE 52

– 17 :: 52 – 0024 Spring 2010

Code

Hello World

To paint the inside of a component, 


  • verride the paintComponent
slide-53
SLIDE 53

– 17 :: 53 – 0024 Spring 2010

Creating and Drawing to an Image

http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html

slide-54
SLIDE 54

– 17 :: 54 – 0024 Spring 2010

Reading/Loading Image

slide-55
SLIDE 55

– 17 :: 55 – 0024 Spring 2010

How to proceed …