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

PPT
Java: GUI
PPTX
Event Handling in java
PPT
PYTHON - TKINTER - GUI - PART 1.ppt
PDF
Android Data Persistence
PPTX
C# Framework class library
PPTX
Components of .NET Framework
PPTX
Event handling
PPTX
Object Oriented Programming in Python
Java: GUI
Event Handling in java
PYTHON - TKINTER - GUI - PART 1.ppt
Android Data Persistence
C# Framework class library
Components of .NET Framework
Event handling
Object Oriented Programming in Python

What's hot (20)

PPT
JAVA OOP
PPTX
JAVA AWT
PPTX
Set data structure
PPT
Lists and scrollbars
PPTX
Functions in python slide share
PPT
Java Input Output and File Handling
PPS
Wrapper class
PPT
android layouts
PDF
Python programming : Files
PDF
JDBC : Java Database Connectivity
PPTX
this keyword in Java.pptx
PPTX
Machine learning ( Part 2 )
PPT
Exception Handling
PDF
Managing I/O in c++
PPT
Simple Java Programs
PPT
Python Part 1
PPT
C++ Memory Management
PDF
javascript objects
JAVA OOP
JAVA AWT
Set data structure
Lists and scrollbars
Functions in python slide share
Java Input Output and File Handling
Wrapper class
android layouts
Python programming : Files
JDBC : Java Database Connectivity
this keyword in Java.pptx
Machine learning ( Part 2 )
Exception Handling
Managing I/O in c++
Simple Java Programs
Python Part 1
C++ Memory Management
javascript objects
Ad

Viewers also liked (20)

PPT
Java Basics
PPT
Java Threads and Concurrency
PPT
JAVA Variables and Operators
PPT
C# Variables and Operators
PPT
Jsp/Servlet
PPT
Rays Technologies
PPT
Log4 J
PPT
C++ oop
PPT
Collections Framework
PPT
PPT
JavaScript
PPT
C# Basics
PPT
JUnit 4
PPT
Resource Bundle
PPT
java swing programming
PPT
Hibernate
PPT
Java Networking
PPT
Java swing
PPT
Java Swing
PPT
Swing and AWT in java
Java Basics
Java Threads and Concurrency
JAVA Variables and Operators
C# Variables and Operators
Jsp/Servlet
Rays Technologies
Log4 J
C++ oop
Collections Framework
JavaScript
C# Basics
JUnit 4
Resource Bundle
java swing programming
Hibernate
Java Networking
Java swing
Java Swing
Swing and AWT in java
Ad

Similar to Java Swing JFC (20)

PPT
Graphical User Interface (GUI) - 1
PPT
14a-gui.ppt
PDF
Getting started with GUI programming in Java_1
PDF
PDF
Swingpre 150616004959-lva1-app6892
PDF
Z blue introduction to gui (39023299)
PPTX
Chapter 11.2
PPT
L11cs2110sp13
PPTX
Introduction to java netbeans
PPT
28 awt
PPT
03_GUI.ppt
PDF
PraveenKumar A T AWS
DOCX
1 How do you create a frame (AWT or swing)How do you set th
PPTX
Java swing
PPTX
Abstract Window Toolkit_Event Handling_python
PPT
awt.ppt java windows programming lecture
PPT
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
PPT
1.Abstract windowing toolkit.ppt of AJP sub
Graphical User Interface (GUI) - 1
14a-gui.ppt
Getting started with GUI programming in Java_1
Swingpre 150616004959-lva1-app6892
Z blue introduction to gui (39023299)
Chapter 11.2
L11cs2110sp13
Introduction to java netbeans
28 awt
03_GUI.ppt
PraveenKumar A T AWS
1 How do you create a frame (AWT or swing)How do you set th
Java swing
Abstract Window Toolkit_Event Handling_python
awt.ppt java windows programming lecture
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
1.Abstract windowing toolkit.ppt of AJP sub

More from Sunil OS (17)

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

Recently uploaded (20)

PPTX
Congenital Hypothyroidism pptx
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
From loneliness to social connection charting
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Landforms and landscapes data surprise preview
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
How to Manage Bill Control Policy in Odoo 18
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Congenital Hypothyroidism pptx
Cardiovascular Pharmacology for pharmacy students.pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
The Final Stretch: How to Release a Game and Not Die in the Process.
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
Software Engineering BSC DS UNIT 1 .pptx
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
From loneliness to social connection charting
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
Week 4 Term 3 Study Techniques revisited.pptx
Open Quiz Monsoon Mind Game Prelims.pptx
Landforms and landscapes data surprise preview
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
How to Manage Bill Control Policy in Odoo 18
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

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