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();
}}

swings.pptx

  • 1.
    Introducing Swing Swing isa 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 Ithas 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 andfeel 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.
  • 5.
    Top Level Containers Top-levelContainers 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() { JFramejfrm = 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 JWindowis 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 classextends 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 acontainer 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.*; importjava.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.*; publicclass 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 usedMethods of Component class
  • 15.
    JButton Declaration of JButtonclass 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.*; classButtonExample { 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 ofJLabel 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.*; importjavax.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 ofa 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 { publicstatic 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 ofa 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 classTextAreaExample { 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(); }}