Swing Module 3
Swing Module 3
Swing Module 3
Swings
Introduction
Swing contains a set of classes that provides more powerful and flexible GUI
components than those of AWT. Swing provides the look and feel of modern
Java GUI. Swing library is an official Java GUI tool kit released by Sun
Microsystems. It is used to create graphical user interface with Java.
Swing is a set of program component s for Java programmers that provide the ability
to create graphical user interface ( GUI ) components, such as buttons and scroll bars,
that are independent of the windowing system for specific operating system . Swing
The original Java GUI subsystem was the Abstract Window Toolkit (AWT).
(peers).
Under AWT, the look and feel of a component was defined by the platform.
1. Platform Independent
2. Customizable
3. Extensible
4. Configurable
5. Lightweight
18 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
Model-View-Controller
Components
Swing components are derived from the JComponent class. The only
exceptions are the four top-level containers: JFrame, JApplet, JWindow,
and JDialog.
All the Swing components are represented by classes in the javax.swing package.
All the component classes start with J: JLabel, JButton, JScrollbar, ...
Containers
There are two types of containers:
19 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
All the component classes start with J: JLabel, JButton, JScrollbar, ...
JRootPane manages the other panes and can add a menu bar.
The content pane is the container used for visual components. The
content pane is an instance of JPanel.
Swing is a very large subsystem and makes use of many packages. These are
The main package is javax.swing. This package must be imported into any
program that uses Swing. It contains the classes that implement the basic
javax.swing javax.swing.plaf.synth
javax.swing.border javax.swing.table
javax.swing.colorchooser javax.swing.text
javax.swing.event javax.swing.text.html
javax.swing.filechooser javax.swing.text.html.parser
javax.swing.plaf javax.swing.text.rtf
javax.swing.plaf.basic javax.swing.tree
javax.swing.plaf.metal javax.swing.undo
javax.swing.plaf.multi
20 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
We can write the code of swing inside the main(), constructor or any
other method.
By creating the object of Frame class
import javax.swing.*;
public class FirstSwing
{
public static void main(String[] args)
{
JFrame f=new JFrame(“ MyApp”);
//creating instance of JFrame and title of the frame is
MyApp. JButton b=new JButton("click");
//creating instance of JButton and name of the button is click.
b.setBounds(130,100,100, 40); //x axis, y axis, width, height
f.add(b); //adding button in JFrame
f.setSize(400,500); //400 width and 500 height
f.setLayout(null); //using no layout managers
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); //making the frame visible
}
}
Output:
21 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
Explanation:
➢ The program begins by importing javax.swing. As mentioned, this package
contains the components and models defined by Swing.
➢ For example, javax.swing defines classes that implement labels, buttons,
text controls, and menus. It will be included in all programs that use
Swing. Next,
➢ the program declares the FirstSwing class
➢ It begins by creating a JFrame, using this line of code:
JFrame f = new JFrame("My App");
f.setSize(400,500);
➢ The setSize( ) method (which is inherited by JFrame from the AWT class
Component) sets the dimensions of the window, which are specified in
pixels. in this example, the width of the window is set to 400 and the height
is setto 500.
➢ By default, when a top-level window is closed (such as when the user clicks
the close box), the window is removed from the screen, but the application
is not terminated.
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Swing by inheritance
22 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
import javax.swing.*;
public class MySwing extends JFram //inheriting JFrame
{
JFrame f;
MySwing()
{
JButton b=new JButton("click");//create
button b.setBounds(130,100,100, 40);
add(b);//adding button on
frame setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new MySwing();
}
}
JLabel(Icon icon)
JLabel(String str)
23 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
import javax.swing.*;
public class JTextFieldPgm
{
f.add(namelabel);
f.add( passwordLabel);
f.add(userText);
f.add(passwordText);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
24 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
Output:
➢
JLabel can be used to display text and/or an icon. It is a passive component in
that it does not respond to user input. JLabel defines several constructors. Here
are three of them:
JLabel(Icon icon)
JLabel(String str)
Here, str and icon are the text and icon used for the label.
➢
These constants are defined in the Swing Constants interface, along with
several others used by the Swing classes. Notice that icons are specified
by
objects of type Icon, which is an interface defined by Swing.
➢
The easiest way to obtain an icon is to use the ImageIcon class. ImageIcon
implements Icon and encapsulates an image. Thus, an object of type
ImageIcon can be passed as an argument to the Icon parameter of
JLabel’s constructor.
ImageIcon(String filename)
25 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
➢ It obtains the image in the file named filename. The icon and text
associated with the label can be obtained by the following methods:
Icon getIcon( )
String getText( )
The icon and text associated with a label can be set by these methods:
Here, icon and str are the icon and text, respectively. Therefore, using
setText( ) it is possible to change the text inside a label during program
execution.
import javax.swing.*;
JFrame jf=new
JFrame("Image Icon");
jf.setLayout(null);
26 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
1. JButton
2. JRadioButton
3. JCheckBox
4. JComboBox
JButton(Icon ic)
JButton(String str)
import javax.swing.*;
class FirstSwing
{
public static void main(String args[])
{
JFrame jf=new JFrame("My App");
jf.add(jb);
jf.add(jb1);
jf.setSize(300, 600);
jf.setLayout(null);
jf.setVisible(true);
}
}
27 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
import javax.swing.*;
public class RadioButton1
{
public static void main(String args[])
{
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
28 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
class FirstSwing
{
public static void main(String args[])
{
JFrame jf=new JFrame("CheckBox");
JCheckBox jb1=new
JCheckBox("Python");
jb1.setBounds(30, 200, 100, 50);
jf.add(jb);
jf.add(jb1);
jf.setSize(300, 600);
jf.setLayout(null);
jf.setVisible(true);
}
}
29 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
import java.awt.*;
import javax.swing.*;
{
JFrame f=new JFrame("Combo demo");
String Branch[]={"cse","ise","ec","mech"};
jc.setBounds(50,50,80,50);
f.add(jc);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
30 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
The JTable class is used to display the data on two dimensional tables of cells.
✓
JScrollPane is a lightweight container that automatically handles the
scrolling of another component.
✓
The component being scrolled can either be an individual component,
such as a table, or a group of components contained within another
lightweight container, such as a JPanel.
✓ In either case, if the object being scrolled is larger than the viewable
area, horizontal and/or vertical scroll bars are automatically provided,
and the component can be scrolled through the pane. Because
JScrollPane automates scrolling, it usually eliminates the need to manage
individual scroll bars.
✓
The viewable area of a scroll pane is called the viewport.
✓ It is a window in which the component being scrolled is displayed.
✓
Thus, the view port displays the visible portion of the component being
scrolled. The scroll bars scroll the component through the viewport.
✓
In its default behavior, a JScrollPane will dynamically add or remove a
scroll bar as needed. For example, if the component is taller than the
viewport, a vertical scroll bar is added. If the component will completely
fit within the viewport, the scroll bars are removed.
31 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
import javax.swing.*;
public class TableExample1
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
JFrame f=new JFrame("Table Demo");
String data[][]={
{"100","CSE","VTU"}
,
{"101","ISE","VTU"}
,
{"102","CSE","VTU"}
,
{"103","ISE","VTU"}
,
{"105","ISE","VTU"}
,
{"106","ISE","VTU"}
};
String column[]={"courseID","Branch","University"};
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
}
}
32 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
JTabbedpane
✓
JTabbedPane encapsulates a tabbed pane. It manages a set of components
by linking them with tabs.
✓ Selecting a tab causes the component associated with that tab to come to
the forefront. Tabbed panes are very common in the modern GUI.
✓
Given the complex nature of a tabbed pane, they are surprisingly easy
to create and use. JTabbedPane defines three constructors. We will
use its
default constructor, which creates an empty control with the tabs
positioned across the top of the pane.
✓
The other two constructors let you specify the location of the tabs, which
can be along any of the four sides.
✓
JTabbedPane uses the SingleSelectionModel model. Tabs are added by calling addTab( )
method. Here is one of its forms:
✓
Here, name is the name for the tab, and comp is the component that should
be added to the tab. Often, the component added to a tab is a JPanel that
contains a group of related components. This technique allows a tab to
hold a set of components.
import javax.swing.*;
{
public static void main(String[] a)
{
JFrame f = new JFrame("JTab");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JTabbedPaneDemo());
f.setSize(500, 500);
f.setVisible(true);
33 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
{
makeGUI();
}
void makeGUI()
{
JTabbedPane jtp = new JTabbedPane();
add(jtp);
}
}
add(b1);
add(b2);
add(b3);
add(b4);
}
JCheckBox("Red"); add(cb1);
JCheckBox("Green"); add(cb2);
JCheckBox cb3 = new
JCheckBox("Blue"); add(cb3);
}
}
public FlavorsPanel()
{
JComboBox jcb = new JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
add(jcb);
}
}
35 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
JList:
JList(Object[ ] items)
✓
This creates a JList that contains the items in the array specified by items.
✓
JList is based on two models. The first is ListModel. This interface
defines how access to the list data is achieved.
✓
The second model is the ListSelectionModel interface, which defines
methods that determine what list item or items are selected.
import java.awt.FlowLayout;
import javax.swing.*;
list.setSelectedIndex(1);
frame.add(new JScrollPane(list));
frame.setSize(300, 400);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
36 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
3. AWT doesn't support pluggable look Swing supports pluggable look and feel.
and feel.
4. AWT provides less components than Swing provides more powerful
Swing. components such as tables, lists,
scrollpanes, colorchooser, tabbedpane
etc.
5. AWT doesn't follows MVC(Model Swing follows MVC .where model
View represents data, view represents
Controller) presentation and controller acts as an
interface between model and view.
37 | P a g e
Object Oriented Concepts-MODULE 5 18CS45
Questions
38 | P a g e