Andreas Schleicher presents at the launch of What does child empowerment mean...
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.
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
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
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.
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