Java Event Handling: Creating Interactive GUIs through Event-Driven Programming, Lecture notes of Java Programming

Learn the fundamentals of Java event handling, including the concept of events, event sources, and event listeners. Discover how to create and attach listener objects to GUI components to make interactive applications. creating event listener classes, implementing event-listener interfaces, and handling events for various components such as text fields, buttons, checkboxes, and radio buttons.

Typology: Lecture notes

2019/2020

Uploaded on 01/12/2020

Joshlul
Joshlul 🇪🇹

5 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Event Handling
(Java)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Event Handling: Creating Interactive GUIs through Event-Driven Programming and more Lecture notes Java Programming in PDF only on Docsity!

Event Handling

(Java)

Introduction

  • (^) In order to create useful interactive GUIs, you must learn how to handle Java events.
  • (^) When the user clicks on a component, moves the mouse over it, or otherwise interacts with it, Java’s GUI system creates a special kind of object called an event to represent this action.
  • GUIs are event driven.
    • (^) When the user interacts with a GUI component, the interaction—known as an event —drives the program to perform a task.
  • (^) Event is
    • (^) An object that represents a user’s interaction with a GUI component and that can be handled by your programs to create interactive components.
  • (^) The code that performs a task in response to an event is called an event handler and the overall process of responding to events is known as event handling.

Steps Required to Set Up Event Handling for a GUI Component

  • (^) Shortly put the steps are: To handle an event, create a listener object and attach it to the component of interest. The listener object contains the code that you want to run when the appropriate event occurs.
  • Before an application can respond to an event for a particular GUI component, we must perform several coding steps: 1. Create a class that represents the event handler. This class is the “event listener class”. - (^) In Java, event listeners are written by implementing particular interfaces. So go to Step-2. 2. Implement an appropriate interface, known as an event-listener interface , for the class created in Step 1. NB: When implementing interface we must override all the methods inside the interface. So, our class in Step-1 must contain methods of the interface it overrides. It is inside this methods that we write the code that we want to run when the event occurs 3. Register Listener for the Component. - (^) This is also refered to as “attaching the listener” to the component
  • Indicate that an object of the class from Steps 1 and 2 should be notified when the event occurs. This is known as registering the event handler.
  • (^) Create and attach object of your listener class to the component

To note

  • (^) Event: clicking a button, altering the text, checking an option, closing a frame, etc.
  • (^) Source: the component that generates an event.
    • (^) the particular GUI component with which the user interacts.
  • (^) Listener: the responsible for receiving and handling (consuming) events.

Example1 - JTextField

  • (^) Three Text Fields
    • (^) setEditable(false)
    • (^) setEnabled(true/flase)
    • (^) setFocusAccelerator(‘N’)
    • (^) setToolTipText(“Name here”)
    • (^) isEditable()
    • (^) isEnabled()
  • (^) Nested class for handling event
  • (^) actionPerformed: with if elses based on “getSource()” and displaying “getActionCommand()”

Example 2 - Button

  • (^) Two buttons (plain & fancy)
  • (^) Plain: nothing special
  • (^) Fancy:
    • (^) Two “Icon”: “ImageIcon(getClass().getResource(“xx.jpg”)) & another
    • (^) New Jbutton(“Fancy”, ic1);
    • (^) Fancy.setRolloverIcon(ic2);
  • (^) Nested event handler
    • (^) Just display the texts using e.getActionCommand()

Example 4 – Radion Button

JTextField txtF; JRadioButton italicB; JRadioButton boldB; JRadioButton italicBoldB; JRadioButton plainB;

  • May be home work

Example 5 - Combo

JComboBox jcb; JLabel label; String names[]={"cross.gif","ball.gif","hand.gif","help.gif"}; Icon icons[]={new ImageIcon(getClass().getResource(names[0])), new ImageIcon(getClass().getResource(names[1])), new ImageIcon(getClass().getResource(names[2])), new ImageIcon(getClass().getResource(names[3]))};