SlideShare a Scribd company logo
Introducing Swing
Swing is a part of Java Foundation Classes (JFC) that is
used to create window-based applications.
With Java 1.1, Swing was used as a separate library.
It is a set of classes which provides many powerful
and flexible components for creating graphical user
interface.
It is built on the top of AWT (Abstract Windowing
Toolkit) API and entirely written in java.
Unlike AWT, Swing provides platform-independent
and lightweight components.
Features of Swings
It has two popular features:
1. Lightweight components
2. Pluggable look and feel
Lightweight components
Swing components are lightweight as they are written
entirely in Java and do not depend on native peers.
they are not restricted to platform-specific
appearance like, rectangular or opaque shape.
Pluggable look and feel
The pluggable look arid feel feature allows us to tailor
the look and feel of the application and applets to
the standard looks like, Windows and Motif.
We can even switch to different look and feel at
runtime.
Swing has the capability to support several look and
feels, but at present, it provides support for the
Windows and Motif.
the look and feel of components is controlled by
Swing rather than by operating system, the feel of
components can also be changed.
Hierarchy of Swing Components
Top Level Containers
Top-level Containers exist mainly to provide a place for
other Swing components to paint themselves. Swing
provides four top-level container classes:
1. JFrame - A top-level window with a title and a border.
2. JWindow - As a rule, not very useful. Provides a
window with no controls or title.
3. JDialog - The main class for creating a dialog window.
4. JApplet - Enables applets to use Swing components.
JFrame
import javax.swing.*;
class SwingDemo
{
SwingDemo()
{
JFrame jfrm = new JFrame("A Simple Swing Application");
jfrm.setSize(275, 100);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jlab = new JLabel(" Swing means powerful GUIs.");
jfrm.add(jlab);
jfrm.setVisible(true);
}
public static void main(String args[])
{ // Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new SwingDemo();
}
});
}
}
JWindow
The class JWindow is a container that can be
displayed but does not have the title bar or
window-management buttons.
Constructors
JWindow()
JWindow(Frame owner)
JWindow(GraphicsConfiguration gc)
JApplet
The JApplet class extends the Applet class.
Example
SimpleApplet.java
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class SimpleApplet extends JApplet {
public void init() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 2, 2, 2));
p.add(new JLabel("Username"));
p.add(new JTextField());
p.add(new JLabel("Password"));
p.add(new JPasswordField());
Container content = getContentPane();
content.setLayout(new GridBagLayout()); // Used to center the panel
content.add(p);
}
}
<html>
<head><title>japplet example</title></head>
<APPLET CODE = SimpleApplet WIDTH = 300
HEIGHT = 200>
< PARAM NAME = "bogus" VALUE ="just
testing">
< /APPLET>
</html>
Light Weight Containers
• Lightweight containers do inherit Jcomponent.
• Lightweight containers are often used to
organize and manage groups of related
components because a lightweight container
can be contained within another container.
• One of the examples of lightweight container
is JPanel.
JPanel
Panel is a container to hold different Swing components.
One can add any number of components to a panel and
there can be multiple panels in the same frame.
JPanel present in javax.swing package.
Constructors
JPanel ()
JPanel(boolean isDoubleBuffered)
JPanel(LayoutManager layout)
JPanel(LayoutManager layout, boolean isDoubleBuffered)
where,
isDoubleBuffered defines whether the panel is double
buffered or not
Example
import javax.swing.*;
import. Java.awt.event.*;
import java.awt.*;
class JPanelExample extends JFrame
{
JPanel panel1,panel2 ;
JTextField txtData;
JButton[] btnData = new JButton[12];
JPanelExample()
{
panel1=new JPanel();
txtData = new JTextField(20);
panel1.add(txtData);
add(panel1,"North");
panel2=new JPanel(new GridLayout(3,4));
for(int i=0;i<=9;i++)
{
btnData[i]=new JButton(""+i);
panel2.add(btnData[i]);
}
add(panel2,"Center");
}
}
class JPanelJavaExample
{
public static void main(String[] args)
{
JPanelExample frame = new JPanelExample();
frame.setTitle("JPanel Java Example");
frame.setBounds(100,200,220,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Example
import javax.swing.*;
import java.awt.*;
public class PanelsWithJFrameJavaExample extends JFrame
{
private final int WIDTH = 250;
private final int HEIGHT = 120;
private JButton BtnOne = new JButton("One");
private JButton BtnTwo = new JButton("Two");
private JButton BtnThree = new JButton("Three");
public PanelsWithJFrameJavaExample()
{
super("Panels in Java swing");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel PnlOne = new JPanel();
JPanel PnlTwo = new JPanel();
Container cntnr = getContentPane();
cntnr.setLayout(new FlowLayout());
cntnr.add(PnlOne);
cntnr.add(PnlTwo);
PnlOne.add(BtnOne);
PnlOne.setBackground(Color.BLUE);
PnlTwo.add(BtnTwo);
PnlTwo.add(BtnThree);
PnlTwo.setBackground(Color.BLUE);
setSize(300,450);
setVisible(true);
}
public static void main(String[] args)
{
PanelsWithJFrameJavaExample panel = new PanelsWithJFrameJavaExample();
}
}
Swing Components
Commonly used Methods of Component class
JButton
Declaration of JButton class
public class JButton extends AbstractButton
implements Accessible
Commonly used Constructors of JButton:
Jbutton()
JButton(String s)
JButton(Icon i)
Example
import java.io.*;
import javax.swing.*;
class ButtonExample {
public static void main(String[] args)
{
JFrame frame= new JFrame(); // creating instance of JFrame
JButton button = new JButton(" Click"); // creating instance of
// JButton
button.setBounds(150, 200, 220,50); // x axis, y axis, width, height
frame.add(button); // adding button in JFrame
frame.setSize(500, 600); // 400 width and 500 height
frame.setLayout(null); // using no layout managers
frame.setVisible(true); // making the frame visible
}
}
JLabel
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.
Constructors
JLabel()
JLabel(String s)
JLabel(Icon i)
JLabel(String s, Icon i, int horizontalAlignment)
Example
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends
{
static JFrame f;
static JLabel l;
text()
{
}
public static void main(String[] args)
{
f = new JFrame("label");
l = new JLabel();
l.setText("label text");
JPanel p = new JPanel();
p.add(l);
f.add(p);
f.setSize(300, 300);
f.show();
}
}
JTextField
The object of a JTextField class is a text component
that allows the editing of a single line text. It
inherits JTextComponent class.
Constructor
JTextField()
JTextField(String text)
JTextField(String text, int columns)
JTextField(int columns)
import javax.swing.*;
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to CMRCET..");
t1.setBounds(50,100, 200,30);
t2=new JTextField("Hi AIDS");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
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
Constructors
JTextArea()
JTextArea(String s)
JTextArea(int row, int column)
JTextArea(String s, int row, int column)
import javax.swing.*;
public class TextAreaExample
{
TextAreaExample(){
JFrame f= new JFrame();
JTextArea area=new JTextArea("Welcome to javatpoint");
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}}

More Related Content

Similar to swings.pptx

SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
GEETHAS668001
 
Java swing
Java swingJava swing
Java swing
ssuser3a47cb
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
PRN USM
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swingPrashant Mehta
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
MohanYedatkar
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
Shehrevar Davierwala
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
renuka gavli
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
Amol Gaikwad
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
karan saini
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
Chhom Karath
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
Ankit Dubey
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
SamyakJain710491
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
bharathiv53
 
Java Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptJava Swing Handson Session (1).ppt
Java Swing Handson Session (1).ppt
ssuser076380
 

Similar to swings.pptx (20)

SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
Java swing
Java swingJava swing
Java swing
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swing
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
Ajp notes-chapter-02
Ajp notes-chapter-02Ajp notes-chapter-02
Ajp notes-chapter-02
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
Lecture9 oopj
Lecture9 oopjLecture9 oopj
Lecture9 oopj
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
Java Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptJava Swing Handson Session (1).ppt
Java Swing Handson Session (1).ppt
 

Recently uploaded

Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

swings.pptx

  • 1. Introducing Swing Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. With Java 1.1, Swing was used as a separate library. It is a set of classes which provides many powerful and flexible components for creating graphical user interface. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java. Unlike AWT, Swing provides platform-independent and lightweight components.
  • 2. Features of Swings It has two popular features: 1. Lightweight components 2. Pluggable look and feel Lightweight components Swing components are lightweight as they are written entirely in Java and do not depend on native peers. they are not restricted to platform-specific appearance like, rectangular or opaque shape.
  • 3. Pluggable look and feel The pluggable look arid feel feature allows us to tailor the look and feel of the application and applets to the standard looks like, Windows and Motif. We can even switch to different look and feel at runtime. Swing has the capability to support several look and feels, but at present, it provides support for the Windows and Motif. the look and feel of components is controlled by Swing rather than by operating system, the feel of components can also be changed.
  • 4. Hierarchy of Swing Components
  • 5. Top Level Containers Top-level Containers exist mainly to provide a place for other Swing components to paint themselves. Swing provides four top-level container classes: 1. JFrame - A top-level window with a title and a border. 2. JWindow - As a rule, not very useful. Provides a window with no controls or title. 3. JDialog - The main class for creating a dialog window. 4. JApplet - Enables applets to use Swing components.
  • 6. JFrame import javax.swing.*; class SwingDemo { SwingDemo() { JFrame jfrm = new JFrame("A Simple Swing Application"); jfrm.setSize(275, 100); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel jlab = new JLabel(" Swing means powerful GUIs."); jfrm.add(jlab); jfrm.setVisible(true); } public static void main(String args[]) { // Create the frame on the event dispatching thread. SwingUtilities.invokeLater(new Runnable() { public void run() { new SwingDemo(); } }); } }
  • 7. JWindow The class JWindow is a container that can be displayed but does not have the title bar or window-management buttons. Constructors JWindow() JWindow(Frame owner) JWindow(GraphicsConfiguration gc)
  • 8. JApplet The JApplet class extends the Applet class. Example SimpleApplet.java import javax.swing.*; import javax.swing.border.*; import java.awt.*; public class SimpleApplet extends JApplet { public void init() { JPanel p = new JPanel(); p.setLayout(new GridLayout(2, 2, 2, 2)); p.add(new JLabel("Username")); p.add(new JTextField()); p.add(new JLabel("Password")); p.add(new JPasswordField()); Container content = getContentPane(); content.setLayout(new GridBagLayout()); // Used to center the panel content.add(p); } }
  • 9. <html> <head><title>japplet example</title></head> <APPLET CODE = SimpleApplet WIDTH = 300 HEIGHT = 200> < PARAM NAME = "bogus" VALUE ="just testing"> < /APPLET> </html>
  • 10. Light Weight Containers • Lightweight containers do inherit Jcomponent. • Lightweight containers are often used to organize and manage groups of related components because a lightweight container can be contained within another container. • One of the examples of lightweight container is JPanel.
  • 11. JPanel Panel is a container to hold different Swing components. One can add any number of components to a panel and there can be multiple panels in the same frame. JPanel present in javax.swing package. Constructors JPanel () JPanel(boolean isDoubleBuffered) JPanel(LayoutManager layout) JPanel(LayoutManager layout, boolean isDoubleBuffered) where, isDoubleBuffered defines whether the panel is double buffered or not
  • 12. Example import javax.swing.*; import. Java.awt.event.*; import java.awt.*; class JPanelExample extends JFrame { JPanel panel1,panel2 ; JTextField txtData; JButton[] btnData = new JButton[12]; JPanelExample() { panel1=new JPanel(); txtData = new JTextField(20); panel1.add(txtData); add(panel1,"North"); panel2=new JPanel(new GridLayout(3,4)); for(int i=0;i<=9;i++) { btnData[i]=new JButton(""+i); panel2.add(btnData[i]); } add(panel2,"Center"); } } class JPanelJavaExample { public static void main(String[] args) { JPanelExample frame = new JPanelExample(); frame.setTitle("JPanel Java Example"); frame.setBounds(100,200,220,200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
  • 13. Example import javax.swing.*; import java.awt.*; public class PanelsWithJFrameJavaExample extends JFrame { private final int WIDTH = 250; private final int HEIGHT = 120; private JButton BtnOne = new JButton("One"); private JButton BtnTwo = new JButton("Two"); private JButton BtnThree = new JButton("Three"); public PanelsWithJFrameJavaExample() { super("Panels in Java swing"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel PnlOne = new JPanel(); JPanel PnlTwo = new JPanel(); Container cntnr = getContentPane(); cntnr.setLayout(new FlowLayout()); cntnr.add(PnlOne); cntnr.add(PnlTwo); PnlOne.add(BtnOne); PnlOne.setBackground(Color.BLUE); PnlTwo.add(BtnTwo); PnlTwo.add(BtnThree); PnlTwo.setBackground(Color.BLUE); setSize(300,450); setVisible(true); } public static void main(String[] args) { PanelsWithJFrameJavaExample panel = new PanelsWithJFrameJavaExample(); } }
  • 14. Swing Components Commonly used Methods of Component class
  • 15. JButton Declaration of JButton class public class JButton extends AbstractButton implements Accessible Commonly used Constructors of JButton: Jbutton() JButton(String s) JButton(Icon i)
  • 16. Example import java.io.*; import javax.swing.*; class ButtonExample { public static void main(String[] args) { JFrame frame= new JFrame(); // creating instance of JFrame JButton button = new JButton(" Click"); // creating instance of // JButton button.setBounds(150, 200, 220,50); // x axis, y axis, width, height frame.add(button); // adding button in JFrame frame.setSize(500, 600); // 400 width and 500 height frame.setLayout(null); // using no layout managers frame.setVisible(true); // making the frame visible } }
  • 17. JLabel 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. Constructors JLabel() JLabel(String s) JLabel(Icon i) JLabel(String s, Icon i, int horizontalAlignment)
  • 18. Example import java.awt.event.*; import java.awt.*; import javax.swing.*; class text extends { static JFrame f; static JLabel l; text() { } public static void main(String[] args) { f = new JFrame("label"); l = new JLabel(); l.setText("label text"); JPanel p = new JPanel(); p.add(l); f.add(p); f.setSize(300, 300); f.show(); } }
  • 19. JTextField The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class. Constructor JTextField() JTextField(String text) JTextField(String text, int columns) JTextField(int columns)
  • 20. import javax.swing.*; class TextFieldExample { public static void main(String args[]) { JFrame f= new JFrame("TextField Example"); JTextField t1,t2; t1=new JTextField("Welcome to CMRCET.."); t1.setBounds(50,100, 200,30); t2=new JTextField("Hi AIDS"); t2.setBounds(50,150, 200,30); f.add(t1); f.add(t2); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } }
  • 21. 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 Constructors JTextArea() JTextArea(String s) JTextArea(int row, int column) JTextArea(String s, int row, int column)
  • 22. import javax.swing.*; public class TextAreaExample { TextAreaExample(){ JFrame f= new JFrame(); JTextArea area=new JTextArea("Welcome to javatpoint"); area.setBounds(10,30, 200,200); f.add(area); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public static void main(String args[]) { new TextAreaExample(); }}