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

Java Swing JFC

  • 1.
  • 2.
    GUI Basic Unit Basicunit 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 InterfaceComponents MENU Status Bar User ID Password SubmitSubmit Frame Panel Label Text Field Button
  • 4.
  • 5.
    www.SunilOS.com 5 GUI – DialogWindow MENU Status Bar User ID Password SubmitSubmitAre you sure you want to exit? Yes No
  • 6.
    www.SunilOS.com 6 Swing Components Top LevelContainer o JFrame o JDialog o JApplet
  • 7.
  • 8.
  • 9.
  • 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 Windowby 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 objectof 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 widgetsdisplay 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 Dineshto 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  JFrameframe = 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 staticvoid 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 staticvoid 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  publicstatic 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);  }
  • 25.
  • 26.
    www.SunilOS.com 26 Event Listeners Some Eventsand 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 classSimpleButtonHandler 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 usingAdapter  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 classWindowHandler 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 classTestWindowListner {  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
  • 33.
  • 34.
    www.SunilOS.com 34  MyWindow.java  publicclass 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  publicclass 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  publicclass 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 FocusListenerFocusAdapter 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  JButtonb = 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 typeis 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 staticvoid 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 voidmain(String[] args) { //Human h = new Human()//Incorrect Human h = Human.KID; h.display(); }
  • 48.
    www.SunilOS.com 48 TestHuman public static voidmain(String[] args) { Human[] h = Human.values(); For(int i=0;i<h.length;i++){ o S.o.p(h[i]); }
  • 49.
    Disclaimer This is aneducational 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
  • 50.