100% found this document useful (1 vote)
138 views

Sharad Institute of Technology Polytecnic, Yadrav: Unit 2 Swings

The document discusses various Swing components used to build graphical user interfaces in Java. It describes key Swing components like JFrame, JApplet, JLabels, JTextFields, JTextAreas, JComboBoxes, JButtons, JCheckboxes and JRadioButtons. It also covers Swing features like platform independence, MVC architecture and differences between AWT and Swing.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
138 views

Sharad Institute of Technology Polytecnic, Yadrav: Unit 2 Swings

The document discusses various Swing components used to build graphical user interfaces in Java. It describes key Swing components like JFrame, JApplet, JLabels, JTextFields, JTextAreas, JComboBoxes, JButtons, JCheckboxes and JRadioButtons. It also covers Swing features like platform independence, MVC architecture and differences between AWT and Swing.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

SHARAD INSTITUTE OF TECHNOLOGY

POLYTECNIC ,YADRAV

UNIT 2

SWINGS

Presented By : Ms. K.S.PATIL


DEPARTMENT OF COMPUTER ENGINEERING
2. Swing

2.1. Introduction to swing : Swing Features, Difference


between AWT and Swing.

2.2. Swing components : Japplet, Jicon and Labels, TextfIelds,


Combo boxes.

2.3. Buttons : The Jbutton, Check boxes, Radio Buttons.

2.4. Advanced swing components : Tabbed panes, Scroll


panes, Trees, Tables, Progress bar, tooltips.

2.5. MVC Architecture.


Introduction to Swing :

• Java Swing is a part of Java Foundation Classes (JFC) that is used to


create window-based applications. It is built on the top of AWT
(Abstract Windowing Toolkit) API and entirely written in java.

• Unlike AWT, Java Swing provides platform-independent and


lightweight components.

• Swing is the most flexible and robust approach.

• The javax.swing package provides classes for java swing API.

• In Swing, classes that represent GUI components have names


beginning with the letter J. such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, Jcombobox etc.
Swing Packages:
Swing is a very large subsystem and makes use of many
packages. These are the packages used by Swing that are given
below:
javax.swing javax.swing.border javax.swing.colorchooser

javax.swing.event javax.swing.filechooser javax.swing.plaf

javax.swing.plaf.basic javax.swing.plaf.metal javax.swing.plaf.multi

javax.swing.plaf.synth javax.swing.table javax.swing.text

javax.swing.text.html javax.swing.text.html.par javax.swing.text.rtf


ser
javax.swing.tree javax.swing.undo
Limitation of AWT :

1. AWT supports limited number of GUI components.

2. The components defined by the AWT are heavy weight


components.

3. The AWT components are developed by using the


platform specific code.

4. The AWT component is converted by the native code of


the operating system.
Swing Features :
1. Platform Independent:
It is platform independent, the swing components that are
used to build the program are not platform specific. It can be used
at any platform and anywhere.

2. Lightweight Components :
Most Swing components are lightweight. That means the
components are not dependent on native peers to render
themselves. Instead, they use simplified graphics primitives to
paint themselves on the screen and can even allow portions to be
transparent.
3. Pluggable Look and Feel :
It has a powerful component that can be extended to
provide the support for the user interface that helps in good look
and feel to the application. Swing support several look and feels.
Swing allows the user to switch look and feel at runtime without
closing the current application. To set the appearance of the applets
and applications to one of the three standard looks : windows,
Motif(Unix), or Metal(the standard Swing look).

4. MVC:
Swing mainly follows the concept of MVC that is Model
View Controller. With the help of this, we can do the changes in
one component without impacting or touching other components.
It is known as loosely coupled architecture as well.
Additional Features of Swings :
Several other features distinguish Swing from the older AWT components:

1. A wide variety of new components, such as tables, trees, sliders,


progress bars, internal frames, and text components.

2. Swing components contain support for replacing their insets with an


arbitrary number of concentric borders.

3. Swing components can have tooltips placed over them. A tooltip is a


textual popup that momentarily appears when the mouse cursor rests
inside the component’s painting region. Tooltips can be used to give
more information about the component in question.

4. There is additional debugging support for the rendering of your own


lightweight Swing components.
Difference between AWT and Swing

AWT SWING

AWT Components are Platform Java swing Components are Platform


Dependent Independent

AWT Does not follow MVC Swing Follows MVC

Lesser Components More powerful components

AWT Does not support pluggable Swing Supports pluggable look and
look and feel feel

AWT Components are Heavyweight swing Components are Lightweight


AWT SWING

AWT components need the java.awt Swing components in Java need the
package. javax.swing package.

AWT components require and Swing components do not occupy as


much memory space as AWT
occupy larger memory space.
components.
2.2. Swing Components :
Components:

• In Swing, Components are derived from the JComponent class where


JComponent provides the functionality that is common to all
components.

• TO display any JComponent on the GUI, it is necessary to add this


component to the container.

• If you do not add these components to container then it will not be


displayed on the GUI.

• There are two important features of the swing components – firstly,


all the component classes begins with the letter J and secondly all
these GUI components are descendent of JComponent class.
Containers:
JComponent inherits the AWT classes Container and Component.
Thus, a Swing component is built on and compatible with an AWT
component. All of Swing’s components are represented by classes
defined within the package javax.swing.

A Container holds a group of components. Thus, a container is a


special type of component that is designed to hold other components.
Thus, all Swing GUIs will have at least one container. Because
containers are components, a container can also hold other containers.

Swing defines two types of containers :


1. Heavy weight container
2. Light weight container
Fig : Swing Class Component Hierarchy
JFrame

• In java, Frame is a standard graphical window.


• The frame can be displayed using the frame class.
• JFrame is a top-level window that can contain borders and
menu bars.
• JFrame extends AWT's Frame class.

Constructor of Frame Class :

1. Frame()

2. Frame(String title)
Methods of Frame class :

Methods Description
void setResizable(boolean resizable) sets frame to resizable.

void setTitle(String title) sets the title of the frame.

void setSize(int width,int height) sets the width and height of a frame.

String getTitle() Obtain the title of the frame.

void setVisible(boolean visible) Sets the frame visible or not.


A Frame can be created by two ways :

1. By extending the Frame class.


2. By creating an instance of a Frame class.
JApplets

• The JApplet is a fundamental swing class. It extends the


Applet class.
• JApplet extends AWT's Applet class.
• It supports the pane for example content pane, glass pane,
and root pane.
• The add method can be used to add the content pane.
• The add method of container class can be used to add the
components on the GUI.
• All the life cycle methods used (such as init(), paint()) in
Applet class.
Icon and Labels :
Icon :
The icons are encapsulated by ImageIcon class.

The constructors of ImageIcon :

1. ImageIcon(String name)
2. ImageIcon(URL url )

The Methods of ImageIcon :

3. int getIconHeight()
4. int getIconwidht()
5. void paintIcon(Component comp, Graphics g, int x, int y)
JLabels :
The instance of Jlabel class creates labels, used to provide
instructions and information.
User can not change this text.

Constructors of Jlabel :
1. Jlabel(ImageIcon obj)
2. JLabel(String text)
3. JLabel(String text, ImageIcon obj, int alignment )

Methods :
4. Void setIcon(ImageIcon obj)
5. ImageIcon getIcon()
6. void setText(String text)
7. String getText()
JTextField :

• The JTextField allows to edit one line of text used as input


component for application.

• The JTextField is derived from JTextComponent class.

The constructors of JTextField :

1. JTextField()
2. JTextField(String text)
3. JTextField(int max_chars)
4. JTextField(String text, int max_chars)
JTextArea :

• JTextArea is used to display plain text in multi-line.

• The AWT’s TextArea deals with scrolling but JTextArea doesn’t


manage scrolling. But a JTextArea can be placed inside a JScrollPane.

The constructors of JTextArea :

1. JTextArea()
2. JTextArea(String text)
3. JTextArea(int rows, int columns)
4. JTextArea(String text, int rows. Int columns)
5. JTextArea(Document doc)
6. JTextArea(Document doc, String text, int rows, int columns)
JCombobox :

• Swing provides JComboBox class that creates a combo box.


• The combo box is combination of text field and a drop
down list.
• By default it shows a drop down list box that allow user to
select one item but user can also type his selection into the
text field.

Constructors of JComboBox :
1. JCombobox()
2. JCombobox(Vector obj)
2.3. Buttons :

1. Jbutton
2. JCheckBox
3. JRadioButton
JButtons :

• The JButton is very much different from AWT Button in many


respects.
• The swing push button is denoted by using JButton class.
• The swing button classes are subclasses of AbstractButton class
• In swing we can associate an icon and/or string with the
JButton class.

Constructors of JButton :

1. JButton(String label)
2. JButton(ImageIcon obj)
3. JButton(String label, ImageIcon obj)
Methods of JButton :

1. void setLabel(String label)


2. String getLabel()
3. void setIcon(ImageIcon obj)
4. ImageIcon getIcon()
JCheckbox :
• JCheckbox is the immediate derived class of JToggleButton
which provides two stated button.
• It is swing version of AWT CheckBox which provides the
facility to insert images also.

Constructors of JCheckBox :

1. JCheckebox(ImageIcon obj)
2. JCheckebox(ImageIcon, boolean state)
3. JCheckebox(String text)
4. JCheckebox(String text, boolean state)
5. JCheckebox(String text, ImageIcon obj)
6. JCheckebox(String text, ImageIcon obj, boolean state)
Methods of JCheckBox :

1. void setText(String text)


2. String getText()
3. void setIcon(ImageIcon obj)
4. ImageIcon getIcon()
5. void setSelected(boolean state)
6. boolean isSelected()
JRadioButton :
• JRadioButton is the immediate derived class of JToggleButton
which provides two stated button.
• It is swing version of AWT RadioButton which provides the
facility to insert images also.

Constructors of JRadioButton :

1. JRadioButton(ImageIcon obj)
2. JRadioButton(ImageIcon, boolean state)
3. JRadioButton(String text)
4. JRadioButton(String text, boolean state)
5. JRadioButton(String text, ImageIcon obj)
6. JRadioButton(String text, ImageIcon obj, boolean state)
• Radio buttons must be configured into a group.
Hence, only one of the radio button can be checked
from a group at any time.
• If user selects a radio button then automatically
previously selected radio button will get deselected.
• So, for this create object of ButtonGroup class with
default constructor.
• Use add() method of ButtonGroup class to add radio
button objects into a group.
Syntax is :
void add(JRadioButton obj)
2.4. Advanced Swing Components :
1. JTabbedPane
2. JScrollPane
3. JTree
4. JTable
5. JProgressBar
6. JToolTip
1. JTabbedPane :

• A tabbed pane is an object of JTabbedPane class.

• JTabbedPane is a component which appears as a group of multiple


tabs, each tab containing a title and a panel as group of components.

• When user select a tab, its contents becomes visible and user can
interact its with the contents.

• Using the addTab method the tabs can be added to the tabbed pane.

• Syntax is,
void addTab(String str, Component comp)
Constructors of JTabbedPane :

1. JTabbedPane( )

2. JTabbedPane(int tabPlacement)

3. JTabbedPane(int tabPlacement, int tabLayoutPolicy)


1. JTabbedPane( ) :

• It creates an empty TabbedPane. When we use the constructor


JTabbedPane( ), it creates the pane without any specification for its
placement. So the tab is placed on its default place that is TOP using
JTabbedPane.TOP.

2. JTabbedPane(int tabPlacement) :

• This constructor creates an empty TabbedPane.


• It provides the privilege to decide the direction or place for the pane.
• The placement of the tab is specified using,
JTabbedPane.TOP,
JTabbedPane.BOTTOM,
JTabbedPane.LEFT, or
JTabbedPane.RIGHT.
3. JTabbedPane(int tabPlacement, int tabLayoutPolicy)

• An empty tabbed pane is created. This provides the privilege to


decide the placement for the tab-pane the same as the
JTabbedPane(int tabPlacement) constructor.

• It also provides facility to decide the tab layout policy. This allows the
programmer to control how the tabs will be displayed. The tab layout
policy is either set to JTabbedPane.SCROLL_TAB_LAYOUT or
JTabbedPane.WRAP_TAB_LAYOUT.
By default, the policy is set toWRAP_TAB_LAYOUT.

• With the policy set to SCROLL_TAB_LAYOUT, the tabs become


scroll-able and a button for scrolling the tabs, left-right or up-down, is
displayed in your tabbed pane.
2. JScrollPane :

• JScrollPane is the class that provides a rectangular


scrollable area in which a component can be viewed.

• Horizontal and/or vertical scrollbar may be provided


if required.

Constructors of JScrollPane :
1. JScrollPane(Component obj)
2. JScrollPane(int vertical, int horizontal)
3. JScrollPane(Component obj, int vertical, int horizontal)
Vertical and horizontal are int constants that defines
type of horizontal and vertical scrollbar defined in
ScrollPaneConstants interface. These constants
are:

1. HORIZONTAL_SCROLLBAR_ALWAYS
2. HORIZONTAL_SCROLLBAR_AS_NEEDED
3. VERTICAL_SCROLLBAR_ALWAYS
4. VERTICAL_SCROLLBAR_AS_NEEDED
Methods of JScrollPane :

1. setColumnHeaderView(Component)
2. setRowHeaderView(Component)
3. setCorner(String key, Component)
4. getCorner(Component)

1. setColumnHeaderView(Component) –
It sets the column header for the scroll pane of the specified
component in the parameters.

2. setRowHeaderView(Component) –
It sets the row header for the scroll pane of the specified component in
the parameters.
3. setCorner(String key, Component) –
It sets the corner for the scroll pane for the specified
component in the parameter. String key parameter is along with
the following:
JScrollPane.UPPER_LEFT_CORNER,
JScrollPane.UPPER_RIGHT_CORNER,
JScrollPane.LOWER_LEFT_CORNER,
JScrollPane.LOWER_RIGHT_CORNER,
JScrollPane.LOWER_LEADING_CORNER,
JScrollPane.LOWER_TRAILING_CORNER,
JScrollPane.UPPER_LEADING_CORNER,
JScrollPane.UPPER_TRAILING_CORNER

4. getCorner(Component) –
It gets the corner for the scroll pane for the specified
component in the parameter.
JTree :

• Tree is a type of component that gives the hierarchical view


of data.
• User can expand or shrink of the tree.
• A JTree is an expandable and collapsible component
containing sub-tree to display.
• In swing the trees are implemented using the JTree class.
This class expands the JComponent.
• JTree is a complex component. It has a 'root node' at the top
most which is a parent for all nodes in the tree.
• A node can have many children nodes. These children
nodes themselves can have further children nodes. If a node
doesn't have any children node, it is called a leaf node.
To create a tree, you will need root node that will be created
by the DefaultMutableTreeNode() method. All objects of
DefaultMutableTreeNode are added in the specifying parent
or child nodes that will be specified by you and root node are
added in the JTree object that will also added on the frame.

1. DefaultMutableTreeNode(Object obj, boolean value):


Using the DefaultMutableTreeNode, it creates a tree node
with no root node, the child of root node, specified by user
object and it allows only children that have to be specified. It
takes boolean types values either 'true' or 'false'. If you will
take 'true' that means children node are allowed otherwise it
will display the IllegalStateException.
Constructors of JTree :

Constructor Description
JTree() Creates a JTree with a sample model.

JTree(Object[] value) Creates a JTree with every element of


the specified array as the child of a new
root node.

JTree(TreeNode root) Creates a JTree with the specified


TreeNode as its root, which displays the
root node.
Methods of JTree :

1. Public TreeModel getModel()–


It displays the model of the tree whose data is
displayed using the Jtree in Java programming language.

2. Public int getRowCount()–


The mentioned function is used to count the number of
rows in the Jtree example. The number of rows also mentions
the number of child nodes that are present under the root node
of the Jtree.

3. Public void
addTreeSelectionListener(TreeSelectionListener)–
Adds a listener in the tree selection in the Jtree panel.
JTable :

• JTable is a component that arranges the data in


rows and columns.
• The JTable class is used to display data in tabular
form.
• The JTable class expands the JComponent class.

Constructors of JTable :

1. JTable(Object data[][], Object colHeads[])


Methods of JTable :

1. addColumn(TableColumn []column) :
adds a column at the end of the JTable.

2. clearSelection() :
Selects all the selected rows and columns.

3. editCellAt(int row, int col) :


edits the intersecting cell of the column number col and row
number row programmatically, if the given indices are valid and the
corresponding cell is editable.

4. setValueAt(Object value, int row, int col) :


Sets the cell value as ‘value’ for the position row, col in the
JTable.
JProgressBar :

• Progress bar is used to display the progress of the task.

• The progress bar fills up as the task reaches it


completion. In addition it shows the percentage of
completion of task, it can also display some text.

• It is normally used while creating a GUI for


downloading or transferring of file.

• JProgressBar uses a BoundedRangeModel as its data


model, and an important property used by a progress
bar is its value.
Constructors of JProgressBar :
1. JProgressBar()
2. JProgressBar(BoundedRangeModel model)
3. JProgressBar(int orient)
4. JProgressBar(int min, int max)
5. JProgressBar(int orient, int min, int max)
Methods of JProgressBar :

1. void setStringPainted(boolean state) :


it is used to determine whether the sting should be displayed.

2. void setString(String s) :
it is used to sets the value of the progress string.

3. void setValue(int value) :


it is used to set the current value on the progress bar.

4. int getMaximum() :
it returns the progress bar’s maximum value.
5. int getMinimum() : 
it returns the progress bar’s minimum value.

6. String getString() :
it get the progress bar’s string representation of
current value.

7. void setMaximum(int n) :
sets the progress bar’s maximum value to the value n.

8. void setMinimum(int n) :
 sets the progress bar’s minimum value to the value n.
JToolTip :
• ToolTip is very important component that shows
a temporary pop-up message when the mouse
pointer stays on a component.
• This pop-up message can be a short description
or labelled information of any component.
• We can add tooltip text to almost all the
components of java swing.
• You can create a tool tip for any JComponent
with setToolTipText() method. This method is
used to set up a tool tip for the component.
void setToolTipText(String text)
When the cursor enters the boundary of that
component a popup appears and text is displayed.

• For example, to add tool tip to PasswordField,


you need to add :
JTextField field;
field.setToolTipText("Enter your Password");  
2.5. MVC Architecture (Model-View-Controller)

• The MVC architecture is the most adopted component


architecture that effectively contains : Model-View-
Controller.

• Any component has three major phases :


1. The information / properties associated with the
component.
2. The way that the component looks when submitted on
the screen.
3. The way that the components reacts to user.

• The MVC architecture of swing specifies how these three


objects (Model, View, Controller) intract.
Model :
• The model defines the state information
associated with the component.
• The model stores the content. It manages the state
and conducts all trasformations on that state.
• The model has no specific knowledge of either its
controllers or its views.
• The system itself maintains links between model
and views when the state change occurs.
• For example- for the button component model
will hold the information about the state of button
such as whether the button is pushed or not.
View :

• The view determines how the component is


displayed on the screen.
• The view displays the contents.
• It manages the visual display of the state
represented by the model.
• So, using view the model can be represented
graphically to the user.
• For example- for the button component the
color, the size of the button will be decided by
the view.
Controller :
• The controller determines how the component
responds or react to the user.
• The controller is for managing the user
interaction with the model.
• It is basically for handling the user input.
• The controller takes care of mouse and
keyboard events.
• For example- for the button component the
reaction of events on the button press will be
decided by the controller.
Model

View Controller

Fig. MVC Design Pattern


Controller :
• The controller determines how the component
responds or react to the user.
• The controller is for managing the user
interaction with the model.
• It is basically for handling the user input.
• The controller takes care of mouse and
keyboard events.
• For example- for the button component the
reaction of events on the button press will be
decided by the controller.
Thank you

You might also like