SlideShare a Scribd company logo
www.SunilOS.com 1
www.sunilos.com
www.raystec.com
GUI -JFC Swing
GUI Basic Unit
Basic unit of Graphical
User Interface (GUI) is
a Window.
A window is called
Frame in AWT or
Swing.
Frame is the top
container of all Visual
Graphical Controls.
www.SunilOS.com
2
www.SunilOS.com
3
Graphical User Interface Components
MENU
Status Bar
User ID
Password
SubmitSubmit
Frame
Panel
Label
Text
Field
Button
www.SunilOS.com
4
Desktop Application
Frame
Panel
Status
Bar
www.SunilOS.com
5
GUI – Dialog Window
MENU
Status Bar
User ID
Password
SubmitSubmitAre you sure you want to exit?
Yes No
www.SunilOS.com
6
Swing Components
Top Level Container
o JFrame
o JDialog
o JApplet
www.SunilOS.com
7
Other Components (Widgets)
JPanel
JLabel
JButton
JTextField
JPassword
JTextArea
JCheckbox
JRadioButton
www.SunilOS.com
8
Components Hierarchy
Object
Component
Container
JComponent
JMenuBar JPopupMenu JAbstractButton JSeparater
www.SunilOS.com
9
Package
javax.swing
java.awt
www.SunilOS.com
10
Create A Window
 public static void main(String[] args) {
 JFrame frame = new JFrame(“My First Window");
 JPanel pan = (JPanel)frame.getContentPane();
 JButton b = new JButton("Click Me");
 pan.add(b);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Close window when click on ‘x’
 frame.pack();
 //frame.setSize(400, 200);
 frame.setVisible(true);
 }
www.SunilOS.com
11
Create a Window by Extending JFrame
 public class Loginform extends JFrame{
 public Loginform(){
o super(“Login Form");
o JPanel pan = (JPanel)getContentPane();
o Jlabel lab1 = new Jlabel(“User ID”);
o pan.add(lab1);
o JtextField fl1 = new JTextField();
o pan.add(fl1);
o JButton button = new JButton(“Go");
o pan.add(button);
o setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
www.SunilOS.com
12
Create an object of extended JFrame
public static void main(String[] args) {
o Loginform frame = new Loginform ();
o frame.pack();
o frame.setVisible(true);
}
}
www.SunilOS.com
13
Layout Management
Arrange widgets display order
java.awt.FlowLayout
java.awt.BorderLayout (Default)
java.awt.GridLayout
java.awt.GridBagLayout
javax.swing.BoxLayout
GridLayout layout = new GridLayout(2,2);
frame.setLayout(layout);
www.SunilOS.com
14
Flow Layout – java.awt.FlowLayout
Button1Button1 Button2Button2 Button3Button3 Button4Button4 Button5Button5
Button6Button6 Button7Button7
www.SunilOS.com
15
Border Layout – java.awt.BorderLayout
WestButtonWestButton Center ButtonCenter Button
NorthButtonNorthButton
EastButtonEastButton
SouthButtonSouthButton
www.SunilOS.com
16
Grid Layout – java.awt.GridLayout
Button1Button1 Button2Button2 Button3Button3
Button4Button4 Button5Button5 Button6Button6
Button7Button7 Button8Button8 Button9Button9
Button10Button10 Button11Button11 Button12Button12
www.SunilOS.com
17
GridBag Layout – java.awt.GridBagLayout
Button1Button1 Button2Button2 Button3Button3
Button4Button4
Button7Button7 Button8Button8
Button9Button9
Button10Button10 Button11Button11
www.SunilOS.com
18
Messenger
Rohit to Dinesh>Hi
Dinesh to Rohit>Hi, Can you tell me about
JDBC.
Rohit to Dinesh>Why?
Dinesh to Rohit>I was absent yesterday.
Nakul to Dinesh>Don’t worry I am here to
help you.
Sheetal to Nakul>I would also like to
learn.
Rohit
Dinesh
Nakul
Ajhar
Pradeep
Saveeta
Nidhi
Sheetal
Enter Message Here SendSend
Rohit
LogoutLogout
www.SunilOS.com
19
Messenger
Rohit to Dinesh > Hi
Dinesh to Rohit > Hi, Can tell me about
JDBC
Rohit to Dinesh > Why
Dinesh to Rohit > I was absent
Nakul to Dinesh > Don’t worry I am here to
Help you
Sheetal to Nakul > I also would like to
Learn.
Rohit
Dinesh
Nakul
Ajhar
Pradeep
Saveeta
Nidhi
Sheetal
Enter Message Here SendSend
Rohit
LogoutLogout
www.SunilOS.com
20
Flow Layout
 JFrame frame = new JFrame(“Flow L Window");
 FlowLayout layout = new FlowLayout();
 //FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
 //FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
 frame.setLayout(layout);
 JPanel pan = (JPanel)frame.getContentPane();
 JButton b1 = new JButton("Button1"); pan.add(b1);
 JTextField text = new JTextField("Enter Text"); pan.add(text);
 JCheckBox cb = new JCheckBox("Select Here"); pan.add(cb);
 JButton b2 = new JButton("Button2"); pan.add(b2);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(400, 200);
 frame.setVisible(true);
www.SunilOS.com
21
BorderLayout
 public static void main(String[] args) {
 JFrame frame = new JFrame("MyFirstWindow");
 frame.setLayout(new BorderLayout());
 JPanel pan = (JPanel)frame.getContentPane();
 JButton b1 = new JButton("North"); pan.add(b1,BorderLayout.NORTH);
 JButton b2 = new JButton("South"); pan.add(b2, BorderLayout.SOUTH);
 JButton b3 = new JButton("Right"); pan.add(b3, BorderLayout.EAST);
 JButton b4 = new JButton("West"); pan.add(b4, BorderLayout.WEST);
 JButton b5 = new JButton("Center"); pan.add(b5);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.pack();
 frame.setVisible(true);
 }
www.SunilOS.com
22
GridLayout
 public static void main(String[] args) {
 JFrame frame = new JFrame("Meri First Window");
 JPanel pan = (JPanel)frame.getContentPane();
 pan.setLayout(new GridLayout(3,2));
 JButton b1 = new JButton("Button1"); pan.add(b1);
 JButton b2 = new JButton("Button2"); pan.add(b2);
 JButton b3 = new JButton("Button3"); pan.add(b3);
 JButton b4 = new JButton("Button4"); pan.add(b4);
 JButton b5 = new JButton("Button5"); pan.add(b5);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(400, 200);
 frame.setVisible(true);
 }
www.SunilOS.com
23
BoxLayout
 JFrame frame = new JFrame("My Box Layout Window");
 JPanel pan = (JPanel) frame.getContentPane();
 BoxLayout layout = new BoxLayout(pan, BoxLayout.X_AXIS);
 // BoxLayout layout =new BoxLayout(pan, BoxLayout.Y_AXIS);
 pan.setLayout(layout);
 JButton b1 = new JButton("Button1"); pan.add(b1);
 JTextField text = new JTextField("Enter Text"); pan.add(text);
 JButton b2 = new JButton("Button2"); pan.add(b2);
 JButton b3 = new JButton("Button3"); pan.add(b3);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.pack();
 frame.setVisible(true);
www.SunilOS.com
24
Absolute Position
 public static void main(String[] args) {
 JFrame frame = new JFrame("Meri First Window");
 JPanel pan = (JPanel)frame.getContentPane();
 pan.setLayout(null);
 JButton b1 = new JButton("Button1");
 b1.setSize(100,30);
 b1.setLocation(10,10);
 pan.add(b1);
 JButton b2 = new JButton("Button2");
 b2.setSize(100,30);
 b2.setLocation(10,50);
 pan.add(b2);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(400, 200);
 frame.setVisible(true);
 }
Event
www.SunilOS.com
25
www.SunilOS.com
26
Event Listeners
Some Events and Their Associated Event Listeners
Act that Results in the Event Listener Type
User clicks a button, presses Enter key while typing
in a text field, or chooses a menu item
ActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is
over a component
MouseListener
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Any property in a component changes such as
the text on a label
PropertyChangeListener
www.SunilOS.com
27
Event Object Hierarchy
 Java.lang.Object
o Java.util.EventObject
o Java.awt.AWTEvent
• Java.awt.event.ActionEvent
• Java.awt.event.ItemEvent
• Java.awt.event.AdjustmentEvent
• Java.awt.event.TextEvent
• Java.awt.event.ComponentEvent
• Java.awt.event.InputEvent
+ Java.awt.event.KeyEvent
+ Java.awt.event.MouseEvent
• Java.awt.event.FocusEvent
• Java.awt.event.ContainerEvent
• Java.awt.event.WindowEvent
www.SunilOS.com
28
SimpleButtonHandler
 public class SimpleButtonHandler implements ActionListener {
 public void actionPerformed(ActionEvent event) {
o JButton b = (JButton) event.getSource();
o String label = b.getText();
o if (label.equals("Click Me")) {
• b.setText("Don't Click Me");
o } else if (label.equals("Don't Click Me")) {
• b.setText("Click Me");
o } else {
• System.out.println("Button is Clicked");
o }
 }
 }
www.SunilOS.com
29
Mouse Listner using Adapter
 public class MouseHandler extends MouseAdapter {
o public void mouseEntered(MouseEvent event) {
o System.out.println("Mouse Enetred");
o }
o public void mouseExited(MouseEvent event) {
o System.out.println("Mouse Exit");
o }
 }
www.SunilOS.com
30
Apply Action Listner
 public class ButtonClickEvent {
 public static void main(String[] args) {
 JFrame frame = new JFrame("Meri First Window");
 JPanel pan = (JPanel)frame.getContentPane();
 pan.setLayout(new FlowLayout());
 JButton button = new JButton("Click Me");
 SimpleButtonHandler handler = new SimpleButtonHandler();
 button.addActionListener(handler); pan.add(button);
 JButton b = new JButton("Click Me & See Console");
 b.addActionListener(handler); pan.add(b);
 b.addMouseListener(new MouseHandler(););
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.pack();
 frame.setVisible(true);
 }
 }
www.SunilOS.com
31
WindowHandler
 public class WindowHandler implements WindowListener {
 public void windowActivated(WindowEvent e) { System.out.println("windowActivated"); }
 public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); }
 public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); }
 public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");
 }
 public void windowDeiconified(WindowEvent e)
{ System.out.println("windowDeiconified"); }
 public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); }
 public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } }
www.SunilOS.com
32
TestWindowListner
 public class TestWindowListner {
 public static void main(String[] args) {
 JFrame f = new JFrame("Test Window Events");
 WindowHandler wh = new WindowHandler(); f.addWindowListener(wh);
 JPanel p = (JPanel) f.getContentPane();
 p.setLayout(new FlowLayout(FlowLayout.LEFT));
 JButton b = new JButton("Send"); p.add(b);
 JButton login = new JButton("Login"); p.add(login);
 JTextField t = new JTextField("Enter TExt Here"); p.add(t);
 f.setSize(300, 200);
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.setVisible(true);
 }
 }
1. windowActivated
2. windowOpened
3. windowIconified
4. windowDeactivated
5. windowDeiconified
6. windowActivated
7. windowClosing
www.SunilOS.com
33
Add Listener
widget.addActionListener(al);
widget.addFocusListener(fl);
widget.addWindowListener(wl);
widget.addMouseListener(ml);
widget.addMouseMotionListener(mml);
www.SunilOS.com
34
 MyWindow.java
 public class MyWindow
extends JFrame {
 …
 }
 ButtonHandler.java
 public class ButtonHandler
implements ActionListener{
 …
 }
 MyWindow.class
 ButtonHandler.class
 MyWindow.java
 public class MyWindow
extends JFrame {
 …
 }
 class ButtonHandler
implements ActionListener{
 …
 }
 MyWindow.class
 ButtonHandler.class
1 Class = 1 File 1 File – n Class
ButtonHandler handler = new ButtonHandler()
www.SunilOS.com
35
 MyWindow.java
 public class MyWindow
extends JFrame {
 …
 public class ButtonHandler
implements ActionListener{
 …
 }//ButtonHandler
 }//MyWindow
 MyWindow.class
 MyWindow$ButtonHandler
.class
Inner Class Private Inner Class
MyWindow w = new MyWindow();
w.ButtonHandler h = w.new ButtonHandler();
 MyWindow.java
 public class MyWindow
extends JFrame {
 …
 private class ButtonHandler
implements
ActionListener{
 …
 }//ButtonHandler
 }//MyWindow
 MyWindow.class
 MyWindow$ButtonHandler
.class
www.SunilOS.com
36
 MyWindow.java
 public class MyWindow extends
JFrame {
 …
 public static class
ButtonHandler implements
ActionListener{
 …
 }
 }
 MyWindow.class
 MyWindow$ButtonHandler.cla
ss
Inner class can be
public
protected (Default)
private
static
Mainly used in Event
handling
Static Inner Class
MyWindow.ButtonHandler h = new MyWindow.ButtonHandler();
www.SunilOS.com
37
Inner Class - TestFocusListner
 public class TestFocusListner {
o private class InnFocusHandler implements FocusListner {
• public void focusGained(FocusEvent e) {
+ JButton b = (JButton) e.getSource();
+ System.out.println("Focus Gained " + b.getText());
• }
• public void focusLost(FocusEvent e) {
+ JButton b = (JButton) e.getSource();
+ System.out.println("Focus lost " + b.getText());}
o }
www.SunilOS.com
38
Inner Class – TestFocusListner (Cont..)
 public static void main(String[] args) {
 JFrame f = new JFrame("Test Focus List Win");
 JPanel p = (JPanel) f.getContentPane();
 p.setLayout(new FlowLayout(FlowLayout.LEFT));
 TestFocusListner tOuterClass = new TestFocusListner();
 InnFocusHandler innFl = tOuterClass.new InnFocusHandler();
 JButton b = new JButton("Send");
 b.addFocusListener(innFl); p.add(b);
 JButton login = new JButton("Login");
 login.addFocusListener(innFl); p.add(login);
 f.setSize(300, 200);
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.setVisible(true);
 }
www.SunilOS.com
39
Event Adapters
Interfaces Adapter
Classes
FocusListener FocusAdapter
MouseListner MouseAdapter
WindowListner WindowAdapter
www.SunilOS.com
40
Adapter vs Interface
 private static class
MyFocHandler extends
FocusAdapter {
 public void
focusGained(FocusEvent arg0) {
 System.out.println("Got Focus");
 }
 }
 private static class
MyFocHandler implements
FocusListner {
 public void
focusGained(FocusEvent a) {
 System.out.println("Got Focus");
 }
 public void
focusLost(FocusEvent a) {
 }
 }
www.SunilOS.com
41
Anonymous Classes
JButton b = new JButton(“Click Me");
FocusAdapter fa= new FocusAdapter();
b.addFocusListener(fa);
OR
JButton b = new JButton(“Click Me");
b.addFocusListener(new FocusAdapter());
www.SunilOS.com
42
Anonymous Classes
 JButton b = new JButton(“Click Me");
 b.addFocusListener(
o new FocusAdapter(){
o public void focusGained(FocusEvent e) {
+ JButton b = (JButton)e.getSource();
+ b.setBackground(Color.BLUE);
• }
o public void focusLost(FocusEvent e) {
• JButton b = (JButton)e.getSource();
• b.setBackground(Color.GRAY);
o }
o }
 );
www.SunilOS.com
43
Enum
An enum type is a datatype whose fields are fixed
set of constants
The enum declaration defines a class
The enum class body can include methods and
other fields
It has a static values() method that returns an
array containing all of the values
All enums implicitly extend java.lang.Enum.
enum cannot extend any other class.
www.SunilOS.com
44
public enum <name>
 package com.sunrays.enumpk;
 public enum Day {
 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY;
 public int getWeekDay() {
o switch (this) {
• case MONDAY:
• return 0;
o }
 }
 }
www.SunilOS.com
45
TestEnum.java
 public static void main(String[] args) {
 Day d;
 d = Day.SATURDAY;
 System.out.println(d.getWeekDay());
 switch (d) {
o case MONDAY:
o System.out.println("Mondays are bad.");
o break;
o case FRIDAY:
o System.out.println("Fridays are better.");
o break;
 }
www.SunilOS.com
46
ENUM with Constructor
 public enum Human {
 KID(10), MAN(50), OLDMAN(70);
 private final int weight;
 Human(int w) {
 this.weight = w;
 }
 public void display() {
 System.out.println(weight);
 }
 }
www.SunilOS.com
47
TestHuman
public static void main(String[] args) {
//Human h = new Human()//Incorrect
Human h = Human.KID;
h.display();
}
www.SunilOS.com
48
TestHuman
public static void main(String[] args) {
Human[] h = Human.values();
For(int i=0;i<h.length;i++){
o S.o.p(h[i]);
}
Disclaimer
This is an educational presentation to enhance the
skill of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are
used in this presentation to simplify technical
examples and correlate examples with the real
world.
We are grateful to owners of these URLs and
pictures.
www.SunilOS.com 49
Thank You!
www.SunilOS.com 50
www.SunilOS.com

More Related Content

What's hot (20)

PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPT
Hibernate
Sunil OS
 
PDF
Introduction to java (revised)
Sujit Majety
 
PPT
JAVA Variables and Operators
Sunil OS
 
PPTX
Awt, Swing, Layout managers
swapnac12
 
PPT
Hibernate
Ajay K
 
PPT
Collection v3
Sunil OS
 
PDF
Java 8 features
NexThoughts Technologies
 
PPT
Java 8 - CJ
Sunil OS
 
PPT
Java Threads and Concurrency
Sunil OS
 
PPTX
Machine learning ( Part 2 )
Sunil OS
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PDF
Files in java
Muthukumaran Subramanian
 
PDF
OOPs & Inheritance Notes
Shalabh Chaudhary
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPT
Java awt
Arati Gadgil
 
PPTX
swings.pptx
Parameshwar Maddela
 
PPT
Java Basics
Sunil OS
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Hibernate
Sunil OS
 
Introduction to java (revised)
Sujit Majety
 
JAVA Variables and Operators
Sunil OS
 
Awt, Swing, Layout managers
swapnac12
 
Hibernate
Ajay K
 
Collection v3
Sunil OS
 
Java 8 features
NexThoughts Technologies
 
Java 8 - CJ
Sunil OS
 
Java Threads and Concurrency
Sunil OS
 
Machine learning ( Part 2 )
Sunil OS
 
Classes objects in java
Madishetty Prathibha
 
OOPs & Inheritance Notes
Shalabh Chaudhary
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Polymorphism in java
Elizabeth alexander
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Java awt
Arati Gadgil
 
swings.pptx
Parameshwar Maddela
 
Java Basics
Sunil OS
 

Viewers also liked (19)

PPT
C# Variables and Operators
Sunil OS
 
PPT
Jsp/Servlet
Sunil OS
 
PPT
Rays Technologies
Sunil OS
 
PPT
Log4 J
Sunil OS
 
PPT
C++ oop
Sunil OS
 
PPT
C++
Sunil OS
 
PPT
JavaScript
Sunil OS
 
PPT
C# Basics
Sunil OS
 
PPT
JUnit 4
Sunil OS
 
PPT
Resource Bundle
Sunil OS
 
PPT
java swing programming
Ankit Desai
 
PPT
Java Networking
Sunil OS
 
PPT
Java swing
Nataraj Dg
 
PPT
Java Swing
Shraddha
 
PPT
Swing and AWT in java
Adil Mehmoood
 
PDF
java swing tutorial for beginners(java programming tutorials)
Daroko blog(www.professionalbloggertricks.com)
 
PPT
java swing
vannarith
 
PPT
GUI Programming In Java
yht4ever
 
PPTX
Java Swing
Komal Gandhi
 
C# Variables and Operators
Sunil OS
 
Jsp/Servlet
Sunil OS
 
Rays Technologies
Sunil OS
 
Log4 J
Sunil OS
 
C++ oop
Sunil OS
 
JavaScript
Sunil OS
 
C# Basics
Sunil OS
 
JUnit 4
Sunil OS
 
Resource Bundle
Sunil OS
 
java swing programming
Ankit Desai
 
Java Networking
Sunil OS
 
Java swing
Nataraj Dg
 
Java Swing
Shraddha
 
Swing and AWT in java
Adil Mehmoood
 
java swing tutorial for beginners(java programming tutorials)
Daroko blog(www.professionalbloggertricks.com)
 
java swing
vannarith
 
GUI Programming In Java
yht4ever
 
Java Swing
Komal Gandhi
 
Ad

Similar to Java Swing JFC (20)

PPT
Graphical User Interface (GUI) - 1
PRN USM
 
PPT
14a-gui.ppt
DrDGayathriDevi
 
PDF
Getting started with GUI programming in Java_1
Muhammad Shebl Farag
 
PDF
Gui
Sardar Alam
 
PDF
Swingpre 150616004959-lva1-app6892
renuka gavli
 
PDF
Z blue introduction to gui (39023299)
Narayana Swamy
 
PPTX
Chapter 11.2
sotlsoc
 
PPT
L11cs2110sp13
karan saini
 
PPTX
Introduction to java netbeans
Shrey Goswami
 
PPT
28 awt
Prachi Vijh
 
PPT
03_GUI.ppt
DrDGayathriDevi
 
PDF
PraveenKumar A T AWS
Praveen Kumar
 
DOCX
1 How do you create a frame (AWT or swing)How do you set th
hirstcruz
 
PPTX
Java swing
ssuser3a47cb
 
PPTX
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
PPT
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
PPT
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
PPTX
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
Graphical User Interface (GUI) - 1
PRN USM
 
14a-gui.ppt
DrDGayathriDevi
 
Getting started with GUI programming in Java_1
Muhammad Shebl Farag
 
Swingpre 150616004959-lva1-app6892
renuka gavli
 
Z blue introduction to gui (39023299)
Narayana Swamy
 
Chapter 11.2
sotlsoc
 
L11cs2110sp13
karan saini
 
Introduction to java netbeans
Shrey Goswami
 
28 awt
Prachi Vijh
 
03_GUI.ppt
DrDGayathriDevi
 
PraveenKumar A T AWS
Praveen Kumar
 
1 How do you create a frame (AWT or swing)How do you set th
hirstcruz
 
Java swing
ssuser3a47cb
 
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
Ad

More from Sunil OS (16)

PPT
Threads V4
Sunil OS
 
PPT
Java IO Streams V4
Sunil OS
 
PPT
OOP V3.1
Sunil OS
 
PPT
Java Basics V3
Sunil OS
 
PPT
DJango
Sunil OS
 
PPT
PDBC
Sunil OS
 
PPT
OOP v3
Sunil OS
 
PPT
Threads v3
Sunil OS
 
PPT
Exception Handling v3
Sunil OS
 
PPTX
Machine learning ( Part 3 )
Sunil OS
 
PPTX
Machine learning ( Part 1 )
Sunil OS
 
PPT
Python Pandas
Sunil OS
 
PPT
Python part2 v1
Sunil OS
 
PPT
Angular 8
Sunil OS
 
PPT
Python Part 1
Sunil OS
 
PPT
C Basics
Sunil OS
 
Threads V4
Sunil OS
 
Java IO Streams V4
Sunil OS
 
OOP V3.1
Sunil OS
 
Java Basics V3
Sunil OS
 
DJango
Sunil OS
 
PDBC
Sunil OS
 
OOP v3
Sunil OS
 
Threads v3
Sunil OS
 
Exception Handling v3
Sunil OS
 
Machine learning ( Part 3 )
Sunil OS
 
Machine learning ( Part 1 )
Sunil OS
 
Python Pandas
Sunil OS
 
Python part2 v1
Sunil OS
 
Angular 8
Sunil OS
 
Python Part 1
Sunil OS
 
C Basics
Sunil OS
 

Recently uploaded (20)

PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 

Java Swing JFC

  • 2. GUI Basic Unit Basic unit of Graphical User Interface (GUI) is a Window. A window is called Frame in AWT or Swing. Frame is the top container of all Visual Graphical Controls. www.SunilOS.com 2
  • 3. www.SunilOS.com 3 Graphical User Interface Components MENU Status Bar User ID Password SubmitSubmit Frame Panel Label Text Field Button
  • 5. www.SunilOS.com 5 GUI – Dialog Window MENU Status Bar User ID Password SubmitSubmitAre you sure you want to exit? Yes No
  • 6. www.SunilOS.com 6 Swing Components Top Level Container o JFrame o JDialog o JApplet
  • 10. www.SunilOS.com 10 Create A Window  public static void main(String[] args) {  JFrame frame = new JFrame(“My First Window");  JPanel pan = (JPanel)frame.getContentPane();  JButton b = new JButton("Click Me");  pan.add(b);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close window when click on ‘x’  frame.pack();  //frame.setSize(400, 200);  frame.setVisible(true);  }
  • 11. www.SunilOS.com 11 Create a Window by Extending JFrame  public class Loginform extends JFrame{  public Loginform(){ o super(“Login Form"); o JPanel pan = (JPanel)getContentPane(); o Jlabel lab1 = new Jlabel(“User ID”); o pan.add(lab1); o JtextField fl1 = new JTextField(); o pan.add(fl1); o JButton button = new JButton(“Go"); o pan.add(button); o setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  }
  • 12. www.SunilOS.com 12 Create an object of extended JFrame public static void main(String[] args) { o Loginform frame = new Loginform (); o frame.pack(); o frame.setVisible(true); } }
  • 13. www.SunilOS.com 13 Layout Management Arrange widgets display order java.awt.FlowLayout java.awt.BorderLayout (Default) java.awt.GridLayout java.awt.GridBagLayout javax.swing.BoxLayout GridLayout layout = new GridLayout(2,2); frame.setLayout(layout);
  • 14. www.SunilOS.com 14 Flow Layout – java.awt.FlowLayout Button1Button1 Button2Button2 Button3Button3 Button4Button4 Button5Button5 Button6Button6 Button7Button7
  • 15. www.SunilOS.com 15 Border Layout – java.awt.BorderLayout WestButtonWestButton Center ButtonCenter Button NorthButtonNorthButton EastButtonEastButton SouthButtonSouthButton
  • 16. www.SunilOS.com 16 Grid Layout – java.awt.GridLayout Button1Button1 Button2Button2 Button3Button3 Button4Button4 Button5Button5 Button6Button6 Button7Button7 Button8Button8 Button9Button9 Button10Button10 Button11Button11 Button12Button12
  • 17. www.SunilOS.com 17 GridBag Layout – java.awt.GridBagLayout Button1Button1 Button2Button2 Button3Button3 Button4Button4 Button7Button7 Button8Button8 Button9Button9 Button10Button10 Button11Button11
  • 18. www.SunilOS.com 18 Messenger Rohit to Dinesh>Hi Dinesh to Rohit>Hi, Can you tell me about JDBC. Rohit to Dinesh>Why? Dinesh to Rohit>I was absent yesterday. Nakul to Dinesh>Don’t worry I am here to help you. Sheetal to Nakul>I would also like to learn. Rohit Dinesh Nakul Ajhar Pradeep Saveeta Nidhi Sheetal Enter Message Here SendSend Rohit LogoutLogout
  • 19. www.SunilOS.com 19 Messenger Rohit to Dinesh > Hi Dinesh to Rohit > Hi, Can tell me about JDBC Rohit to Dinesh > Why Dinesh to Rohit > I was absent Nakul to Dinesh > Don’t worry I am here to Help you Sheetal to Nakul > I also would like to Learn. Rohit Dinesh Nakul Ajhar Pradeep Saveeta Nidhi Sheetal Enter Message Here SendSend Rohit LogoutLogout
  • 20. www.SunilOS.com 20 Flow Layout  JFrame frame = new JFrame(“Flow L Window");  FlowLayout layout = new FlowLayout();  //FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);  //FlowLayout layout = new FlowLayout(FlowLayout.LEFT);  frame.setLayout(layout);  JPanel pan = (JPanel)frame.getContentPane();  JButton b1 = new JButton("Button1"); pan.add(b1);  JTextField text = new JTextField("Enter Text"); pan.add(text);  JCheckBox cb = new JCheckBox("Select Here"); pan.add(cb);  JButton b2 = new JButton("Button2"); pan.add(b2);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.setSize(400, 200);  frame.setVisible(true);
  • 21. www.SunilOS.com 21 BorderLayout  public static void main(String[] args) {  JFrame frame = new JFrame("MyFirstWindow");  frame.setLayout(new BorderLayout());  JPanel pan = (JPanel)frame.getContentPane();  JButton b1 = new JButton("North"); pan.add(b1,BorderLayout.NORTH);  JButton b2 = new JButton("South"); pan.add(b2, BorderLayout.SOUTH);  JButton b3 = new JButton("Right"); pan.add(b3, BorderLayout.EAST);  JButton b4 = new JButton("West"); pan.add(b4, BorderLayout.WEST);  JButton b5 = new JButton("Center"); pan.add(b5);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.pack();  frame.setVisible(true);  }
  • 22. www.SunilOS.com 22 GridLayout  public static void main(String[] args) {  JFrame frame = new JFrame("Meri First Window");  JPanel pan = (JPanel)frame.getContentPane();  pan.setLayout(new GridLayout(3,2));  JButton b1 = new JButton("Button1"); pan.add(b1);  JButton b2 = new JButton("Button2"); pan.add(b2);  JButton b3 = new JButton("Button3"); pan.add(b3);  JButton b4 = new JButton("Button4"); pan.add(b4);  JButton b5 = new JButton("Button5"); pan.add(b5);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.setSize(400, 200);  frame.setVisible(true);  }
  • 23. www.SunilOS.com 23 BoxLayout  JFrame frame = new JFrame("My Box Layout Window");  JPanel pan = (JPanel) frame.getContentPane();  BoxLayout layout = new BoxLayout(pan, BoxLayout.X_AXIS);  // BoxLayout layout =new BoxLayout(pan, BoxLayout.Y_AXIS);  pan.setLayout(layout);  JButton b1 = new JButton("Button1"); pan.add(b1);  JTextField text = new JTextField("Enter Text"); pan.add(text);  JButton b2 = new JButton("Button2"); pan.add(b2);  JButton b3 = new JButton("Button3"); pan.add(b3);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.pack();  frame.setVisible(true);
  • 24. www.SunilOS.com 24 Absolute Position  public static void main(String[] args) {  JFrame frame = new JFrame("Meri First Window");  JPanel pan = (JPanel)frame.getContentPane();  pan.setLayout(null);  JButton b1 = new JButton("Button1");  b1.setSize(100,30);  b1.setLocation(10,10);  pan.add(b1);  JButton b2 = new JButton("Button2");  b2.setSize(100,30);  b2.setLocation(10,50);  pan.add(b2);  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.setSize(400, 200);  frame.setVisible(true);  }
  • 26. www.SunilOS.com 26 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the Event Listener Type User clicks a button, presses Enter key while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener
  • 27. www.SunilOS.com 27 Event Object Hierarchy  Java.lang.Object o Java.util.EventObject o Java.awt.AWTEvent • Java.awt.event.ActionEvent • Java.awt.event.ItemEvent • Java.awt.event.AdjustmentEvent • Java.awt.event.TextEvent • Java.awt.event.ComponentEvent • Java.awt.event.InputEvent + Java.awt.event.KeyEvent + Java.awt.event.MouseEvent • Java.awt.event.FocusEvent • Java.awt.event.ContainerEvent • Java.awt.event.WindowEvent
  • 28. www.SunilOS.com 28 SimpleButtonHandler  public class SimpleButtonHandler implements ActionListener {  public void actionPerformed(ActionEvent event) { o JButton b = (JButton) event.getSource(); o String label = b.getText(); o if (label.equals("Click Me")) { • b.setText("Don't Click Me"); o } else if (label.equals("Don't Click Me")) { • b.setText("Click Me"); o } else { • System.out.println("Button is Clicked"); o }  }  }
  • 29. www.SunilOS.com 29 Mouse Listner using Adapter  public class MouseHandler extends MouseAdapter { o public void mouseEntered(MouseEvent event) { o System.out.println("Mouse Enetred"); o } o public void mouseExited(MouseEvent event) { o System.out.println("Mouse Exit"); o }  }
  • 30. www.SunilOS.com 30 Apply Action Listner  public class ButtonClickEvent {  public static void main(String[] args) {  JFrame frame = new JFrame("Meri First Window");  JPanel pan = (JPanel)frame.getContentPane();  pan.setLayout(new FlowLayout());  JButton button = new JButton("Click Me");  SimpleButtonHandler handler = new SimpleButtonHandler();  button.addActionListener(handler); pan.add(button);  JButton b = new JButton("Click Me & See Console");  b.addActionListener(handler); pan.add(b);  b.addMouseListener(new MouseHandler(););  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.pack();  frame.setVisible(true);  }  }
  • 31. www.SunilOS.com 31 WindowHandler  public class WindowHandler implements WindowListener {  public void windowActivated(WindowEvent e) { System.out.println("windowActivated"); }  public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); }  public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); }  public void windowDeactivated(WindowEvent e) { System.out.println("windowDeactivated");  }  public void windowDeiconified(WindowEvent e) { System.out.println("windowDeiconified"); }  public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); }  public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } }
  • 32. www.SunilOS.com 32 TestWindowListner  public class TestWindowListner {  public static void main(String[] args) {  JFrame f = new JFrame("Test Window Events");  WindowHandler wh = new WindowHandler(); f.addWindowListener(wh);  JPanel p = (JPanel) f.getContentPane();  p.setLayout(new FlowLayout(FlowLayout.LEFT));  JButton b = new JButton("Send"); p.add(b);  JButton login = new JButton("Login"); p.add(login);  JTextField t = new JTextField("Enter TExt Here"); p.add(t);  f.setSize(300, 200);  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  f.setVisible(true);  }  } 1. windowActivated 2. windowOpened 3. windowIconified 4. windowDeactivated 5. windowDeiconified 6. windowActivated 7. windowClosing
  • 34. www.SunilOS.com 34  MyWindow.java  public class MyWindow extends JFrame {  …  }  ButtonHandler.java  public class ButtonHandler implements ActionListener{  …  }  MyWindow.class  ButtonHandler.class  MyWindow.java  public class MyWindow extends JFrame {  …  }  class ButtonHandler implements ActionListener{  …  }  MyWindow.class  ButtonHandler.class 1 Class = 1 File 1 File – n Class ButtonHandler handler = new ButtonHandler()
  • 35. www.SunilOS.com 35  MyWindow.java  public class MyWindow extends JFrame {  …  public class ButtonHandler implements ActionListener{  …  }//ButtonHandler  }//MyWindow  MyWindow.class  MyWindow$ButtonHandler .class Inner Class Private Inner Class MyWindow w = new MyWindow(); w.ButtonHandler h = w.new ButtonHandler();  MyWindow.java  public class MyWindow extends JFrame {  …  private class ButtonHandler implements ActionListener{  …  }//ButtonHandler  }//MyWindow  MyWindow.class  MyWindow$ButtonHandler .class
  • 36. www.SunilOS.com 36  MyWindow.java  public class MyWindow extends JFrame {  …  public static class ButtonHandler implements ActionListener{  …  }  }  MyWindow.class  MyWindow$ButtonHandler.cla ss Inner class can be public protected (Default) private static Mainly used in Event handling Static Inner Class MyWindow.ButtonHandler h = new MyWindow.ButtonHandler();
  • 37. www.SunilOS.com 37 Inner Class - TestFocusListner  public class TestFocusListner { o private class InnFocusHandler implements FocusListner { • public void focusGained(FocusEvent e) { + JButton b = (JButton) e.getSource(); + System.out.println("Focus Gained " + b.getText()); • } • public void focusLost(FocusEvent e) { + JButton b = (JButton) e.getSource(); + System.out.println("Focus lost " + b.getText());} o }
  • 38. www.SunilOS.com 38 Inner Class – TestFocusListner (Cont..)  public static void main(String[] args) {  JFrame f = new JFrame("Test Focus List Win");  JPanel p = (JPanel) f.getContentPane();  p.setLayout(new FlowLayout(FlowLayout.LEFT));  TestFocusListner tOuterClass = new TestFocusListner();  InnFocusHandler innFl = tOuterClass.new InnFocusHandler();  JButton b = new JButton("Send");  b.addFocusListener(innFl); p.add(b);  JButton login = new JButton("Login");  login.addFocusListener(innFl); p.add(login);  f.setSize(300, 200);  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  f.setVisible(true);  }
  • 39. www.SunilOS.com 39 Event Adapters Interfaces Adapter Classes FocusListener FocusAdapter MouseListner MouseAdapter WindowListner WindowAdapter
  • 40. www.SunilOS.com 40 Adapter vs Interface  private static class MyFocHandler extends FocusAdapter {  public void focusGained(FocusEvent arg0) {  System.out.println("Got Focus");  }  }  private static class MyFocHandler implements FocusListner {  public void focusGained(FocusEvent a) {  System.out.println("Got Focus");  }  public void focusLost(FocusEvent a) {  }  }
  • 41. www.SunilOS.com 41 Anonymous Classes JButton b = new JButton(“Click Me"); FocusAdapter fa= new FocusAdapter(); b.addFocusListener(fa); OR JButton b = new JButton(“Click Me"); b.addFocusListener(new FocusAdapter());
  • 42. www.SunilOS.com 42 Anonymous Classes  JButton b = new JButton(“Click Me");  b.addFocusListener( o new FocusAdapter(){ o public void focusGained(FocusEvent e) { + JButton b = (JButton)e.getSource(); + b.setBackground(Color.BLUE); • } o public void focusLost(FocusEvent e) { • JButton b = (JButton)e.getSource(); • b.setBackground(Color.GRAY); o } o }  );
  • 43. www.SunilOS.com 43 Enum An enum type is a datatype whose fields are fixed set of constants The enum declaration defines a class The enum class body can include methods and other fields It has a static values() method that returns an array containing all of the values All enums implicitly extend java.lang.Enum. enum cannot extend any other class.
  • 44. www.SunilOS.com 44 public enum <name>  package com.sunrays.enumpk;  public enum Day {  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;  public int getWeekDay() { o switch (this) { • case MONDAY: • return 0; o }  }  }
  • 45. www.SunilOS.com 45 TestEnum.java  public static void main(String[] args) {  Day d;  d = Day.SATURDAY;  System.out.println(d.getWeekDay());  switch (d) { o case MONDAY: o System.out.println("Mondays are bad."); o break; o case FRIDAY: o System.out.println("Fridays are better."); o break;  }
  • 46. www.SunilOS.com 46 ENUM with Constructor  public enum Human {  KID(10), MAN(50), OLDMAN(70);  private final int weight;  Human(int w) {  this.weight = w;  }  public void display() {  System.out.println(weight);  }  }
  • 47. www.SunilOS.com 47 TestHuman public static void main(String[] args) { //Human h = new Human()//Incorrect Human h = Human.KID; h.display(); }
  • 48. www.SunilOS.com 48 TestHuman public static void main(String[] args) { Human[] h = Human.values(); For(int i=0;i<h.length;i++){ o S.o.p(h[i]); }
  • 49. Disclaimer This is an educational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 49