0% found this document useful (0 votes)
27 views30 pages

Swing in Java

The document provides an overview of Swing, a Java framework for creating GUI applications. It details key features, class hierarchy, and methods for components like JFrame, JLabel, JTextField, and JButton, along with examples of window-based application programs. Additionally, it explains the delegation event model used for event handling in Swing applications.

Uploaded by

user23052036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

Swing in Java

The document provides an overview of Swing, a Java framework for creating GUI applications. It details key features, class hierarchy, and methods for components like JFrame, JLabel, JTextField, and JButton, along with examples of window-based application programs. Additionally, it explains the delegation event model used for event handling in Swing applications.

Uploaded by

user23052036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Swing

Prof. Debashis Hati


Email :[email protected]
Cell No: 9437028209/ 6370961683

07-JAN-2022 Prof. Debashis Hati


Swing
Java Program
1. Console Based application program
2. GUI or Window based application program

Swing provides powerful and flexible GUI


components which are used to
implement window based application
program.
Key Features of Swing
1. Swing components are lightweight. This
means that they are written entirely in
java. So there is no complication occurred
while using components in different
platforms.
2. Swing supports a pluggable look and feel
that means the look and feel of component
is separate from other logic of the
component.
Swing class hiearchy
Object

Component

Container

Panel Window JComponent

Applet Frame Swing Component

JApplet JFrame
Classes in Brief
• Component : is an abstract class that encapsulates
all the attributes of a visual component.
• Container: It has other methods that allow other
components nested within it.
• Panel : is an window which does not have title
bar,menu bar or border.
• Window: It creates a top level window which sits
directly on the desktop. You won’t create object of
Window directly,but you can use subclass of
Window; that is Frame or JFrame
Example
Window based Application Program
Steps for window based application
program using Swing
1. javax.swing package is imported.
2. One container (JFrame / JApplet) is selected.
Jframe is for stand alone(desktop) and JApplet is
for internet application.[Java Server Faces]
3. Component objects are created and are added
into the container.[into JFrame object)
4. Event handling codes are written for the added
components as per the requirement
Window program using JFrame

There are 2 ways to implement


1. The user defined class has a data member of
JFrame .
2. The user defined class is a sub class of JFrame
Let us add components only
Adding Label to an window.
Constructors Required
You have to remember the constructors and methods of
predefined JFrame ,Jlabel,JTextField andJButton classes.
Methods of Jframe class
Constructor
1. Jframe(String x) : displays the string in title bar.
2. void setSize(int width,int height): sets the dimension of
window in terms of pixels.
3. void setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE) :
terminates the program when the user closes the
application.
4. void setVisible(boolean ) : when the argument is true, the
window will be displayed.
Methods of JLabel class
 JLabel is a passive component.(does not respond to user
input)

 Jlabel(String)

 String getText()
 void setText(String)
Methods of JTextField class
 JTextField is an active component.(does respond to user
input)

 JTextField( )
 JTextField(int cols)
 JTextField(String)

 String getText()
 void setText(String)

When user presses enter after inputting text, ActionEvent


is generated.
Methods of JButton class
 JButton is an active component.(does respond to user
input) and ActionEvent is generated.

 JButton( String)

 setBounds(int x,int y,int width,int height)

String getActionCommand() : this method is defined in


ActionEvent class.
Defining my class
import javax.swing;
class MyWindow{

}
Defining my class
import javax.swing;
class MyWindow{
JFrame j;
JLabel l;

}
Defining my class
import javax.swing;
class MyWindow{
JFrame j;
JLabel l;
// constructor
MyWindow(){
j=new Jframe(“My Window Program”);
j.setSize(100,200);
j. SetDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
l=new JLabel(“Welcome”);
j.add(l);
j.setVisible(true);

}
}
Defining my class
import javax.swing;
class MyWindow{
JFrame j;
JLabel l;
MyWindow(){
j=new Jframe(“My Window Program”);
j.setSize(100,200);
j. SetDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
l=new JLabel(“Welcome”);
j.add(l);
j.setVisible(true);

public static void main(String s[]{

}
}
Defining my class
import javax.swing;
class MyWindow{
JFrame j;
JLabel l;
MyWindow(){
j=new Jframe(“My Window Program”);
j.setSize(100,200);
j. SetDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
l=new JLabel(“Welcome”);
j.add(l);
j.setVisible(true);

} public static void main(String s[]{


new MyWindow();
}
}
Another way Defining my class
import javax.swing;
class MyWindow extends Jframe{
Jlabel l;
MyWindow(){
super(“My Window Program”);
setSize(100,200);
setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
l=new JLabel(“Welcome”);
add(l);
setVisible(true);
}
public static void main(String s[]{
new MyWindow();
}
}
Passive & Active components

Passive Component: no interaction is possible with


the user. Example : Jlabel
Active Component: Interaction is possible with the
user. When the user interacts with this type of
component, an event is generated and that event
has to be handled or processed.
Example : Jbutton, JTextField,JCheckBox etc….
Delegation Event Model
Event Handling is the core of any window
based application like swing programming.
And is based on delegation event model.
Delegation event model consists of 3 objects
1. Source
2. Event
3. Listener
1. Source
1. Source : A source generates an event and sends to
one or more listeners . The listener simply waits
until it receives an event. Once received, the
listener processes the event.

A source must register a Listener to receive an


event.
public void addTypeListener(TypeListener e);
Type= name of the event
This method is defined in Component class.
Source of Events
Source of Events
Source Event Generated
JButton ActionEvent
JCheckBox ItemEvent
2. Event
2. Event : An event is an object that describes a state
change in a source. It happens when the user
interacts components in windows or GUI.
JButton,JRadio, JMenu,JList ActionEvent
JcheckboxItemEvent
3. Listener
3. Listener : is an object that is notified when an event
occurs. It has two major requirements
a) it must be registered with source
b)it must implement methods to handle or process event.
Listeners are defined by implementing predefined
interfaces available in java.awt.event package.
ActionListener
public void actionPerformed(ActionEvent e)
ItemListener
public void itemStateChanged(ItemEvent e)
Example
Let your window consists of one label which
is displayed as Hello and a button whose
caption is change. When the button is
clicked , the label will be changed to Bye.
How many components required??
One label JLabel
One Button Jbutton

First Define your frame class and it contains


two data members.
Adding Jbutton and JLabel
class MySwing2 implements ActionListener {
JFrame j
JLabel l;
JButton b
MySwing2(){
j=new Jframe(“My Window Program”);
j.setSize(100,200);
j. SetDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
l=new JLabel(“Welcome”);
b=new Jbutton(“Click”);
b.setBounds(100,100,60,40);
j.add(l);
j.add(b);
j.setVisible(true);
b.addActionListener(this);
}
Adding Jbutton and Jlabel
class MySwing2 extends JFrame implements ActionListener{
JButton b;
JLabel l;
MySwing2(){
super();
b=new JButton("click");
l=new JLabel("Welcome");
//b.setBounds(130,100,100,40);
setSize(400,600);
//setDefaultCloseOperation(super.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
//Container c=getContentPane();
//c.add(l);
//c.add(b);
add(l);
add(b);
b.addActionListener(this);

}
public static void main(String s[]){
new MySwing2();

}
public void actionPerformed(ActionEvent e){

if ((b.getActionCommand()).equals("click")){
l.setText("bye");
}
}
}

You might also like