SlideShare a Scribd company logo
1 of 43
Nadar saraswathi college of arts &
science,theni.
Department of cs & it
ADVANCED JAVA PROGRAMMING
PRESENTED BY
G.KAVIYA
M.SC(IT)
TOPIC:SWING COMPONENTS.
SWING COMPONENTS
SYNOPSIS
o WHAT IS SWING?
o WHAT IS COMPONENTS?
o WHAT IS SWING COMPONENTS?
o TYPES OF SWING COMPONENTS?
o ADVANTAGES AND DISADVANTAGE OF SWING
o DEFINITION OF JTEXT FIELD
o DEFINITION OF JCHECKBOX
o DEFINITION OF JRADIOBUTTON
CONTINOUS:
o DEFINITION OF JLIST
o DEFINITION OF JCOMBOBOX
o DEFINITION OF JLABLE
o CODING
o ADVANTAGES AND DISADVANTAGE OF SWING
o CONCLUSION
What is Swing?
 Swing does not use native resources, so it is light weighted.
 The swing user interface does not depend on any specific platform, so
the swing is platform-independent.
 Swing used to develop a standalone desktop application.
 Swing provides plat-independent and light weighted.
Why we called as a swing is an lightweight
component?
 A Swing Component is said to be a lightweight Components
because it written entirely in java and does the high-level
display work itself, rather than relying on code provided by
your computer’s operating system.
What is components?
 Swing components are derived from the JComponent class. The
only exceptions are the four top level containers: jframe, japplet,
jwindow, and jdialog.
 JComponent inherits awt classes container and component.
 All the swing components are represented by classes in the javax.
Swing package.
 All the component classes start with J: JLabel, JButton,
JScrollBar,……
Swing Components
 Swing components are the basic building blocks of an application.
 Swing is GUI widget toolkit for java.
 Every application has some basic interactive interface for the user.
 Example: button, checkbox, radiobutton, textfield etc.,
 Swing components are the interactive element in a java.
Diagram:
Commonly used methods of components class
METHODS 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(boolen b) Sets the visibility of the component. It is by
default false.
Types of components
o JScrollBar
o JTabbedPane
o JTree
o JToggleButton
o JTable
o JTextField
o JButton
o JCheckbox
o JComboBox
o JLabel
o JList
o JRadioButton
jtextfield
 JTextField renders an editable single line text box.
A user can input non-formatted text in the box. To initialize the
text field, call its constructor and the pass an optional integer
parameter to it. This parameter sets the width of the box
measured by the columns.
JTextField class declaration
 public class jtexfield extends jtext components to
swingConstants.
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 and 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.
EXAMPLE:
It renders a textbox of 20 columns width
JTextField txtBox = new
JTextField(20);
Jcheckbox
 The JCheckBox class used to create a checkbox it 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 JToggleButton
class.
JCheckBox class declaration
public class JCheckBox extends JToggleButton implements
Accessible
Commonly used Constructors of
JCheckBox
Constructor Description
JCheckBox() Creates an initially unselected check box
button with no text, no icon.
JCheckBox(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.
Commonly used methods :
METHODS DESCRIPTION
AccessibleContext getAccessibleContent() Its used to get the AccessibleContext
associated with this JCheckBox.
protected String paramString() It returns a string representation of this
JCheckBox.
EXAMPLE:
Boolean value that indicates the default state of the check-box.
CheckBox chkBox = new
JCheckBox(“Show Help’, true)
Jradiobutton
 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 radio button only.
JRadioButton class declaration:
public class JRadioButton extends JToggleButton implements
Accessible.
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 set Text(String s) Its used to set specified text on button.
String getText() Its used to return the text of the button.
void setEnabled(boolean b) Its used to enable or disable the button.
void setIcon(Icon b) Its used to specified Icon on the button.
Icon getIcon() Its used to get the Icon of the button.
void set Mnemonic(int a) Its used to set the mnemonic on the button.
void addActionListener(ActionListener a) Its used to add action listener to this object.
EXAMPLE:
ButtonGroup radioGroup = newButtonGroup();
JRadioButton rb1 = new JRadioButton(“Easy”, true);
JRadioButton rb2 = new JRadioButton(“medium”);
JRadioButton rb3 = new JRadioButton(“Hard”);
radioGroup.add(rb1);
radioGroup.add(rb2);
radioGroup.add(rb3);
Jlist
 Jlist components renders a scrollable list of elements. A user
can select a value or multiple values from the list. The select
behaviour is define in the code by the developer . It inherits
JComponents class.
JList class declaration:
public class JList extends JComponent implements Scrollable
Accessible.
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 :
METHODS DESCRIPTION
void addListSelectionListener
(ListSelectionListenerlistener)
It is used to add a listener to the list,to be
notified each time a change a selection occurs.
int getSelectedIndex() It is used to return the smallest selected cell
index.
ListModel getModel() It is used to be return 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.
EXAMPLE:
DefaultListItem cityList = new DefaultListItem();
CityList.addElements (“Mumbai”):
CityList.addElements(“London”):
CityList.addElements(“New York”):
CityList.addElements(“Sydney”):
CityList.addElement(“Tokyo”):
JList cities = new Jlist(cityList);
Cities.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Jcombobox
 The object of Choice class is used to show popup menu of
choices. Choice selected by user in shown on the top of a menu.
It inherits JComponent class.
JComboBox class declaration:
public class JComboBox extends JComponent implements
ItemSelectable.
ListDataListener, ActionListener, Accessible.
Commonly used Constructors of
JComboBox
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(Vertex<?> items) Creates a JComboBox that contains the
elements in the specified Vector.
Commonly used methods :
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.
EXAMPLE:
String[] cityString={“Mumbai”, “London”, “New York”,
“Sydney”, “Tokyo”};
JComboBox cities = new JComboBox(citiesList);
cities.setSelectedIndex(3);
Jlabel
 The object of JLabel class is a component for placing 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.
JLabel class declaration:
public class JButton extends AbstractButton implements Accessible.
Commonly used Constructors of JLable
Constructor Description
JLabel() Creates a JLabel instances with no image and
with an empty string for the title.
JLabel(String s) Creates a JLabel instances with the specified
text.
JLabel(Icon i) Creates a JLabel instances with the specified
image.
Jlabel(String s, Icon i, int
horizontalAlignment)
Creates a Jlabel instances with the specified
text, image and horizontal alignment.
Commonly used methods :
METHODS DESCRIPTION
String getText() It returns the text string that a label display.
void set Text(String text) It defines the single line of the text of this
component will display.
void setHorizontalAlignment(int alignment) It sets the alignment of the Label’s content along
the X-axis.
Icon getIcon() It returns the graphic image that the label
display.
int getHorizontalAlignment() It returns the alignment of the label’s contents
along the X-axis.
EXAMPLE:
It returns a label with a home icon.
JLabel imgLabel = new
JLabel(homeIcon);
CODING
import javax.swing.*;
public class SwingApp {
SwingApp(){
JFrame f = new JFrame();
JLabel firstName = new JLabel(“First Name”);
firstName.setBounds(20,50,80,20);
JLabel lastName = new JLabel(“Last Name”);
lastName.setBounds(20,80,80,20);
JLabel dob = new JLabel(“Date of Birth”);
dob.setBounds(20,110,80,20);
continuous
JTextField firstNameTF = new JTextField();
firstNameTF.setBounds(120,50,100,20);
JTextField lastNameTF = new JTextField();
lastNameTF.setBounds(120,80,100,20);
JTextField dobTF = new JTextField();
dobTF.setBounds(120,110,100,20);
JButton sbmt = new JButton(“Submit”);
sbmt.setBounds(20,160,100,30);
continuous
JButton reset = new JButton(“Reset”);
Reset.setBounds(120,160,100,30);
f.add(firstName);
f.add(lastName);
f.add(dob);
f.add(firstNameTF);
f.add(lastNameTF);
f.add(dobTF);
f.add(sbmt);
f.add(reset);
continuous
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args)
// TODO Auto-generated method stub
SwingApp s = new SwingApp();
}
}
Output:
ADVANTAGES AND DISADVANTAGE OF SWING
ADVANTAGE DISADVANTAGE
 Swing provides paint debugging support
for when you build your own
components.
 Swing components that look like a native
components might not act exactly like
native components.
 Swing components use the model-view-
controller paradigm(MVC) and thus can
provide a much more flexible UI.
 It requires java 1.2 or a separate JAR file.
 Swing provides built-in double buffering.  It can be slower than AWT(all
components are drawn)as if we’re not
careful in programming.
Conclusion
 The list of Components in Swing, it is highly
recommended to dive deeper and explore more.
Swing Components are fun to play around with and
can help create some real cool application.
Advanced java programming

More Related Content

What's hot

Abstract factory petterns
Abstract factory petternsAbstract factory petterns
Abstract factory petterns
HyeonSeok Choi
 
Google app engine cheat sheet
Google app engine cheat sheetGoogle app engine cheat sheet
Google app engine cheat sheet
Piyush Mittal
 

What's hot (19)

Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
java swing
java swingjava swing
java swing
 
Swings in java
Swings in javaSwings in java
Swings in java
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
Web Design & Development - Session 6
Web Design & Development - Session 6Web Design & Development - Session 6
Web Design & Development - Session 6
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 
Abstract factory petterns
Abstract factory petternsAbstract factory petterns
Abstract factory petterns
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
jQuery 1.7 visual cheat sheet
jQuery 1.7 visual cheat sheetjQuery 1.7 visual cheat sheet
jQuery 1.7 visual cheat sheet
 
Jquery 17-visual-cheat-sheet1
Jquery 17-visual-cheat-sheet1Jquery 17-visual-cheat-sheet1
Jquery 17-visual-cheat-sheet1
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Google app engine cheat sheet
Google app engine cheat sheetGoogle app engine cheat sheet
Google app engine cheat sheet
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 

Similar to Advanced java programming

Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
Narayana Swamy
 

Similar to Advanced java programming (20)

DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
Swing basics
Swing basicsSwing basics
Swing basics
 
Java swing
Java swingJava swing
Java swing
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
Swings
SwingsSwings
Swings
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Jp notes
Jp notesJp notes
Jp notes
 
JAVA Jcheckbox in simple and easy .pptx
JAVA Jcheckbox in simple and  easy .pptxJAVA Jcheckbox in simple and  easy .pptx
JAVA Jcheckbox in simple and easy .pptx
 
Module 4
Module 4Module 4
Module 4
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
Gui programming a review - mixed content
Gui programming   a review - mixed contentGui programming   a review - mixed content
Gui programming a review - mixed content
 
AWT New-3.pptx
AWT New-3.pptxAWT New-3.pptx
AWT New-3.pptx
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of applet
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
GUI programming
GUI programmingGUI programming
GUI programming
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
 

More from Kaviya452563

More from Kaviya452563 (14)

softcomputing.pptx
softcomputing.pptxsoftcomputing.pptx
softcomputing.pptx
 
OOAD.pptx
OOAD.pptxOOAD.pptx
OOAD.pptx
 
DIP.pptx
DIP.pptxDIP.pptx
DIP.pptx
 
Big Data Analytics.pptx
Big Data Analytics.pptxBig Data Analytics.pptx
Big Data Analytics.pptx
 
client server computing.pptx
client server computing.pptxclient server computing.pptx
client server computing.pptx
 
WE.pptx
WE.pptxWE.pptx
WE.pptx
 
Internet of Things.pptx
Internet of Things.pptxInternet of Things.pptx
Internet of Things.pptx
 
data mining.pptx
data mining.pptxdata mining.pptx
data mining.pptx
 
python programming.pptx
python programming.pptxpython programming.pptx
python programming.pptx
 
Distributing computing.pptx
Distributing computing.pptxDistributing computing.pptx
Distributing computing.pptx
 
Artificial Intelligence.pptx
Artificial Intelligence.pptxArtificial Intelligence.pptx
Artificial Intelligence.pptx
 
Network and internet security
Network and internet securityNetwork and internet security
Network and internet security
 
Advanced computer architecture
Advanced computer architectureAdvanced computer architecture
Advanced computer architecture
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 

Advanced java programming

  • 1. Nadar saraswathi college of arts & science,theni. Department of cs & it ADVANCED JAVA PROGRAMMING PRESENTED BY G.KAVIYA M.SC(IT) TOPIC:SWING COMPONENTS.
  • 3. SYNOPSIS o WHAT IS SWING? o WHAT IS COMPONENTS? o WHAT IS SWING COMPONENTS? o TYPES OF SWING COMPONENTS? o ADVANTAGES AND DISADVANTAGE OF SWING o DEFINITION OF JTEXT FIELD o DEFINITION OF JCHECKBOX o DEFINITION OF JRADIOBUTTON
  • 4. CONTINOUS: o DEFINITION OF JLIST o DEFINITION OF JCOMBOBOX o DEFINITION OF JLABLE o CODING o ADVANTAGES AND DISADVANTAGE OF SWING o CONCLUSION
  • 5. What is Swing?  Swing does not use native resources, so it is light weighted.  The swing user interface does not depend on any specific platform, so the swing is platform-independent.  Swing used to develop a standalone desktop application.  Swing provides plat-independent and light weighted.
  • 6. Why we called as a swing is an lightweight component?  A Swing Component is said to be a lightweight Components because it written entirely in java and does the high-level display work itself, rather than relying on code provided by your computer’s operating system.
  • 7. What is components?  Swing components are derived from the JComponent class. The only exceptions are the four top level containers: jframe, japplet, jwindow, and jdialog.  JComponent inherits awt classes container and component.  All the swing components are represented by classes in the javax. Swing package.  All the component classes start with J: JLabel, JButton, JScrollBar,……
  • 8. Swing Components  Swing components are the basic building blocks of an application.  Swing is GUI widget toolkit for java.  Every application has some basic interactive interface for the user.  Example: button, checkbox, radiobutton, textfield etc.,  Swing components are the interactive element in a java.
  • 10. Commonly used methods of components class METHODS 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(boolen b) Sets the visibility of the component. It is by default false.
  • 11. Types of components o JScrollBar o JTabbedPane o JTree o JToggleButton o JTable o JTextField o JButton o JCheckbox o JComboBox o JLabel o JList o JRadioButton
  • 12. jtextfield  JTextField renders an editable single line text box. A user can input non-formatted text in the box. To initialize the text field, call its constructor and the pass an optional integer parameter to it. This parameter sets the width of the box measured by the columns. JTextField class declaration  public class jtexfield extends jtext components to swingConstants.
  • 13. 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 and columns
  • 14. 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.
  • 15. EXAMPLE: It renders a textbox of 20 columns width JTextField txtBox = new JTextField(20);
  • 16. Jcheckbox  The JCheckBox class used to create a checkbox it 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 JToggleButton class. JCheckBox class declaration public class JCheckBox extends JToggleButton implements Accessible
  • 17. Commonly used Constructors of JCheckBox Constructor Description JCheckBox() Creates an initially unselected check box button with no text, no icon. JCheckBox(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.
  • 18. Commonly used methods : METHODS DESCRIPTION AccessibleContext getAccessibleContent() Its used to get the AccessibleContext associated with this JCheckBox. protected String paramString() It returns a string representation of this JCheckBox.
  • 19. EXAMPLE: Boolean value that indicates the default state of the check-box. CheckBox chkBox = new JCheckBox(“Show Help’, true)
  • 20. Jradiobutton  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 radio button only. JRadioButton class declaration: public class JRadioButton extends JToggleButton implements Accessible.
  • 21. 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.
  • 22. Commonly used methods : METHODS DESCRIPTION void set Text(String s) Its used to set specified text on button. String getText() Its used to return the text of the button. void setEnabled(boolean b) Its used to enable or disable the button. void setIcon(Icon b) Its used to specified Icon on the button. Icon getIcon() Its used to get the Icon of the button. void set Mnemonic(int a) Its used to set the mnemonic on the button. void addActionListener(ActionListener a) Its used to add action listener to this object.
  • 23. EXAMPLE: ButtonGroup radioGroup = newButtonGroup(); JRadioButton rb1 = new JRadioButton(“Easy”, true); JRadioButton rb2 = new JRadioButton(“medium”); JRadioButton rb3 = new JRadioButton(“Hard”); radioGroup.add(rb1); radioGroup.add(rb2); radioGroup.add(rb3);
  • 24. Jlist  Jlist components renders a scrollable list of elements. A user can select a value or multiple values from the list. The select behaviour is define in the code by the developer . It inherits JComponents class. JList class declaration: public class JList extends JComponent implements Scrollable Accessible.
  • 25. 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.
  • 26. Commonly used methods : METHODS DESCRIPTION void addListSelectionListener (ListSelectionListenerlistener) It is used to add a listener to the list,to be notified each time a change a selection occurs. int getSelectedIndex() It is used to return the smallest selected cell index. ListModel getModel() It is used to be return 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.
  • 27. EXAMPLE: DefaultListItem cityList = new DefaultListItem(); CityList.addElements (“Mumbai”): CityList.addElements(“London”): CityList.addElements(“New York”): CityList.addElements(“Sydney”): CityList.addElement(“Tokyo”): JList cities = new Jlist(cityList); Cities.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  • 28. Jcombobox  The object of Choice class is used to show popup menu of choices. Choice selected by user in shown on the top of a menu. It inherits JComponent class. JComboBox class declaration: public class JComboBox extends JComponent implements ItemSelectable. ListDataListener, ActionListener, Accessible.
  • 29. Commonly used Constructors of JComboBox 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(Vertex<?> items) Creates a JComboBox that contains the elements in the specified Vector.
  • 30. Commonly used methods : 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.
  • 31. EXAMPLE: String[] cityString={“Mumbai”, “London”, “New York”, “Sydney”, “Tokyo”}; JComboBox cities = new JComboBox(citiesList); cities.setSelectedIndex(3);
  • 32. Jlabel  The object of JLabel class is a component for placing 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. JLabel class declaration: public class JButton extends AbstractButton implements Accessible.
  • 33. Commonly used Constructors of JLable Constructor Description JLabel() Creates a JLabel instances with no image and with an empty string for the title. JLabel(String s) Creates a JLabel instances with the specified text. JLabel(Icon i) Creates a JLabel instances with the specified image. Jlabel(String s, Icon i, int horizontalAlignment) Creates a Jlabel instances with the specified text, image and horizontal alignment.
  • 34. Commonly used methods : METHODS DESCRIPTION String getText() It returns the text string that a label display. void set Text(String text) It defines the single line of the text of this component will display. void setHorizontalAlignment(int alignment) It sets the alignment of the Label’s content along the X-axis. Icon getIcon() It returns the graphic image that the label display. int getHorizontalAlignment() It returns the alignment of the label’s contents along the X-axis.
  • 35. EXAMPLE: It returns a label with a home icon. JLabel imgLabel = new JLabel(homeIcon);
  • 36. CODING import javax.swing.*; public class SwingApp { SwingApp(){ JFrame f = new JFrame(); JLabel firstName = new JLabel(“First Name”); firstName.setBounds(20,50,80,20); JLabel lastName = new JLabel(“Last Name”); lastName.setBounds(20,80,80,20); JLabel dob = new JLabel(“Date of Birth”); dob.setBounds(20,110,80,20);
  • 37. continuous JTextField firstNameTF = new JTextField(); firstNameTF.setBounds(120,50,100,20); JTextField lastNameTF = new JTextField(); lastNameTF.setBounds(120,80,100,20); JTextField dobTF = new JTextField(); dobTF.setBounds(120,110,100,20); JButton sbmt = new JButton(“Submit”); sbmt.setBounds(20,160,100,30);
  • 38. continuous JButton reset = new JButton(“Reset”); Reset.setBounds(120,160,100,30); f.add(firstName); f.add(lastName); f.add(dob); f.add(firstNameTF); f.add(lastNameTF); f.add(dobTF); f.add(sbmt); f.add(reset);
  • 39. continuous f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public static void main(String[] args) // TODO Auto-generated method stub SwingApp s = new SwingApp(); } }
  • 41. ADVANTAGES AND DISADVANTAGE OF SWING ADVANTAGE DISADVANTAGE  Swing provides paint debugging support for when you build your own components.  Swing components that look like a native components might not act exactly like native components.  Swing components use the model-view- controller paradigm(MVC) and thus can provide a much more flexible UI.  It requires java 1.2 or a separate JAR file.  Swing provides built-in double buffering.  It can be slower than AWT(all components are drawn)as if we’re not careful in programming.
  • 42. Conclusion  The list of Components in Swing, it is highly recommended to dive deeper and explore more. Swing Components are fun to play around with and can help create some real cool application.