session 1 - introduction swing
TRANSCRIPT
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 1/34
Slide 1 of 34
Introduction to Swing
Swing Components and MVC Model
Session 1
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 2/34
Slide 2 of 34
Introduction to Swing
Swing MVC Architecture
Container Components
Lightweight Components
Common Methods of Swing Components
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 3/34
Slide 3 of 34
Swing is framework based on the Model-
View-Controller(MVC) architecture as
compared to AWT which was just a toolkit Swing supports a technology called
Plugable-Look-And-Feel
Java Foundation Classes (JFC)
AWT Swing Java2D
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 4/34
Slide 4 of 34
Swing components arelightweight as comparedto AWT components
Swing components arevisually appealing, lookand feel. AWTcomponents alwaysinherit the look and feelof the native platform.
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 5/34
Slide 5 of 34
What is MVC?
Slide 5 of 20
User
Controller
Model
View
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 6/34
Slide 6 of 34
MVC is an object-oriented user interface
design pattern first introduced in SmallTalk
somewhere in the late 1970. The code to maintain the data, its state
The code to represent graphics
The code to control user interactions The MVC Architecture has three distinct
objects
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 7/34
Slide 7 of 34
MODEL: represents all the data and the various states of the
component
VIEW: depicts the graphical part on the screen. It takes the
data and various states from the Model to render thecomponents graphical part
CONTROLLER: responsible for determining whether the
component should react to any input events from input
devices such as keyboard or mouse. The Controller canreceive direct messages from the View, and indirect
messages from the Model
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 8/34
Slide 8 of 34
A Container is mean to contain components.There are two types of Containers, top-level
container and general-purpose containers.
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 9/34
Slide 9 of 34
Glass pane
Layered pane
Content pane
Menu Bar
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 10/34
Slide 10 of 34
A Jframe is a Top-level Container used to
created a GUI-based application. It is
available in the javax.swing package. There are two ways to create Jframe
± Extending the javax.swing.Jframe
±Declaring the javax.swing.JFrame
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 11/34
Slide 11 of 34
An Applet is a Java program which is
meant to run as part of a web-page.
While using Swing, the javax.applet.JApplet class is used to
create an Applet.
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 12/34
Slide 12 of 34
The Jpanel class is both a container and a
component.Jpane component
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 13/34
Slide 13 of 34
The lightweight components render themselveswith pure Java code. They do not depend on
external native language code like C/C++.
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 14/34
Slide 14 of 34
Jlabel is a component to display static text ina GUI-based application. A Jlabel can also
display an icon, or both text and icon
Constructors
Methods
± getText()
± setText(String Text)
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 15/34
Slide 15 of 34
A Jbutton is a rectangular component with a
text or icon as its label, and can fire an event
when cliked with a mouse. Constructors
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 16/34
Slide 16 of 34
Methods
± setText(String text)
± getText()
Event
± mouseClicked
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 17/34
Slide 17 of 34
The JCheckBox component creates acomponent which can have only two states,
either checked or unchecked.
Constructors
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 18/34
Slide 18 of 34
Methods
± isSelected()
± setSelected(boolean state)
Event
± itemStateChanged
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 19/34
Slide 19 of 34
The JRadioButton component creates a
component which can have only two states,
either checked or unchecked. Constructors
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 20/34
Slide 20 of 34
Since the radio buttons are mutually
exclusive, they have to be grouped together,
else they will behave like a JCheckBox. Methods
± isSelected()
±setSelected(boolean state)
Event
± itemStateChanged
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 21/34
Slide 21 of 34
A JTextField component allows to input and
edit a single line of text.
Constructors
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 22/34
Slide 22 of 34
Methods
± getText()
± setText(String text) Events
± focusGained
± focusLost
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 23/34
Slide 23 of 34
A JTextArea component allows to input and
edit a multiple lines of text.
Constructors
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 24/34
Slide 24 of 34
Methods
± setText(String text)
± getText() Events
± inputMethodTextChanged
± mouseClickedDEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 25/34
Slide 25 of 34
A JPasswordField is similar a JTextField in
appearance. When the user types, the
asterisk character (*) is echoed for everycharacter typed.
DEMO
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 26/34
Slide 26 of 34
This method is defined in the class
javax.swing.AbstractButton. Only
components which are derived from
AbstractButton class such as Jbutton,
JCheckBox and so no inherit thisfunctionality.
Syntax:
± setIcon(ImageIcon icon):
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 27/34
Slide 27 of 34
Mnemonics allows one characer in thecomponents label to be underlined. The
component can be either clicked with a
mouse or alternately with the Alt key and the
mnemonic character combination.
Syntax:
± setMnemonic(char ch)
± setMnemonic(int number)
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 28/34
Slide 28 of 34
A Tool Tip can be also used on a component
to comprehend additional information for
user. Syntax:
± setToolTipText(String text)
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 29/34
Slide 29 of 34
The setBackground() method is used to set
the background color of the component
Syntax: ± setBackground(Color color)
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 30/34
Slide 30 of 34
The setForeground() method is used to set
the foreground color of the component
Syntax: ± setForeground(Color color)
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 31/34
Slide 31 of 34
The setFont() method is used to set the
current font of the component. It accepts a
java.awt.Font object as a parameter. Example set font for button OK
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 32/34
Slide 32 of 34
The setVisible method is used to make acomponent either visible or invisible
Syntax:
± setVisible(boolean state)
The setEnable method is used to make a
component either enable or disable. All
components which extends from
javax.swing.Jcomponent inherit this method.
Systax:
± setEnable(boolean state)
8/3/2019 Session 1 - Introduction Swing
http://slidepdf.com/reader/full/session-1-introduction-swing 33/34
Slide 33 of 34
Handling Event
Class event : ActionEvent, ItemEvent
Interface Implements : ActionListenter, Item
Listener
Method override : actionPerformed(),
itemStateChanged()
DEMO