2.introduction To Swing
2.introduction To Swing
Mr.Ghanawajeer D.J.
1
POINTS TO BE COVERED
What is Swing?
4
DIFFERENCE BETWEEN AWT AND
SWING
AWT SWING
AWT components are platform- Java swing components are
dependent. platform-independent.
AWT components are heavyweight. Swing components are lightweight.
AWT doesn't support pluggable look Swing supports pluggable look and
and feel. feel.
AWT provides less components than Swing provides more powerful
Swing. components such as Tables, Lists,
ScrollPanes, Trees,TabbedPanes etc.
AWT doesn't follows MVC(Model Swing follows MVC.
View Controller) where model
represents data, view represents
presentation and controller acts as an 5
interface between model and view.
FEATURES OF SWING
Light Weight − Swing components are independent of
native Operating System's API as Swing API controls
are rendered mostly using pure JAVA code instead of
underlying operating system calls.
6
FEATURES OF SWING
Highly Customizable − Swing controls can be
customized in a very easy way as visual appearance is
independent of internal representation.
7
MVC ARCHITECTURE
The Model-View-Controller Architecture:
13
MVC ARCHITECTURE
MVC in Swing:
Swing actually makes use of a simplified variant of the
MVC design called the model-delegate . This design
combines the view and the controller object into a single
element that draws the component to the screen and
handles GUI events known as the UI delegate.
14
CONTAINERS
Swing defines two types of containers.
The first are top-level containers: JFrame, JApplet,
JWindow, and JDialog. These containers do not
inherit JComponent. They do, however, inherit the
AWT classes Component and Container.
The second type of containers supported by Swing are
lightweight containers.
Lightweight containers do inherit JComponent. An
example of a lightweight container is JPanel, which is15
a general-purpose container.
THE TOP-LEVEL CONTAINER PANES
Each top-level container defines a set of panes.
At the top of the hierarchy is an instance of
JRootPane.
18
THE TOP-LEVEL CONTAINER PANES
19
ADDING COMPONENTS
All top-level containers have a content pane in which
components are stored. Thus, to add a component to a
frame or applet, you must add it to the frame’s content
pane or applet’s content pane. This is accomplished by
calling add( );
Syntax:
Component add(Component comp)
The content pane can be obtained by calling
getContentPane( ) on a JFrame or JApplet instance.
Syntax:
Container getContentPane( )
It returns a Container reference to the content pane. 20
CLOSING WINDOW
You will usually want the entire application to terminate when its
setDefaultCloseOperation( ).
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window is closed.
JFrame.DISPOSE_ON_CLOSE
21
JFrame.HIDE_ON_CLOSE
JFrame.DO_NOTHING_ON_CLOSE
SWING COMPONENT:
CLASS HIERARCHY
22
JFrame
SWING COMPONENTS
23
JLABEL AND IMAGEICON
JLabel is Swing’s easiest-to-use component.
Constructors:
JLabel(Icon icon)
JLabel(String str)
Swing classes.
JLABEL AND IMAGEICON
The easiest way to obtain an icon is to use the
ImageIcon class.
ImageIcon(String filename) 26
String getText( )
Constructors:
JTextField(int cols)
29
JTextField(String str, int cols)
JTextField(String str)
JTEXTFIELD
Here, str is the string to be initially presented, and cols is
the number of columns in the text field. If no string is
specified, the text field is initially empty. If the number
of columns is not specified, the text field is sized to fit
the specified string.
32
JPASSWORDFIELD
The object of a JPasswordField class is a text component
specialized for password entry.
Constructors:
JPasswordField()
JPasswordField(int columns)
JPasswordField(String text) 33
34
JTEXTAREA
The JTextArea class is used to create a text area. It is a
multiline area that displays the plain text only.
Constructors:
37
JCOMBOBOX
Swing provides a combo box (a combination of a text field
and a drop-down list) through the JComboBox class.
Constructors:
JComboBox()
JComboBox(Object[] items) 38
JComboBox(Vector items)
JCOMBOBOX
Methods:
40
JCOMBOBOX
JComboBox generates an action event when the user
selects an item from the list.
41
JCOMBOBOX EXAMPLE
42
THE SWING BUTTONS
Swing defines four types of buttons: JButton,
JToggleButton, JCheckBox, and JRadioButton.
All are subclasses of the AbstractButton class, which
extends JComponent. Thus, all buttons share a set of
common traits.
AbstractButton contains many methods that allow
you to control the behavior of buttons.
For example, you can define different icons that are
displayed for the button when it is disabled, pressed,
or selected. Another icon can be used as a rollover icon,
which is displayed when the mouse is positioned over a
button. 43
THE SWING BUTTONS
44
ABSTRACTBUTTON COMMON METHODS
Here, di, pi, si, and ri are the icons to be used for the
indicated purpose.
45
ABSTRACTBUTTON COMMON METHODS
47
JBUTTON
The JButton class is used to create a button that have
platform-independent implementation.
Constructors:
50
JTOGGLEBUTTON
A useful variation on the push button is called a toggle
button. A toggle button looks just like a push button,
but it acts differently because it has two states: pushed
and released.
That is, when you press a toggle button, it stays
pressed rather than popping back up as a regular push
button does.
When you press the toggle button a second time, it
releases (pops up).
Therefore, each time a toggle button is pushed, it
toggles between its two states.
51
JTOGGLEBUTTON
JToggleButton implements AbstractButton.
Constructors:
54
JTOGGLEBUTTON
Like JButton, JToggleButton generates an action
event each time it is pressed.
56
JTOGGLEBUTTON EXAMPLE
57
JCHECKBOX
The JCheckBox class is used to create a checkbox. It is
used to turn an option on (true) or off (false). Clicking on
a CheckBox changes its state from "on" to "off" or from
"off" to "on ".
58
JCHECKBOX
Constructors:
61
JCHECKBOX
When the user selects or deselects a check box, an
ItemEvent is generated.
AccessibleContext getAccessibleContext()-It is
used to get the Accessible Context associated with
this JCheckBox.
63
JCHECKBOX EXAMPLE
64
JRADIOBUTTON
The JRadioButton class is used to create a radio
button.
66
JRADIOBUTTON
Constructors:
68
JRADIOBUTTON
Constructors:
69
BUTTONGROUP
The ButtonGroup class can be used to group multiple
buttons so that at a time only one button can be
selected.
70
JRADIOBUTTON
A JRadioButton generates action events, item events,
and change events each time the button selection
changes.
72
JPROGRESSBAR
The JProgressBar class is used to display the progress
of the task.
Constructors:
76
JSCROLLPANE
JScrollPane is a lightweight container that automatically
handles the scrolling of another component.
Constructors:
JScrollPane()
JScrollPane(Component comp)
79
JSCROLLPANE
vsb defines:
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEED
ED
hsb defines:
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWA
YS
ScrollPaneConstants.
HORIZONTAL_SCROLLBAR_AS_NEEDED
80
JSCROLLPANE EXAMPLE
81
JTABLE
The JTable class is used to display the data on two
dimensional in tabular form.
It is composed of rows and columns.
You can drag the cursor on column boundaries to resize
columns.
Constructors:
84
JTABBEDPANE
JTabbedPane encapsulates a tabbed pane.
Constructors:
87
JTABBEDPANE
Tabs are added by calling addTab( ).
Here, name is the name for the tab, and comp is the
component that should be added to the tab.
89
JTREE
A tree is a component that presents a hierarchical
view of data.