class presentation swings
TRANSCRIPT
-
8/3/2019 Class Presentation Swings
1/72
SWING
NIRAIMATHI.S
ASSISTANT PROFESSOR
P.G.DEPT OF COMPUTER APPLICATIONS
N.G.M COLLEGE
POLLACHI
Swing Components, Containers,MVC, Event handling
-
8/3/2019 Class Presentation Swings
2/72
Overview of JFC/Swing Packages
javax.swing.plaf
javax.swing.plaf.basic
javax.swing.plaf.metal
javax.swing.plaf.multi
javax.swing.text
javax.swing.text.html
javax.swing.text.html.parser
javax.swing.text.rtf
javax.swingjavax.swing.table
javax.swing.tree
javax.swing.border
javax.swing.colorchooser
javax.swing.filechooser
javax.swing.event
javax.swing.undo
-
8/3/2019 Class Presentation Swings
3/72
Overview of the Overview
Overview of JFC/Swing PackagesOverview of JFC/Swing Packages
javax.swing.plaf
javax.swing.plaf.basic
javax.swing.plaf.metal
javax.swing.plaf.multi
javax.swing.text
javax.swing.text.html
javax.swing.text.html.parser
javax.swing.text.rtf
javax.swing
javax.swing.table
javax.swing.tree
javax.swing.border j avax.swing.co lorchooser
javax.swing.fi lechooser
javax.swing.event
j avax.swing.undo
Text-based widgets(including html/rtf display) New event packages
Components,
includingaggregateor complex
components
Packages tocontrol the
look and feelof Swing
-
8/3/2019 Class Presentation Swings
4/72
-
8/3/2019 Class Presentation Swings
5/72
Swing Components
CIS 068
Top Level Containers
General Purpose Containers Special Purpose Containers
Basic Controls Uneditable Information Displays Interactive Displays of Highly Formatted
Information
-
8/3/2019 Class Presentation Swings
6/72
Swing Components
CIS 068
Top Level Containers
Your application usually extends one of these classes !
-
8/3/2019 Class Presentation Swings
7/72
Swing Components
General Purpose Containers
CIS 068
-
8/3/2019 Class Presentation Swings
8/72
Swing Components
General Purpose Containerstypically used to collect Basic Controls(JButton, JChoiceBox)
Added to layout of top-level containers
CIS 068
JPanel
JFrame
-
8/3/2019 Class Presentation Swings
9/72
Swing Components
Special Purpose Containers
CIS 068
-
8/3/2019 Class Presentation Swings
10/72
Swing Components
Basic Controls
CIS 068
-
8/3/2019 Class Presentation Swings
11/72
Swing Components
Basic ControlsUnlike passive containers, controls are theactive part of your GUIRemark: containers arent only passive, they are also active sources of events, eg.
Mouse-events.
Being the visible part of your interface,
controls bring your application to life
Controls are event sources !Objects of your application register to
controls to handle the events
CIS 068
-
8/3/2019 Class Presentation Swings
12/72
Swing Components
Uneditable Information Displays
CIS 068
-
8/3/2019 Class Presentation Swings
13/72
Swing Components
Interactive Displays of Highly FormattedInformation
CIS 068
-
8/3/2019 Class Presentation Swings
14/72
Swing Components
Interactive Displays of Highly FormattedInformation
Define standard interfaces for frequentlyneeded tasks
CIS 068
-
8/3/2019 Class Presentation Swings
15/72
-
8/3/2019 Class Presentation Swings
16/72
-
8/3/2019 Class Presentation Swings
17/72
-
8/3/2019 Class Presentation Swings
18/72
-
8/3/2019 Class Presentation Swings
19/72
-
8/3/2019 Class Presentation Swings
20/72
-
8/3/2019 Class Presentation Swings
21/72
-
8/3/2019 Class Presentation Swings
22/72
-
8/3/2019 Class Presentation Swings
23/72
-
8/3/2019 Class Presentation Swings
24/72
-
8/3/2019 Class Presentation Swings
25/72
-
8/3/2019 Class Presentation Swings
26/72
A JFrame Autopsy
A java.awt.Frame is composed of
a single container--the Frameobject itself
JLayeredPane
JPanel
ContentPane
Menu
A javax.swing.JFrame iscomposed of a transparent
glassPane surface, and aninner JPanel with Contents andMenu
The PopTart/Sandwich Duality
-
8/3/2019 Class Presentation Swings
27/72
rame ass ew
JRootPane
JMenuBar
JLayeredPane
JFrame
Container
Component
ContentPane
GlassPane
contains
JComponent
Frame
TheThe JRootPaneJRootPane is a container with ais a container with aJLayeredPaneJLayeredPane (holding the Menu and(holding the Menu and
ContentPaneContentPane) and a Component) and a ComponentGlassPaneGlassPane. It serves as base for. It serves as base for
numerous classes.numerous classes.
AWTAWT
JFCJFC
-
8/3/2019 Class Presentation Swings
28/72
JRootPane: The Content Pane
The JRootPane contains only two components:
the JLayeredPane and
the Component GlassPane
Its layout manager ignores all attempts to add new components.
Instead, one must add to the JRootPanes ContentPane, found inside theJLayeredPane.
A call to getContentPane() returns an instance of the ContentPane.
-
8/3/2019 Class Presentation Swings
29/72
-
8/3/2019 Class Presentation Swings
30/72
-
8/3/2019 Class Presentation Swings
31/72
-
8/3/2019 Class Presentation Swings
32/72
Event Handling
GUIs are event driven Generate events when user interacts with GUI
e.g., moving mouse, pressing button, typing in textfield, etc.
Class java.awt.AWTEventObject
EventObject
AWTEvent
ActionEvent
AdjustmentEvent
ItemEvent
TextEvent
ContainerEvent
FocusEvent
PaintEvent
WindowEvent
InputEvent
MouseWheelEvent
ComponentEvent
KeyEvent MouseEvent
Object
EventObject
AWTEvent
ComponentEvent
TextEvent
ItemEvent
AdjustmentEvent
ActionEvent
WindowEvent
InputEvent
MouseEventKeyEvent
MouseWheelEvent
FocusEvent
PaintEvent
ContainerEvent
-
8/3/2019 Class Presentation Swings
33/72
Event-handling model
Event-handling model Three parts
Event source GUI component with which user interacts
Event object Encapsulates information about event that occurred
Event listener Receives event object when notified, then responds
Programmer must perform two tasks Register event listener for event source
Implement event-handling method (event handler)
-
8/3/2019 Class Presentation Swings
34/72
Event-handling model
interfaceEventListener
interfaceActionListener
interfaceAdjustmentListener
interfaceComponentListener
interfaceContainerListener
interfaceFocusListener
interfaceItemListener
interfaceKeyListener
interfaceMouseListener
interfaceMouseMotionListener
interfaceTextListener
interfaceWindowListener
interfaceEventListener
interfaceActionListener
interfaceAdjustmentListener
interfaceComponentListener
interfaceContainerListener
interfaceFocusListener
interfaceItemListener
interfaceKeyListener
interface
MouseListener
interfaceMouseMotionListener
interfaceTextListener
interfaceTextListener
C
-
8/3/2019 Class Presentation Swings
35/72
35
Events Components Listener Methods
keyEvent All KeyListener keyTyped()
keyPressed()
keyReleased()
MouseEvent All MouseListener MouseMotionListen
er
mouseClicked()mousePressed()
mouseReleased
()
mouseEntered()
mouseExited()mouseDragged()
mouseMoved()
ActionEvent Jbutton
JCheckBoxMenuItem
JComboBoxJFileChooser
Jlist
JRadioButtonMenuIte
m
JTextField
JToggleButton
ActionListenerActionEvent JButton ActionListener actionPerformed
()
-
8/3/2019 Class Presentation Swings
36/72
36
Events Components Listener Methods
ItemEvent JCheckBoxMenuItem
JComboBoxJRadioButtonMenuItem
JToggleButton
ItemListener itemStateChanged()
MenuEvent JMenu MenuListener menuCanceled()
menuDeselected()
menuSelected()
WindowEvent JDialog
JFrame
JWindow
WindowListener windowOpened()
windowClosing()
windowClosed()
windowIconified()
windowDeiconified()
windowActivated()
windowDeactivated()
AdjustmentEven
t
JScrollBar AdjustmentListener adjustmentValueChangd()
-
8/3/2019 Class Presentation Swings
37/72
Event Listeners(interfaces)
Event Adapters(inheritance)
Costs Benefits
Must code allmethods; wasteful
no-ops result
Uses up singleinheritance
opportunity
Keep allevents in
single class
Good abstraction;override those
methodsyou need
Event Handling Options: How to Decide?
-
8/3/2019 Class Presentation Swings
38/72
Layout Managers
Layout managers Provided for arranging GUI components
Provide basic layout capabilities
Processes layout details
Programmer can concentrate on basic look and feel
InterfaceLayoutManager
-
8/3/2019 Class Presentation Swings
39/72
Layout manager DescriptionFlowLayout
Default forjava.awt.Applet, java.awt.Panel andjavax.swing.JPanel. Places components sequentially (left toright) in the order they were added. It is also possible to specify the
order of the components by using the Container method add,
which takes a Component and an integer index position as
arguments.BorderLayout Default for the content panes ofJFrames (and other windows) and
JApplets. Arranges the components into five areas: NORTH,SOUTH, EAST, WEST and CENTER.
GridLayout Arranges the components into rows and columns.
-
8/3/2019 Class Presentation Swings
40/72
Additional Layout Managers
Layout Manager DescriptionBoxLayout A layout manager that allows GUI components to be arranged left-to-
right or top-to-bottom in a container. ClassBox
declares a containerwith BoxLayout as its default layout manager and provides static
methods to create a Box with a horizontal or vertical BoxLayout.
GridBagLayout A layout manager similar to GridLayout. Unlike GridLayout, each
component size can vary and components can be added in any order.
-
8/3/2019 Class Presentation Swings
41/72
FlowLayout
FlowLayout Most basic layout manager
GUI components placed in container from left to right
-
8/3/2019 Class Presentation Swings
42/72
BorderLayout
BorderLayout Arranges components into five regions
NORTH (top of container)
SOUTH (bottom of container)
EAST (left of container)
WEST (right of container)
CENTER (center of container)
-
8/3/2019 Class Presentation Swings
43/72
-
8/3/2019 Class Presentation Swings
44/72
GridLayout
GridLayout Divides container into grid of specified row and
columns
Components are added starting at top-left cell
Proceed left-to-right until row is full
-
8/3/2019 Class Presentation Swings
45/72
-
8/3/2019 Class Presentation Swings
46/72
BoxLayout
BoxLayout Arranges GUI components
Horizontally along x-axis
Vertically along y-axis
-
8/3/2019 Class Presentation Swings
47/72
-
8/3/2019 Class Presentation Swings
48/72
GridBagLayout
GridBagLayout flexibleGridBagLayout
Components can vary in size
Components can occupy multiple rows and columns
Components can be added in any order
Uses GridBagConstraints
Specifies how component is placed inGridBagLayout
-
8/3/2019 Class Presentation Swings
49/72
-
8/3/2019 Class Presentation Swings
50/72
CIS 068
Layout Management
Java supplies five commonly used layoutmanagers:
1. BorderLayout
2. BoxLayout
3. FlowLayout
4. GridBagLayout
5. GridLayout
-
8/3/2019 Class Presentation Swings
51/72
CIS 068
Layouts
BorderLayout
Position must be specified, e.g. add (North, myComponent)
-
8/3/2019 Class Presentation Swings
52/72
CIS 068
Layouts
BoxLayout
The BoxLayout class putscomponents in a single row
or column.It respects the componentsrequested maximum sizes.
-
8/3/2019 Class Presentation Swings
53/72
CIS 068
Layouts
FlowLayout
FlowLayout is the default layout manager for every JPanel.It simply lays out components from left to right, starting new
rows if necessary
-
8/3/2019 Class Presentation Swings
54/72
CIS 068
Layouts
GridBagLayout
GridBagLayout is the most sophisticated, flexible layout manager theJava platform provides. If you really want to use it, go tojava.sun.com
-
8/3/2019 Class Presentation Swings
55/72
CIS 068
Layouts
GridLayout
GridLayout simply makes a bunch of components equal in size anddisplays them in the requested number of rows and columns .
-
8/3/2019 Class Presentation Swings
56/72
-
8/3/2019 Class Presentation Swings
57/72
-
8/3/2019 Class Presentation Swings
58/72
-
8/3/2019 Class Presentation Swings
59/72
-
8/3/2019 Class Presentation Swings
60/72
-
8/3/2019 Class Presentation Swings
61/72
-
8/3/2019 Class Presentation Swings
62/72
-
8/3/2019 Class Presentation Swings
63/72
-
8/3/2019 Class Presentation Swings
64/72
CIS 068
The First Swing Program
Example:
The First Swing Program
-
8/3/2019 Class Presentation Swings
65/72
CIS 068
The GUI
Container: JFrame
Layout: BorderLayoutNorth
Center
Components: JLabel JButton, containingan ImageIcon
-
8/3/2019 Class Presentation Swings
66/72
CIS 068
Steps to build a GUI
1. import package
2. set up top level container(e.g. JFrame)
3. apply layout
(e.g. BorderLayout)
4. add components(e.g. Label, Button)
5. REGISTER listeners
6. show it to the world !
h
-
8/3/2019 Class Presentation Swings
67/72
CIS 068
The Source
1. import package
2. set up top level container(e.g. JFrame)
3. apply layout
(e.g. BorderLayout)
4. add components(e.g. Label, Button)
5. REGISTER listeners
6. show it to the world !
-
8/3/2019 Class Presentation Swings
68/72
68
My First Swing ProgramMy First Swing Program
import javax.swing.*;import java.awt.BorderLayout;
public class First {public static void main(String[] args) {
JFrame frame = new JFrame("My First Frame");
// operation to do when the window is closed.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());frame.getContentPane().add(new JLabel("I Love Swing"),
BorderLayout.CENTER);frame.pack();frame.setVisible(true);
}}
-
8/3/2019 Class Presentation Swings
69/72
-
8/3/2019 Class Presentation Swings
70/72
-
8/3/2019 Class Presentation Swings
71/72
-
8/3/2019 Class Presentation Swings
72/72