1 / 9

GUI Basics and Event-Driven Programming

GUI Basics and Event-Driven Programming. Feb 7, 2000. What are provided by GUI library?. Creation of user interface components (often called controls ) Giving a specific behaviour to the controls by coupling with GUI events. Grouping and arranging(layout) the controls on the screen.

vega
Download Presentation

GUI Basics and Event-Driven Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GUI Basics and Event-Driven Programming Feb 7, 2000

  2. What are provided by GUI library? • Creation of user interface components (often called controls) • Giving a specific behaviour to the controls by coupling with GUI events. • Grouping and arranging(layout) the controls on the screen. • Graphics operation Ex: Polygon drawing/filling, Clipping, etc.

  3. Procedural versus Graphical programming • Graphical Programs are WAY different from procedural programs. • A procedural program is a sequential flow of control. We DO know the start and end. • Windowing(graphical) programs are unpredictable(asynchronous). Who knows when a button will be clicked? • Therefore we use event-driven programming.

  4. Event-Driven Programming • In event-driven programming, we need a outermost loop which is constantly waiting for user input. (Indefinite loop) • When an user input is occurred(eg. Mouse click), the window manager creates an event and passes it onto an event handler that is provided by programmer. This is known as callback. • In other words, it’s your job to provide such a event handler

  5. Java Event Model • Java event model is primarily for GUI programming. • Generally it is used to connect your code to any kind of asynchronous events. • To receive a specific event, a Java class should tell the window system(register) its interest in the event. • In other words, we connect the controls(event source) by registering a callback with your event-handling class.

  6. Delegation Model Listener object Event source object Listener object Event source object Listener object Event source object Fire events Register handler

  7. import javax.swing.*; public class FrameDemo { public static void main(String args[]) { JFrame frame = new JFrame(“Hoony Wrote this”); frame.setSize(400,100); frame.setVisible(true); } } minimize maximize close • JFrame is capable of receiving events from Windows. In other words it is a Window. All three buttons are working as usual. • However even after clicking close button, FrameDemo.java is not killed. (Becomes ZOMBIE!!!) • Need to register handler!!!

  8. import javax.swing.*; public class FrameDemo { public static void main(String args[]) { JFrame frame = new JFrame(“Hoony Wrote this”); frame.setSize(400,100); frame.setVisible(true); MyHandler hand = new MyHandler(); frame.addWindowListener(hand); } } class MyHandler implements WindowListener { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(Window Event e){} public void windowDeIconified(Window Event e){} public void windowActivated(Window Event e) {} public void windowDeactivated(Window Event e) {} }

  9. Basic framework for event handling in Java • Write a class that implements the interface associated with a certain event. Generally this takes the form of SomethingListener eg) WindowListener, MouseListener • Creata an object of the type(class) above. • Call registering method provided by underlying component. Generally this takes the form of addSomethingListern(listener) Note: listener is SomethingListener type. • Shortcut: Inner class, Listener Adapter class

More Related