SlideShare a Scribd company logo
1 of 50
Download to read offline
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
1
Prepared By: Asst. Prof. Sejal Jadav
Unit-5
CS – 19 : Programming with Java
UNIT – 5
GUI using SWING, Event Handling
B.C.A & B.SC.(IT) – 4
Prepared By: Asst. Prof. Sejal Jadav
Q: JPanel
• The JPanel class is used to create a panel.
• We can add our components to a panel and the panel should be added
to a container such as a frame.
• Now to draw the applet window, following method is used instead of
the paint() method:
public void paintComponent(Graphics g)
Prepared By: Asst. Prof. Sejal Jadav
Q: JFrame
• The JFrame class creates a frame which is a window.
• This window has a border, a title and buttons for closing, minimizing
and maximizing the window.
• The constructors for JFrame are:
Prepared By: Asst. Prof. Sejal Jadav
• JFrame()
• JFrame(String title)
• Here the title specifies the title of frame window.
• To handle the window closing events, there is WindowListener
interface which to contains the methods such as windowClosing() etc.
Prepared By: Asst. Prof. Sejal Jadav
• For example:
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
Prepared By: Asst. Prof. Sejal Jadav
• To set the default closing behavior following method is used:
void setDefaultCloseOperation(int op)
• The op is a constant of the windowConstants interface.
• These constants are:
• HIDE_ON_CLOSE: It hides the window when you press the close button,
but still in remains alive. This is the default operation.
Prepared By: Asst. Prof. Sejal Jadav
• DISPOSE_ON_CLOSE: This hides and disposes(remove) the window
when you press the close button in the title bar. It removes the window
from the screen and removes it from the memory also.
• EXIT_ON_CLOSE: It exits the whole application on pressing the close
button -> Sytem.exit(0) method.
Prepared By: Asst. Prof. Sejal Jadav
• DO_NOTHING_ON_CLOSE: It does nothing when the close button is
pressed.
Prepared By: Asst. Prof. Sejal Jadav
Some others methods of JFrame are:
• int getDefaultCloseOperation(int operation)
• Here, operation is any WindowConstants described before.
• Dimension getSize()
• It returns a Dimension object specifying the size.
Prepared By: Asst. Prof. Sejal Jadav
• void setSize(Dimension d)
• d -> the specified dimension for the width and height values
• Creates an instance of Dimension whose width and height are the same
as for the specified dimension.
Prepared By: Asst. Prof. Sejal Jadav
• void setSize(int w , int h )
• It set the size of frame by width w and height h.
• void setLocation(int x, int y)
• It position the frame at x and y coordinates.
Prepared By: Asst. Prof. Sejal Jadav
void setBounds(int x, int y, int w , int h)
• It set the size and position of the frame window.
• The x and y are the coordinates of the upper-left corner of the frame
and w and h are the width and height of the window.
Prepared By: Asst. Prof. Sejal Jadav
void setBounds(Rectangle r)
• It set the size and position of the frame to the rectangle.
Rectangle getBounds()
• It returns the Rectangle object specifying the size and position of the frame.
void pack()
• It set the size of the frame window, so that all the components have same or
above their normal size.
Prepared By: Asst. Prof. Sejal Jadav
• Example: JFrameEx.java
Prepared By: Asst. Prof. Sejal Jadav
Q: JToggleButton
• A JToggleButton is a two-state button. The two states are selected and
unselected.
• The JRadioButton and JCheckBox classes are subclasses of this class.
• When the user presses the toggle button, it toggles between being
pressed or unpressed.
Prepared By: Asst. Prof. Sejal Jadav
 Constructors in JToggleButton:
• JToggleButton(): Creates an initially unselected toggle button without
setting the text or image.
• JToggleButton(String text, boolean selected): Creates a toggle button
with the specified text and selection state.
Prepared By: Asst. Prof. Sejal Jadav
• JToggleButton(String text, Icon icon, boolean selected): Creates a
toggle button with the specified text, image, and selection state.
Prepared By: Asst. Prof. Sejal Jadav
• setDefaultCloseOperation():-> is used to set close option for frame.
• Example:
• JRadioButtonDemo.java
• JToggleButtonExamp.java (after Q-9: ItemEvent Class and Q-15
Event Listener Interface)
Prepared By: Asst. Prof. Sejal Jadav
Q-9 Explain ItemEvent Class.
• An ItemEvent is generated when a checkbox or a list item is clicked or
when a checkable menu item is selected or deselected.
• There are two types of itemevents, which are identified by the
following integer constants:
Prepared By: Asst. Prof. Sejal Jadav
SELECTED
• It is set when an item is selected.
DESELECTED
• It is set when an item is deselected.
ITEM_STATE_CHANGED
• It is set when the state of an item is changed i.e selected or deselected.
Prepared By: Asst. Prof. Sejal Jadav
• Its method is:
Object getItem( )
• It’s the item that generated this event
ItemSelectable getItemSelectable( )
• It returns the ItemSelecteable object that generated this event.
int getStateChange()
• It returns the change of state i.e. SELECTED or DESELECTED
Prepared By: Asst. Prof. Sejal Jadav
Q-15 Explain Event Listener interfaces.
• The ItemListener Interface:
• This interface defines the itemStateChanged( ) method that is invoked
when the state of an item changes.
• Its general form is shown here:
void itemStateChanged(ItemEvent ie)
Prepared By: Asst. Prof. Sejal Jadav
Q: JTabbedPane
• The JTabbedPane class is used to switch between a group of
components by clicking on a tab with a given title. It inherits
JComponent class.
Constructor: JTabbedPane()
Example: TabbedPaneExample.java
Prepared By: Asst. Prof. Sejal Jadav
Q: JProgressBar
• The JProgressBar class is used to display the progress of the task.
• It inherits JComponent class.
• Commonly used Constructors:
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
 Commonly used Methods:
Prepared By: Asst. Prof. Sejal Jadav
• Example:ProgressBarExample.java
Prepared By: Asst. Prof. Sejal Jadav
Q: JSlider
• The Java JSlider class is used to create the slider.
• By using JSlider, a user can select a value from a specific range.
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Methods of JSlider class
Prepared By: Asst. Prof. Sejal Jadav
• Example:
• SliderExample.java
• SliderExample1.java
Prepared By: Asst. Prof. Sejal Jadav
Q- NOLayout Manager
• We know earlier in "Java Layout Managers" that two styles of placing
components in containers – pixel format and position format. Now let
us discuss clearly what pixel format is and how to place a component in
terms of pixels.
Prepared By: Asst. Prof. Sejal Jadav
• We also know earlier, the default layout manager for Frame is
BorderLayout and for Panel and Applet, it is FlowLayout.
• To use pixel format, first we must set the default layout manager of the
container to null.
• To give the size and location, setBounds() method is used.
• But, this style is very rarely used (to avoid the complexity of
GridBagLayout).
Prepared By: Asst. Prof. Sejal Jadav
• Following is the method signature as defined in java.awt.Component
class
setBounds(int x, int y, int width, int height)
• First two coordinates x and y give the position in the container and
the last two width and height give the size of the component.
Prepared By: Asst. Prof. Sejal Jadav
• In the following program, one label and two buttons are placed using
setBounds() method.
• Example:NullLayoutManager.java
Prepared By: Asst. Prof. Sejal Jadav
super(“No Layout Manager”);
• This is another style of using setTitle().
• The super class constructor of Frame is called, with super(), and
passed the string as parameter and this string is placed on the frame
title bar.
Prepared By: Asst. Prof. Sejal Jadav
setLayout(null);
• To use setBounds() method, first the default layout manager
BorderLayout of frame should be set to null.
Prepared By: Asst. Prof. Sejal Jadav
sign.setBounds(80, 35, 130, 25);
agreedBut.setBounds(60, 75, 75, 25);
• The label sign is placed at x-coordinate of 80 and y-coodinate 35 pixels.
The width of the label is given as 130 pixels and height as 25 pixels.
• Simialrly, the agreeBut is placed at 60 and 75 coordinates with 75 pixels
width and 25 height.
Prepared By: Asst. Prof. Sejal Jadav
• Example: NoLayoutTest.java
Prepared By: Asst. Prof. Sejal Jadav
Event Handling
Unit-5(Part-2)
Prepared By: Asst. Prof. Sejal Jadav
Q.1 what is event delegation model?
• The event delegation model is a mechanism that handles the generating
and processing of events.
• This mechanism works on a simple concept. An event source generates
an event and it is sent to one or more listeners.
Prepared By: Asst. Prof. Sejal Jadav
• When the listeners listens the event, it processes the event. To
listen to an event, the listener must be registered with the event
source.
• To understand the event delegation model we have to understand
about events, source of event and event listeners.
Prepared By: Asst. Prof. Sejal Jadav
1. Event
• An event is said to be happened when a phenomenon(event)
occurs like the mouse is moved, mouse button is clicked, a key is
pressed, a button is pressed or an item is selected from the list etc.
In java an event is an object that describes this change of state in a
source that generated the event.
Prepared By: Asst. Prof. Sejal Jadav
2. Source of event
• A source is an object that can generate an event. A source can generate
more than one type. An event source must register to its listener in
order to listen to its events. To register an event, there are methods for
each type of event listener.
• The general from of event registration method is:
public void addEventTypeListener(EventTypeListener obj)
Prepared By: Asst. Prof. Sejal Jadav
public void addEventTypeListener(EventTypeListener obj)
• Here EventType is the name of the event and obj is the object of the
class implements the EventTypeListener interface.
• In most cases, the obj will be ‘this’ object.
• For example to register the mouse event following method is used:
sourceObj.addMouseListener(this)
Prepared By: Asst. Prof. Sejal Jadav
• Some event sources allow only one listener to register. This type of listener
methods can throw a TooManyListenersException which is in java.util
package.
• To unregister an event following method form is used:
public void removeEventTypeListener ( EventType obj)
• For example to remove a keyword listener, removeKeyListener() method is
used.
Prepared By: Asst. Prof. Sejal Jadav
3. Event Listeners:
• An event listener is one that listens or understands the event.
• In java an event listener is an object. In order to receive the event
notification, the event object has to be registered with the event and its
class must implement all the methods of the event listener interface.
• These methods define the process that will be done when the specified
event occurs.
Prepared By: Asst. Prof. Sejal Jadav
• For example: keyListener interface listens the key events and it has
three methods-
• keyPressed(),
• keyReleased() and
• keyTyped().
• These interfaces & classes are java.awt.event package.
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
Prepared By: Asst. Prof. Sejal Jadav
123

More Related Content

Similar to Programming Java GUI using SWING, Event Handling

Why You Shouldn't Write OO
Why You Shouldn't Write OO Why You Shouldn't Write OO
Why You Shouldn't Write OO Yehuda Katz
 
Organizing jQuery Projects Without OO
Organizing jQuery Projects Without OOOrganizing jQuery Projects Without OO
Organizing jQuery Projects Without OOYehuda Katz
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1Lokesh Singrol
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1PRN USM
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Javababak danyal
 
Swings in java
Swings in javaSwings in java
Swings in javaJyoti Totla
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Nuha Noor
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on SwingsMohanYedatkar
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorialsumitjoshi01
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
Advanced java programming
Advanced java programmingAdvanced java programming
Advanced java programmingKaviya452563
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTSbharathiv53
 

Similar to Programming Java GUI using SWING, Event Handling (20)

Java swing
Java swingJava swing
Java swing
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 
Why You Shouldn't Write OO
Why You Shouldn't Write OO Why You Shouldn't Write OO
Why You Shouldn't Write OO
 
Organizing jQuery Projects Without OO
Organizing jQuery Projects Without OOOrganizing jQuery Projects Without OO
Organizing jQuery Projects Without OO
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Gui
GuiGui
Gui
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
Swings in java
Swings in javaSwings in java
Swings in java
 
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
Quiz app(j tabbed pane,jdialog,container,actionevent,jradiobutton,buttongroup...
 
Swing
SwingSwing
Swing
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
03_GUI.ppt
03_GUI.ppt03_GUI.ppt
03_GUI.ppt
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
Advanced java programming
Advanced java programmingAdvanced java programming
Advanced java programming
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 

More from Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassJadavsejal
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2Jadavsejal
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1Jadavsejal
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15Jadavsejal
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14Jadavsejal
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13Jadavsejal
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12Jadavsejal
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11Jadavsejal
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10Jadavsejal
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9Jadavsejal
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8Jadavsejal
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2Jadavsejal
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3Jadavsejal
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1Jadavsejal
 
05_system architecture
05_system architecture05_system architecture
05_system architectureJadavsejal
 

More from Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
C++ unit-1-part-15
C++ unit-1-part-15C++ unit-1-part-15
C++ unit-1-part-15
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
 
05_system architecture
05_system architecture05_system architecture
05_system architecture
 

Recently uploaded

Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Recently uploaded (20)

Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

Programming Java GUI using SWING, Event Handling

  • 1. Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav 1
  • 2. Prepared By: Asst. Prof. Sejal Jadav Unit-5 CS – 19 : Programming with Java UNIT – 5 GUI using SWING, Event Handling B.C.A & B.SC.(IT) – 4
  • 3. Prepared By: Asst. Prof. Sejal Jadav Q: JPanel • The JPanel class is used to create a panel. • We can add our components to a panel and the panel should be added to a container such as a frame. • Now to draw the applet window, following method is used instead of the paint() method: public void paintComponent(Graphics g)
  • 4. Prepared By: Asst. Prof. Sejal Jadav Q: JFrame • The JFrame class creates a frame which is a window. • This window has a border, a title and buttons for closing, minimizing and maximizing the window. • The constructors for JFrame are:
  • 5. Prepared By: Asst. Prof. Sejal Jadav • JFrame() • JFrame(String title) • Here the title specifies the title of frame window. • To handle the window closing events, there is WindowListener interface which to contains the methods such as windowClosing() etc.
  • 6. Prepared By: Asst. Prof. Sejal Jadav • For example: public void windowClosing(WindowEvent e) { System.exit(0); }
  • 7. Prepared By: Asst. Prof. Sejal Jadav • To set the default closing behavior following method is used: void setDefaultCloseOperation(int op) • The op is a constant of the windowConstants interface. • These constants are: • HIDE_ON_CLOSE: It hides the window when you press the close button, but still in remains alive. This is the default operation.
  • 8. Prepared By: Asst. Prof. Sejal Jadav • DISPOSE_ON_CLOSE: This hides and disposes(remove) the window when you press the close button in the title bar. It removes the window from the screen and removes it from the memory also. • EXIT_ON_CLOSE: It exits the whole application on pressing the close button -> Sytem.exit(0) method.
  • 9. Prepared By: Asst. Prof. Sejal Jadav • DO_NOTHING_ON_CLOSE: It does nothing when the close button is pressed.
  • 10. Prepared By: Asst. Prof. Sejal Jadav Some others methods of JFrame are: • int getDefaultCloseOperation(int operation) • Here, operation is any WindowConstants described before. • Dimension getSize() • It returns a Dimension object specifying the size.
  • 11. Prepared By: Asst. Prof. Sejal Jadav • void setSize(Dimension d) • d -> the specified dimension for the width and height values • Creates an instance of Dimension whose width and height are the same as for the specified dimension.
  • 12. Prepared By: Asst. Prof. Sejal Jadav • void setSize(int w , int h ) • It set the size of frame by width w and height h. • void setLocation(int x, int y) • It position the frame at x and y coordinates.
  • 13. Prepared By: Asst. Prof. Sejal Jadav void setBounds(int x, int y, int w , int h) • It set the size and position of the frame window. • The x and y are the coordinates of the upper-left corner of the frame and w and h are the width and height of the window.
  • 14. Prepared By: Asst. Prof. Sejal Jadav void setBounds(Rectangle r) • It set the size and position of the frame to the rectangle. Rectangle getBounds() • It returns the Rectangle object specifying the size and position of the frame. void pack() • It set the size of the frame window, so that all the components have same or above their normal size.
  • 15. Prepared By: Asst. Prof. Sejal Jadav • Example: JFrameEx.java
  • 16. Prepared By: Asst. Prof. Sejal Jadav Q: JToggleButton • A JToggleButton is a two-state button. The two states are selected and unselected. • The JRadioButton and JCheckBox classes are subclasses of this class. • When the user presses the toggle button, it toggles between being pressed or unpressed.
  • 17. Prepared By: Asst. Prof. Sejal Jadav  Constructors in JToggleButton: • JToggleButton(): Creates an initially unselected toggle button without setting the text or image. • JToggleButton(String text, boolean selected): Creates a toggle button with the specified text and selection state.
  • 18. Prepared By: Asst. Prof. Sejal Jadav • JToggleButton(String text, Icon icon, boolean selected): Creates a toggle button with the specified text, image, and selection state.
  • 19. Prepared By: Asst. Prof. Sejal Jadav • setDefaultCloseOperation():-> is used to set close option for frame. • Example: • JRadioButtonDemo.java • JToggleButtonExamp.java (after Q-9: ItemEvent Class and Q-15 Event Listener Interface)
  • 20. Prepared By: Asst. Prof. Sejal Jadav Q-9 Explain ItemEvent Class. • An ItemEvent is generated when a checkbox or a list item is clicked or when a checkable menu item is selected or deselected. • There are two types of itemevents, which are identified by the following integer constants:
  • 21. Prepared By: Asst. Prof. Sejal Jadav SELECTED • It is set when an item is selected. DESELECTED • It is set when an item is deselected. ITEM_STATE_CHANGED • It is set when the state of an item is changed i.e selected or deselected.
  • 22. Prepared By: Asst. Prof. Sejal Jadav • Its method is: Object getItem( ) • It’s the item that generated this event ItemSelectable getItemSelectable( ) • It returns the ItemSelecteable object that generated this event. int getStateChange() • It returns the change of state i.e. SELECTED or DESELECTED
  • 23. Prepared By: Asst. Prof. Sejal Jadav Q-15 Explain Event Listener interfaces. • The ItemListener Interface: • This interface defines the itemStateChanged( ) method that is invoked when the state of an item changes. • Its general form is shown here: void itemStateChanged(ItemEvent ie)
  • 24. Prepared By: Asst. Prof. Sejal Jadav Q: JTabbedPane • The JTabbedPane class is used to switch between a group of components by clicking on a tab with a given title. It inherits JComponent class. Constructor: JTabbedPane() Example: TabbedPaneExample.java
  • 25. Prepared By: Asst. Prof. Sejal Jadav Q: JProgressBar • The JProgressBar class is used to display the progress of the task. • It inherits JComponent class. • Commonly used Constructors:
  • 26. Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav
  • 27. Prepared By: Asst. Prof. Sejal Jadav  Commonly used Methods:
  • 28. Prepared By: Asst. Prof. Sejal Jadav • Example:ProgressBarExample.java
  • 29. Prepared By: Asst. Prof. Sejal Jadav Q: JSlider • The Java JSlider class is used to create the slider. • By using JSlider, a user can select a value from a specific range.
  • 30. Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav
  • 31. Prepared By: Asst. Prof. Sejal Jadav Methods of JSlider class
  • 32. Prepared By: Asst. Prof. Sejal Jadav • Example: • SliderExample.java • SliderExample1.java
  • 33. Prepared By: Asst. Prof. Sejal Jadav Q- NOLayout Manager • We know earlier in "Java Layout Managers" that two styles of placing components in containers – pixel format and position format. Now let us discuss clearly what pixel format is and how to place a component in terms of pixels.
  • 34. Prepared By: Asst. Prof. Sejal Jadav • We also know earlier, the default layout manager for Frame is BorderLayout and for Panel and Applet, it is FlowLayout. • To use pixel format, first we must set the default layout manager of the container to null. • To give the size and location, setBounds() method is used. • But, this style is very rarely used (to avoid the complexity of GridBagLayout).
  • 35. Prepared By: Asst. Prof. Sejal Jadav • Following is the method signature as defined in java.awt.Component class setBounds(int x, int y, int width, int height) • First two coordinates x and y give the position in the container and the last two width and height give the size of the component.
  • 36. Prepared By: Asst. Prof. Sejal Jadav • In the following program, one label and two buttons are placed using setBounds() method. • Example:NullLayoutManager.java
  • 37. Prepared By: Asst. Prof. Sejal Jadav super(“No Layout Manager”); • This is another style of using setTitle(). • The super class constructor of Frame is called, with super(), and passed the string as parameter and this string is placed on the frame title bar.
  • 38. Prepared By: Asst. Prof. Sejal Jadav setLayout(null); • To use setBounds() method, first the default layout manager BorderLayout of frame should be set to null.
  • 39. Prepared By: Asst. Prof. Sejal Jadav sign.setBounds(80, 35, 130, 25); agreedBut.setBounds(60, 75, 75, 25); • The label sign is placed at x-coordinate of 80 and y-coodinate 35 pixels. The width of the label is given as 130 pixels and height as 25 pixels. • Simialrly, the agreeBut is placed at 60 and 75 coordinates with 75 pixels width and 25 height.
  • 40. Prepared By: Asst. Prof. Sejal Jadav • Example: NoLayoutTest.java
  • 41. Prepared By: Asst. Prof. Sejal Jadav Event Handling Unit-5(Part-2)
  • 42. Prepared By: Asst. Prof. Sejal Jadav Q.1 what is event delegation model? • The event delegation model is a mechanism that handles the generating and processing of events. • This mechanism works on a simple concept. An event source generates an event and it is sent to one or more listeners.
  • 43. Prepared By: Asst. Prof. Sejal Jadav • When the listeners listens the event, it processes the event. To listen to an event, the listener must be registered with the event source. • To understand the event delegation model we have to understand about events, source of event and event listeners.
  • 44. Prepared By: Asst. Prof. Sejal Jadav 1. Event • An event is said to be happened when a phenomenon(event) occurs like the mouse is moved, mouse button is clicked, a key is pressed, a button is pressed or an item is selected from the list etc. In java an event is an object that describes this change of state in a source that generated the event.
  • 45. Prepared By: Asst. Prof. Sejal Jadav 2. Source of event • A source is an object that can generate an event. A source can generate more than one type. An event source must register to its listener in order to listen to its events. To register an event, there are methods for each type of event listener. • The general from of event registration method is: public void addEventTypeListener(EventTypeListener obj)
  • 46. Prepared By: Asst. Prof. Sejal Jadav public void addEventTypeListener(EventTypeListener obj) • Here EventType is the name of the event and obj is the object of the class implements the EventTypeListener interface. • In most cases, the obj will be ‘this’ object. • For example to register the mouse event following method is used: sourceObj.addMouseListener(this)
  • 47. Prepared By: Asst. Prof. Sejal Jadav • Some event sources allow only one listener to register. This type of listener methods can throw a TooManyListenersException which is in java.util package. • To unregister an event following method form is used: public void removeEventTypeListener ( EventType obj) • For example to remove a keyword listener, removeKeyListener() method is used.
  • 48. Prepared By: Asst. Prof. Sejal Jadav 3. Event Listeners: • An event listener is one that listens or understands the event. • In java an event listener is an object. In order to receive the event notification, the event object has to be registered with the event and its class must implement all the methods of the event listener interface. • These methods define the process that will be done when the specified event occurs.
  • 49. Prepared By: Asst. Prof. Sejal Jadav • For example: keyListener interface listens the key events and it has three methods- • keyPressed(), • keyReleased() and • keyTyped(). • These interfaces & classes are java.awt.event package.
  • 50. Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav Prepared By: Asst. Prof. Sejal Jadav 123