SlideShare a Scribd company logo
1 of 46
Download to read offline
JAVA SWING
Created by Apurbo Datta
OVERVIEW:
What is Swing?
Java swing components
How to change TitleBar icon in
Java Swing
Some Java Swing Apps
What is java 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.
 Java Swing provides platform-independent and lightweight
components.
 The javax.swing package provides classes for java swing API
such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, JMenu, JColorChooser etc.
No. Java AWT Java Swing
1)
AWT components are platform-
dependent.
Java swing components are platform-
independent.
2) AWT components are heavyweight. Swing components are lightweight.
3)
AWT doesn't support pluggable look
and feel.
Swing supports pluggable look and
feel.
4 )
AWT provides less components than
Swing.
Swing provides more powerful
components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.
5)
AWT doesn't follows MVC(Model View
Controller) where model represents
data, view represents presentation and
controller acts as an interface between
model and view.
Swing follows MVC.
Difference between AWT and Swing
Hierarchy of Java Swing classes
Commonly used Methods of Component class
Method Description
public void add(Component c) add a component on another component.
public void setSize(int width,int height) sets size of the component.
public void setLayout(LayoutManager m) sets the layout manager for the component.
public void setVisible(boolean b)
sets the visibility of the component. It is by
default false.
Java JButton
Declaration of JButton class.
public class JButton extends AbstractButton implements Access
ible
Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i)
It creates a button with the specified icon
object.
Commonly used Constructors of JButton:
Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b)
It is used to set the specified Icon on the
button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void addActionListener(ActionListener a)
It is used to add the action listener to this
object.
Commonly used Methods of AbstractButton class:
Example of Java JButton
Java JPasswordField
The object of a JPasswordField class is a text component specialized for password entry. It allows the
editing of a single line of text. It inherits JTextField class.
JPasswordField class declaration
public class JPasswordField extends JTextField
Constructor Description
JPasswordField()
Constructs a new JPasswordField, with a default
document, null starting text string, and 0 column
width.
JPasswordField(int columns)
Constructs a new empty JPasswordField with the
specified number of columns.
JPasswordField(String text)
Constructs a new JPasswordField initialized with
the specified text.
JPasswordField(String text, int columns)
Construct a new JPasswordField initialized with
Commonly used Constructors JPasswordField:
Java JPasswordField Example Java JPasswordField Example with ActionListener
The object of JLabel class is a component for placing text in a container. It is
used to display a single line of read only text. The text can be changed by an
application but a user cannot edit it directly. It inherits JComponent class.
Java JLabel
JLabel class declaration
public class JButton extends AbstractButton implements Acc
essible
Constructor Description
JLabel()
Creates a JLabel instance with no image and
with an empty string for the title.
JLabel(String s)
Creates a JLabel instance with the specified
text.
JLabel(Icon i)
Creates a JLabel instance with the specified
image.
JLabel(String s, Icon i, int
horizontalAlignment)
Creates a JLabel instance with the specified
text, image, and horizontal alignment.
Commonly used Constructors of JLabel:
Methods Description
String getText() t returns the text string that a label displays.
void setText(String text)
It defines the single line of text this
component will display.
void setHorizontalAlignment(int alignment)
It sets the alignment of the label's contents
along the X axis.
Icon getIcon()
It returns the graphic image that the label
displays.
int getHorizontalAlignment()
It returns the alignment of the label's contents
along the X axis.
Commonly used Methods of JLabel:
Java JLabel Example
Java JTextField
The object of a JTextField class is a text component that allows the editing of a
single line text. It inherits JTextComponent class.
JTextField class declaration
public class JTextField extends JTextComponent implements SwingCo
nstants
Commonly used Constructors of JTextField :
Constructor Description
JTextField() Creates a new TextField
JTextField(String text)
Creates a new TextField initialized with the
specified text.
JTextField(String text, int columns)
Creates a new TextField initialized with the
specified text and columns.
JTextField(int columns)
Creates a new empty TextField with the
specified number of columns
Commonly used Methods:
Methods Description
void addActionListener(ActionListener l)
It is used to add the specified action listener
to receive action events from this textfield.
Action getAction()
It returns the currently set Action for this
ActionEvent source, or null if no Action is set.
void setFont(Font f) It is used to set the current font.
void removeActionListener(ActionListener l)
It is used to remove the specified action
listener so that it no longer receives action
events from this textfield.
Java JTextField Example
Java JTextArea
The object of a JTextArea class is a multi line region that displays text. It allows the editing
of multiple line text. It inherits JTextComponent class
JTextArea class declaration
public class JTextArea extends JTextComponent
Constructor Description
JTextArea() Creates a text area that displays no text initially.
JTextArea(String s)
Creates a text area that displays specified text
initially.
JTextArea(int row, int column)
Creates a text area with the specified number of
rows and columns that displays no text initially.
JTextArea(String s, int row, int column)
Creates a text area with the specified number of
rows and columns that displays specified text.
Commonly used Constructors of JTextArea:
Commonly used Methods of JTextArea:
Methods Description
void setRows(int rows) It is used to set specified number of rows.
void setColumns(int cols) It is used to set specified number of columns.
void setFont(Font f) It is used to set the specified font.
void insert(String s, int position)
It is used to insert the specified text on the
specified position.
void append(String s)
It is used to append the given text to the end
of the document.
Java JTextArea Example Java JTextArea Example with
ActionListener
Java 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 ".It inherits JToggleButton class.
JCheckBox class declaration
public class JCheckBox extends JToggleButton implements Accessible
Constructor Description
JJCheckBox()
Creates an initially unselected check box
button with no text, no icon.
JChechBox(String s)
Creates an initially unselected check box with
text.
JCheckBox(String text, boolean selected)
Creates a check box with text and specifies
whether or not it is initially selected.
JCheckBox(Action a)
Creates a check box where properties are taken
from the Action supplied.
Methods Description
AccessibleContext getAccessibleContext()
It is used to get the AccessibleContext
associated with this JCheckBox.
protected String paramString()
It returns a string representation of this
JCheckBox.
Commonly used Constructors:
Commonly used Methods:
Java JCheckBox Example with ItemListene
Java JCheckBox Example
The JRadioButton class is used to create a radio button. It is used to choose one option from multiple
options. It is widely used in exam systems or quiz.
 It should be added in ButtonGroup to select one radio button only.
Java JRadioButton
JRadioButton class declaration:
public class JRadioButton extends JToggleButton implements A
ccessible
Commonly used Constructors of JRadioButton:
Constructor Description
JRadioButton()
Creates an unselected radio button with no
text.
JRadioButton(String s)
Creates an unselected radio button with
specified text.
JRadioButton(String s, boolean selected)
Creates a radio button with the specified text
and selected status.
Commonly used Methods:
Methods Description
void setText(String s) It is used to set specified text on button.
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b)
It is used to set the specified Icon on the
button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void addActionListener(ActionListener a)
It is used to add the action listener to this
object.
Java JRadioButton
Example
Java JRadioButton Example with ActionListener
Java JComboBox
The object of Choice class is used to show popup menu of choices. Choice
selected by user is shown on the top of a menu. It inherits JComponent
class.
JComboBox class declaration
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListen
er, Accessible
Constructor Description
JComboBox()
Creates a JComboBox with a default data
model.
JComboBox(Object[] items)
Creates a JComboBox that contains the
elements in the specified array.
JComboBox(Vector<?> items)
Creates a JComboBox that contains the
elements in the specified Vector.
Commonly used Constructors Of JComboBox:
Methods Description
void addItem(Object anObject) It is used to add an item to the item list.
void removeItem(Object anObject) It is used to delete an item to the item list.
void removeAllItems() It is used to remove all the items from the list.
void setEditable(boolean b)
It is used to determine whether the
JComboBox is editable.
void addActionListener(ActionListener a) It is used to add the ActionListener.
void addItemListener(ItemListener i) It is used to add the ItemListener.
Commonly used Methods Of JComboBox :
Java JComboBox
Example
Java JComboBox Example with ActionListene
Java JTable
The JTable class is used to display data in tabular form. It is composed of rows and
columns.
Commonly used Constructors of JTable:
Constructor Description
JTable() Creates a table with empty cells.
JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.
Java JTable Example Java JTable Example with ListSelectionListene
If you select an element in column NAME, name of the element will be
displayed on the console:
Table element selected is: Sachin
Java JList
The object of JList class represents a list of text items. The list of text items can be set
up so that the user can choose either one item or multiple items. It inherits
JComponent class.
JList class declaration
public class JList extends JComponent implements Scrollable, Accessibl
e
Commonly used Constructors of JList :
Constructor Description
JList()
Creates a JList with an empty, read-only,
model.
JList(ary[] listData)
Creates a JList that displays the elements in
the specified array.
JList(ListModel<ary> dataModel)
Creates a JList that displays elements from
the specified, non-null, model.
Commonly used Methods of JList :
Methods Description
Void
addListSelectionListener(ListSelectionListener
listener)
It is used to add a listener to the list, to be
notified each time a change to the selection
occurs.
int getSelectedIndex()
It is used to return the smallest selected cell
index.
ListModel getModel()
It is used to return the data model that holds
a list of items displayed by the JList
component.
void setListData(Object[] listData)
It is used to create a read-only ListModel from
an array of objects.
Java JList Example with ActionListen
Java JList Example
Java JPanel
The JPanel is a simplest container class. It provides space in which an
application can attach any other component. It inherits the JComponents
class.
It doesn't have title bar.
JPanel class declaration
public class JPanel extends JComponent implements Accessible
Commonly used Constructors of JPanel :
Constructor Description
JPanel()
It is used to create a new JPanel with a double
buffer and a flow layout.
JPanel(boolean isDoubleBuffered)
It is used to create a new JPanel with
FlowLayout and the specified buffering
strategy.
JPanel(LayoutManager layout)
It is used to create a new JPanel with the
specified layout manager.
Java JPanel Example
Java JProgressBar
The JProgressBar class is used to display the progress of the task. It inherits
JComponent class.
JProgressBar class declaration
public class JProgressBar extends JComponent implements SwingConstants, Ac
cessible
Commonly used Constructors:
Constructor Description
JProgressBar()
It is used to create a horizontal progress bar but no
string text.
JProgressBar(int min, int max)
It is used to create a horizontal progress bar with
the specified minimum and maximum value.
JProgressBar(int orient)
It is used to create a progress bar with the
specified orientation, it can be either Vertical or
Horizontal by using SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL constants.
JProgressBar(int orient, int min, int max)
It is used to create a progress bar with the
specified orientation, minimum and maximum
Commonly used Methods:
Method Description
void setStringPainted(boolean b) It is used to determine whether string should be displayed.
void setString(String s) It is used to set value to the progress string.
void setOrientation(int orientation)
It is used to set the orientation, it may be either vertical or
horizontal by using SwingConstants.VERTICAL and
SwingConstants.HORIZONTAL constants.
void setValue(int value) It is used to set the current value on the progress bar.
Java JProgressBar Example
Java JSlider
The Java JSlider class is used to create the slider. By using JSlider, a user can select a value
from a specific range.
Commonly used Constructors of JSlider class
Constructor Description
JSlider()
creates a slider with the initial value of 50 and
range of 0 to 100.
JSlider(int orientation)
creates a slider with the specified orientation
set by either JSlider.HORIZONTAL or
JSlider.VERTICAL with the range 0 to 100 and
initial value 50.
JSlider(int min, int max)
creates a horizontal slider using the given min
and max.
JSlider(int min, int max, int value)
creates a horizontal slider using the given min,
max and value.
JSlider(int orientation, int min, int max, int
value)
creates a slider using the given orientation,
min, max and value.
Commonly used Methods of JSlider class
Method Description
public void setMinorTickSpacing(int n)
is used to set the minor tick spacing to the
slider.
public void setMajorTickSpacing(int n)
is used to set the major tick spacing to the
slider.
public void setPaintTicks(boolean b)
is used to determine whether tick marks are
painted.
public void setPaintLabels(boolean b)
is used to determine whether labels are
painted.
public void setPaintTracks(boolean b) is used to determine whether track is painted.
Java JSlider Example
Java JSlider Example: painting ticks
How to change TitleBar icon in Java Swing
Some Java Swing Apps
Online Exam
IP Finder
Calculator

More Related Content

Similar to DSJ_Unit III.pdf (20)

Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
JavaYDL17
JavaYDL17JavaYDL17
JavaYDL17
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 
Java awt
Java awtJava awt
Java awt
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
PDF ON JAVA - JFC CONCEPT
PDF ON JAVA - JFC CONCEPTPDF ON JAVA - JFC CONCEPT
PDF ON JAVA - JFC CONCEPT
 
tL19 awt
tL19 awttL19 awt
tL19 awt
 
Swing
SwingSwing
Swing
 
Swing
SwingSwing
Swing
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
Jp notes
Jp notesJp notes
Jp notes
 
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event HandlingB.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
B.Sc. III(VI Sem) Advance Java Unit3: AWT & Event Handling
 
extragui.ppt
extragui.pptextragui.ppt
extragui.ppt
 
GUI programming
GUI programmingGUI programming
GUI programming
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
Swings
SwingsSwings
Swings
 

More from Arumugam90

Notes for AR.ppt
Notes for AR.pptNotes for AR.ppt
Notes for AR.pptArumugam90
 
Unity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdfUnity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdfArumugam90
 
AUGMENTED REALITY.pptx
AUGMENTED REALITY.pptxAUGMENTED REALITY.pptx
AUGMENTED REALITY.pptxArumugam90
 
Introductiontokaryotyping.pptx
Introductiontokaryotyping.pptxIntroductiontokaryotyping.pptx
Introductiontokaryotyping.pptxArumugam90
 
Unit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.pptUnit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.pptArumugam90
 
Unit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdfUnit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdfArumugam90
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.pptArumugam90
 
HTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdfHTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdfArumugam90
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdfArumugam90
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdfArumugam90
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdfArumugam90
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdfArumugam90
 
DSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfDSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfArumugam90
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfArumugam90
 

More from Arumugam90 (20)

Notes for AR.ppt
Notes for AR.pptNotes for AR.ppt
Notes for AR.ppt
 
Unity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdfUnity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdf
 
AUGMENTED REALITY.pptx
AUGMENTED REALITY.pptxAUGMENTED REALITY.pptx
AUGMENTED REALITY.pptx
 
Introductiontokaryotyping.pptx
Introductiontokaryotyping.pptxIntroductiontokaryotyping.pptx
Introductiontokaryotyping.pptx
 
ML
MLML
ML
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
Unit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.pptUnit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.ppt
 
Unit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdfUnit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdf
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Chapter16.ppt
Chapter16.pptChapter16.ppt
Chapter16.ppt
 
Chapter15.ppt
Chapter15.pptChapter15.ppt
Chapter15.ppt
 
HTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdfHTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdf
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
JSP.pdf
JSP.pdfJSP.pdf
JSP.pdf
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
JDBC.pdf
JDBC.pdfJDBC.pdf
JDBC.pdf
 
DSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfDSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdf
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 

Recently uploaded

VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Delhi Call girls
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...shivangimorya083
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...shambhavirathore45
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 

Recently uploaded (20)

VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 

DSJ_Unit III.pdf

  • 1. JAVA SWING Created by Apurbo Datta
  • 2. OVERVIEW: What is Swing? Java swing components How to change TitleBar icon in Java Swing Some Java Swing Apps
  • 3. What is java 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.  Java Swing provides platform-independent and lightweight components.  The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
  • 4. No. Java AWT Java Swing 1) AWT components are platform- dependent. Java swing components are platform- independent. 2) AWT components are heavyweight. Swing components are lightweight. 3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel. 4 ) AWT provides less components than Swing. Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc. 5) AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view. Swing follows MVC. Difference between AWT and Swing
  • 5. Hierarchy of Java Swing classes
  • 6. Commonly used Methods of Component class Method Description public void add(Component c) add a component on another component. public void setSize(int width,int height) sets size of the component. public void setLayout(LayoutManager m) sets the layout manager for the component. public void setVisible(boolean b) sets the visibility of the component. It is by default false.
  • 7. Java JButton Declaration of JButton class. public class JButton extends AbstractButton implements Access ible Constructor Description JButton() It creates a button with no text and icon. JButton(String s) It creates a button with the specified text. JButton(Icon i) It creates a button with the specified icon object. Commonly used Constructors of JButton:
  • 8. Methods Description void setText(String s) It is used to set specified text on button String getText() It is used to return the text of the button. void setEnabled(boolean b) It is used to enable or disable the button. void setIcon(Icon b) It is used to set the specified Icon on the button. Icon getIcon() It is used to get the Icon of the button. void setMnemonic(int a) It is used to set the mnemonic on the button. void addActionListener(ActionListener a) It is used to add the action listener to this object. Commonly used Methods of AbstractButton class:
  • 9. Example of Java JButton
  • 10. Java JPasswordField The object of a JPasswordField class is a text component specialized for password entry. It allows the editing of a single line of text. It inherits JTextField class. JPasswordField class declaration public class JPasswordField extends JTextField Constructor Description JPasswordField() Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width. JPasswordField(int columns) Constructs a new empty JPasswordField with the specified number of columns. JPasswordField(String text) Constructs a new JPasswordField initialized with the specified text. JPasswordField(String text, int columns) Construct a new JPasswordField initialized with Commonly used Constructors JPasswordField:
  • 11. Java JPasswordField Example Java JPasswordField Example with ActionListener
  • 12. The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class. Java JLabel JLabel class declaration public class JButton extends AbstractButton implements Acc essible
  • 13. Constructor Description JLabel() Creates a JLabel instance with no image and with an empty string for the title. JLabel(String s) Creates a JLabel instance with the specified text. JLabel(Icon i) Creates a JLabel instance with the specified image. JLabel(String s, Icon i, int horizontalAlignment) Creates a JLabel instance with the specified text, image, and horizontal alignment. Commonly used Constructors of JLabel:
  • 14. Methods Description String getText() t returns the text string that a label displays. void setText(String text) It defines the single line of text this component will display. void setHorizontalAlignment(int alignment) It sets the alignment of the label's contents along the X axis. Icon getIcon() It returns the graphic image that the label displays. int getHorizontalAlignment() It returns the alignment of the label's contents along the X axis. Commonly used Methods of JLabel:
  • 16. Java JTextField The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class. JTextField class declaration public class JTextField extends JTextComponent implements SwingCo nstants
  • 17. Commonly used Constructors of JTextField : Constructor Description JTextField() Creates a new TextField JTextField(String text) Creates a new TextField initialized with the specified text. JTextField(String text, int columns) Creates a new TextField initialized with the specified text and columns. JTextField(int columns) Creates a new empty TextField with the specified number of columns
  • 18. Commonly used Methods: Methods Description void addActionListener(ActionListener l) It is used to add the specified action listener to receive action events from this textfield. Action getAction() It returns the currently set Action for this ActionEvent source, or null if no Action is set. void setFont(Font f) It is used to set the current font. void removeActionListener(ActionListener l) It is used to remove the specified action listener so that it no longer receives action events from this textfield.
  • 20. Java JTextArea The object of a JTextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits JTextComponent class JTextArea class declaration public class JTextArea extends JTextComponent Constructor Description JTextArea() Creates a text area that displays no text initially. JTextArea(String s) Creates a text area that displays specified text initially. JTextArea(int row, int column) Creates a text area with the specified number of rows and columns that displays no text initially. JTextArea(String s, int row, int column) Creates a text area with the specified number of rows and columns that displays specified text. Commonly used Constructors of JTextArea:
  • 21. Commonly used Methods of JTextArea: Methods Description void setRows(int rows) It is used to set specified number of rows. void setColumns(int cols) It is used to set specified number of columns. void setFont(Font f) It is used to set the specified font. void insert(String s, int position) It is used to insert the specified text on the specified position. void append(String s) It is used to append the given text to the end of the document.
  • 22. Java JTextArea Example Java JTextArea Example with ActionListener
  • 23. Java 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 ".It inherits JToggleButton class. JCheckBox class declaration public class JCheckBox extends JToggleButton implements Accessible
  • 24. Constructor Description JJCheckBox() Creates an initially unselected check box button with no text, no icon. JChechBox(String s) Creates an initially unselected check box with text. JCheckBox(String text, boolean selected) Creates a check box with text and specifies whether or not it is initially selected. JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied. Methods Description AccessibleContext getAccessibleContext() It is used to get the AccessibleContext associated with this JCheckBox. protected String paramString() It returns a string representation of this JCheckBox. Commonly used Constructors: Commonly used Methods:
  • 25. Java JCheckBox Example with ItemListene Java JCheckBox Example
  • 26. The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz.  It should be added in ButtonGroup to select one radio button only. Java JRadioButton JRadioButton class declaration: public class JRadioButton extends JToggleButton implements A ccessible Commonly used Constructors of JRadioButton: Constructor Description JRadioButton() Creates an unselected radio button with no text. JRadioButton(String s) Creates an unselected radio button with specified text. JRadioButton(String s, boolean selected) Creates a radio button with the specified text and selected status.
  • 27. Commonly used Methods: Methods Description void setText(String s) It is used to set specified text on button. String getText() It is used to return the text of the button. void setEnabled(boolean b) It is used to enable or disable the button. void setIcon(Icon b) It is used to set the specified Icon on the button. Icon getIcon() It is used to get the Icon of the button. void setMnemonic(int a) It is used to set the mnemonic on the button. void addActionListener(ActionListener a) It is used to add the action listener to this object.
  • 28. Java JRadioButton Example Java JRadioButton Example with ActionListener
  • 29. Java JComboBox The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class. JComboBox class declaration public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListen er, Accessible Constructor Description JComboBox() Creates a JComboBox with a default data model. JComboBox(Object[] items) Creates a JComboBox that contains the elements in the specified array. JComboBox(Vector<?> items) Creates a JComboBox that contains the elements in the specified Vector. Commonly used Constructors Of JComboBox:
  • 30. Methods Description void addItem(Object anObject) It is used to add an item to the item list. void removeItem(Object anObject) It is used to delete an item to the item list. void removeAllItems() It is used to remove all the items from the list. void setEditable(boolean b) It is used to determine whether the JComboBox is editable. void addActionListener(ActionListener a) It is used to add the ActionListener. void addItemListener(ItemListener i) It is used to add the ItemListener. Commonly used Methods Of JComboBox :
  • 31. Java JComboBox Example Java JComboBox Example with ActionListene
  • 32. Java JTable The JTable class is used to display data in tabular form. It is composed of rows and columns. Commonly used Constructors of JTable: Constructor Description JTable() Creates a table with empty cells. JTable(Object[][] rows, Object[] columns) Creates a table with the specified data.
  • 33. Java JTable Example Java JTable Example with ListSelectionListene If you select an element in column NAME, name of the element will be displayed on the console: Table element selected is: Sachin
  • 34. Java JList The object of JList class represents a list of text items. The list of text items can be set up so that the user can choose either one item or multiple items. It inherits JComponent class. JList class declaration public class JList extends JComponent implements Scrollable, Accessibl e Commonly used Constructors of JList : Constructor Description JList() Creates a JList with an empty, read-only, model. JList(ary[] listData) Creates a JList that displays the elements in the specified array. JList(ListModel<ary> dataModel) Creates a JList that displays elements from the specified, non-null, model.
  • 35. Commonly used Methods of JList : Methods Description Void addListSelectionListener(ListSelectionListener listener) It is used to add a listener to the list, to be notified each time a change to the selection occurs. int getSelectedIndex() It is used to return the smallest selected cell index. ListModel getModel() It is used to return the data model that holds a list of items displayed by the JList component. void setListData(Object[] listData) It is used to create a read-only ListModel from an array of objects.
  • 36. Java JList Example with ActionListen Java JList Example
  • 37. Java JPanel The JPanel is a simplest container class. It provides space in which an application can attach any other component. It inherits the JComponents class. It doesn't have title bar. JPanel class declaration public class JPanel extends JComponent implements Accessible
  • 38. Commonly used Constructors of JPanel : Constructor Description JPanel() It is used to create a new JPanel with a double buffer and a flow layout. JPanel(boolean isDoubleBuffered) It is used to create a new JPanel with FlowLayout and the specified buffering strategy. JPanel(LayoutManager layout) It is used to create a new JPanel with the specified layout manager. Java JPanel Example
  • 39. Java JProgressBar The JProgressBar class is used to display the progress of the task. It inherits JComponent class. JProgressBar class declaration public class JProgressBar extends JComponent implements SwingConstants, Ac cessible Commonly used Constructors: Constructor Description JProgressBar() It is used to create a horizontal progress bar but no string text. JProgressBar(int min, int max) It is used to create a horizontal progress bar with the specified minimum and maximum value. JProgressBar(int orient) It is used to create a progress bar with the specified orientation, it can be either Vertical or Horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants. JProgressBar(int orient, int min, int max) It is used to create a progress bar with the specified orientation, minimum and maximum
  • 40. Commonly used Methods: Method Description void setStringPainted(boolean b) It is used to determine whether string should be displayed. void setString(String s) It is used to set value to the progress string. void setOrientation(int orientation) It is used to set the orientation, it may be either vertical or horizontal by using SwingConstants.VERTICAL and SwingConstants.HORIZONTAL constants. void setValue(int value) It is used to set the current value on the progress bar. Java JProgressBar Example
  • 41. Java JSlider The Java JSlider class is used to create the slider. By using JSlider, a user can select a value from a specific range. Commonly used Constructors of JSlider class Constructor Description JSlider() creates a slider with the initial value of 50 and range of 0 to 100. JSlider(int orientation) creates a slider with the specified orientation set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and initial value 50. JSlider(int min, int max) creates a horizontal slider using the given min and max. JSlider(int min, int max, int value) creates a horizontal slider using the given min, max and value. JSlider(int orientation, int min, int max, int value) creates a slider using the given orientation, min, max and value.
  • 42. Commonly used Methods of JSlider class Method Description public void setMinorTickSpacing(int n) is used to set the minor tick spacing to the slider. public void setMajorTickSpacing(int n) is used to set the major tick spacing to the slider. public void setPaintTicks(boolean b) is used to determine whether tick marks are painted. public void setPaintLabels(boolean b) is used to determine whether labels are painted. public void setPaintTracks(boolean b) is used to determine whether track is painted. Java JSlider Example Java JSlider Example: painting ticks
  • 43. How to change TitleBar icon in Java Swing
  • 44.
  • 45. Some Java Swing Apps Online Exam