BM214E Object Oriented Programming Lecture 13

Size: px
Start display at page:

Download "BM214E Object Oriented Programming Lecture 13"

Transcription

1 BM214E Object Oriented Programming Lecture 13

2 Events To understand how events work in Java, we have to look closely at how we use GUIs. When you interact with a GUI, there are many events taking place each second. Only a few of these, however, may actually be delivered to the application.

3 Events Java uses a delegation event model found in many other toolkits. Under the delegation model, components fire events, which can be caught and acted on by listeners. A listener is linked to a component through a registration process. The delegation event model is contrasted to an event filtration model where all events are delivered to target components regardless of whether they asked for them.

4 General Overview Recall our first consideration of events, where our first frame would not close, even when the end of main() was reached. We explained this behavior by thinking of our program as entering an infinite loop when the graphics are shown. This infinite loop is actually an event-driven cycle, but we can think of it as a while (true) structure that periodically polls for user input.

5 The Real Story import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI We usually think of our program as a single, linear set of steps being executed. But something special happens when we create graphical objects.

6 The Real Story When Java sees that you ve created a GUI, your program gets a second set of linear instructions. import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI This is actually a separate thread, but don t worry if that s unclear for now. We can think of this as a second part of our program than handles special graphicsrelated tasks (such as drawing the window, etc.)

7 Graphics Thread import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI Both of these threads are your program. You coded one of the lines of control. Java provides the other one. That way, things appear to happen simultaneously--your code executes and the window gets redrawn, refreshed, etc. Java quickly switches between your code and the graphics drawing code, so that both threads appear to execute at the same time.

8 Don t Panic Don t worry if this thread stuff is confusing. Other classes go into this in detail. For our purposes, we only have to understand that there are two areas of a graphic program. 1 The code we wrote in main() and other methods import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI 2 The code Java provides to handle the graphics side of things. Your Code Graphics

9 Who Cares? This model is very important to understand because as it turns out, when an event occurs--such as mouse click, it happens in the graphics side of the model. import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI Mouse Click occurs The code trapping this event appears in the graphics thread Actually, there s a separate event queue that handles incoming events. But this is already complicated enough. Let s just generalize and imagine that all events arrive in the graphics side of things.

10 Call backs Since the event arrived in the graphics half of our program, we need a way to have it call a method in our program. This is known as a call back. Our event handling code import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI The code trapping this event appears in the graphics thread callback

11 How? So Java needs to call some event handling code that we write. The trouble is, how will Java know what we called out method? We can name them anything we want, and Java won t necessarily know what methods handle events. import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI But Wait! callback We can use interfaces, right?

12 Event Interfaces Java uses interfaces as its primary event handling scheme. If you implement an event-related interface, Java will know which methods to call. This is because the contract nature of interfaces requires all methods to appear in the implementing class. ActionListener import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI public void actionperformed (ActionEvent e) { // code doing something } This method MUST be there, so Java knows it can callback to it callback

13 Why Delegation? Since any class can implement any interface, we can have just about any object handle the events. We can therefore delegate event handling to this object. V C M Remember MVC? Some (smart) folks believe you should organize where the events get handled. (This is the controller aspect to MVC.)

14 Why Registration? We are told that event registration must occur before event handling will occur. What does this mean? Well, since we can have any class handle events, we need to tell Java which object implements the proper event handling interface. This registers the component as being interested in receiving callbacks. import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println ( About to make GUI ); Frame f = new Frame ( Hello GUIs ); f.setsize( 200, 200 ); f.show(); System.out.println ( Finished making GUI ); }// main }// class HelloGUI Where to callback?

15 Another example public class DemoFrame extends Frame { public DemoFrame( ) { super ( A poor use of inheritance, but simple ); Handler2 h = new Handler2(); this.setsize(400,400); this.setlayout(new FlowLayout()); Button b = new Button ( Click me ); this.add(b); this.show(); } // Constructor public static void main(string args[]) { DemoFrame df; df = new DemoFrame(); } // main } // DemoFrame

16 Another example public class Handler2 implements ActionListener { public void actionperformed(actionevent e) { } System.out.println ( Button was clicked ); } // Handler2 Why doesn t this work?

17 Another example public class DemoFrame extends Frame { public DemoFrame( ) { super ( A poor use of inheritance, but simple ); Handler2 h = new Handler2(); this.setsize(400,400); this.setlayout(new FlowLayout()); Button b = new Button ( Click me ); b.addactionlistener(h); this.add(b); this.show(); } // Constructor public static void main(string args[]) { DemoFrame df; df = new DemoFrame(); } // main } // DemoFrame

18 Question... We said we had to have a Listener to handle the event and it had to be an object. Does it have to be a separate object?

19 Another example public class DemoFrame extends Frame implements ActionListener { public DemoFrame( ) { super ( A poor use of inheritance, but simple ); Handler2 h = new Handler2(); this.setsize(400,400); this.setlayout(new FlowLayout()); Button b = new Button ( Click me ); b.addactionlistener(this); this.add(b); this.show(); } // Constructor public void actionperformed(actionevent e) { System.out.println ( Button was clicked ); } public static void main(string args[]) { DemoFrame df; df = new DemoFrame(); } // main } // DemoFrame

20 Questions?

21 Event types Anything can be an event. Including general protection faults. But for the most part, good programming dictates that handled events should come from the following area of input: a Mouse events b Keyboard events c d Timing events Other user action inputs

22 Java Event Handling Strategies With this basic understanding, we can investigate the FOUR primary means of event handling in Java Very similar Very general Very old Event Listeners Event Adapters Semantic Events Inheritance-based event handling

23 Listeners From the discussion about callbacks, we noted that interfaces were the primary mechanism for structuring our event handling. There are numerous event interfaces we can implement, roughly divided around categories of events. The next slide lists many of them. Don t freak out because there are so many. We ll highlight the most commonly used ones...

24 Yikes. So Many Choices Package java.awt.event features: ActionListener MouseListener MouseMotionListener AdjustmentListener ComponentListener FocusListener ContainerListener ItemListener KeyListener WindowListener TextListener Most commonly used As it turns out, the ActionListener is part of the semantic event group, even though it s an interface. So let s focus on simple events like MouseListener...

25 MouseListener The MouseListener interface has several methods we have to code: public void mouseclicked(mouseevent e) { } -- a timing-based determination; else the events are processed as pressed/releases public void mouseentered(mouseevent e) { } -- entry into component public void mouseexited(mouseevent e) { } -- exit from component public void mousepressed(mouseevent e) { } -- simply a press... public void mousereleased(mouseevent e){ } the corresponding release

26 import java.awt.*; import java.awt.event.*; public class MouseFrame implements MouseListener{ Color highlight, normal; boolean bhighlight = true; Frame fr; public MouseFrame () { fr = new Frame( For demonstration only ); highlight = Color.red; } normal = Color.gray; frame.setsize(400,400); Button b = new Button("Click"); b.addmouselistener(this); fr.setbackground(normal); fr.setlayout(new FlowLayout()); fr.add(b); fr.show(); public static void main(string[] args) { new MouseFrame(); } (more) To keep it simple, we ignore WindowEvents Note that when we run this the constructor will run and terminate

27 } public void mousereleased(mouseevent e){ System.out.println ("Changing color"); if (bhighlight) frame.setbackground(highlight); else frame.setbackground(normal); bhighlight =!bhighlight; public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousepressed(mouseevent e) {} } // MouseFrame

28 Event Listener Summary We need a class that implements the appropriate listener type. We need to register a component as interested in receiving events: addxyzlistener ( <listener instance> ); Whatever listener we re working with. E.g.: addmouselistener(this); addmousemotionlistener(myeventhandler);

29 Observations The WindowListener interface required numerous methods. But only one was important to us. All the rest were coded as no-op or no operation methods. There s another strategy using adapters, using inheritance that could have saved us some trouble...

30 Adapters Java has built-in classes called event adapters that implement each of the various event listeners. But all of these methods are no-ops. public class MouseAdapter implements MouseListener { public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} } WHY?

31 Key to Adapters: Inheritance MouseAdapter MouseFrame Why a bunch of no-op methods? Well, if you subclass the adapter, your class IS-A type of event listener. And you then only have to override the one or two methods you care about. The rest can be inherited as no-ops

32 import java.awt.*; import java.awt.event.*; public class MouseFrame extends MouseAdapter implements MouseListener{ Color highlight, normal; boolean bhighlight = true; Frame frame; public MouseFrame () { frame = new Frame( For demonstration only ); highlight = Color.red; normal = Color.gray; frame.setsize(400,400); Button b = new Button("Click"); b.addmouselistener(this); frame.setbackground(normal); frame.setlayout(new FlowLayout()); frame.add(b); frame.show(); } public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousepressed(mouseevent e) {} Parent class takes care of these

33 Example (cont d) public void mousereleased(mouseevent e){ System.out.println ("Changing color"); if (bhighlight) frame.setbackground(highlight); else frame.setbackground(normal); bhighlight =!bhighlight; } We override the one or two methods we care about public static void main(string[] args) { new MouseFrame(); } } // MouseFrame Same behavior; less code; but we use up our single inheritance

34 public class MouseAdapter implements MouseListener { public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} } import java.awt.*; import java.awt.event.*; public class MouseFrame extends MouseAdapter { Color highlight, normal; boolean bhighlight = true; Frame frame; public MouseFrame () { frame = new Frame( For demonstration only ); highlight = Color.red; normal = Color.gray; frame.setsize(400,400); Button b = new Button("Click"); b.addmouselistener(this); frame.setbackground(normal); frame.setlayout(new FlowLayout()); frame.add(b); frame.show(); } public void mousereleased(mouseevent e) { System.out.println ("Changing color"); if (bhighlight) frame.setbackground(highlight); else frame.setbackground(normal); bhighlight =!bhighlight; } public static void main(string[] args) { new MouseFrame(); } } // MouseFrame This comes with Java!

35 Big Picture Time So far, we ve tinkered with different ways of coding very low-level event handling. But what if our event handling needs are very general. Consider this simple dialog box: Are you sure you wish to proceed? cancel ok There s not much interaction that needs to be supported. Mouse entry/exit might not be needed at all.

36 Semantic Events Wouldn t it be convenient to abstract all of these small events into one just-tell-me-when-its-clicked event? public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) {} public void mouseexited(mouseevent e) {} public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} M1A1 Abstractor public void actionperformed(actionevent e)

37 Semantic Events Semantic events provide a means of handling events at the component level. That is, you will not address fine-grained events like mouse entry and exit. Instead, you ll only receive a callback when the component has received some type of input event

38 Semantic Events Semantic Event Components and Firing Event ActionEvent AdjustmentEvent Button (activated) List (double-clicked) MenuItem (selected) TextField (typed) Scrollbar (moved) There are numerous event handlers for low-level events associated with these widgets. ItemEvent TextEvent Checkbox (toggled) CheckboxMenuItem (selected) Choice (selected) List (selected) TextComponent (text changes) Note: ALL input is sent into one of these FOUR categories.

39 Example import java.awt.*; import java.awt.event.*; public class MouseFrame extends Frame implements ActionListener { Color highlight, normal; boolean bhighlight = true; public MouseFrame () { highlight = Color.red; normal = Color.gray; this.setsize(400,400); Button b = new Button("Click"); b.addactionlistener(this); this.setbackground(normal); this.setlayout(new FlowLayout()); this.add(b); this.show(); }

40 Example (cont d) public void actionperformed(actionevent e){ System.out.println ("Changing color"); if (bhighlight) this.setbackground(highlight); else this.setbackground(normal); bhighlight =!bhighlight; } public static void main(string[] args) { new MouseFrame(); } } // MouseFrame We therefore lose the ability to handle very finegrained events (e.g., mouse entry/exit). But that might be acceptable for certain applications.

41 Event Handling Options: How to Decide Event Listeners (interfaces) Event Adapters (inheritance) Semantic Events Costs Must code all methods; wasteful no-ops result Uses up single inheritance opportunity Simplifies event handling Benefits Keep all events in single class Good abstraction; override those methods you need Loss of granular control; linear code

42 Debugging re: Event Handlers Debugging an event-driven program (whether applet or graphical application) is more tricky than debugging a non-event-driven program. With an event-driven Java program, you don't explicitly code any kind of event-handling loop that "polls" for occurring events, then calls the appropriate handler(s) for those events. Instead, the Java internals handle this polling action for you. Debugging becomes trickier because now you have to make sure that your event handling code works correctly. You also have to make sure you're handling the correct events in the first place! For example, your code for mouseentered( ) may work perfectly, but if you're expecting it to get called when the user clicks a mouse button, it won't be!

43 Debugging re: Event Handlers So, in debugging event-driven programs written with Java, the steps are: Be sure you're handling the appropriate events: Map out on paper what events get thrown from what components, and what class(es) handle them. Handle the events appropriately: This is the kind of debugging you're already familiar with: Once you're sure the appropriate events are getting handled, the rest is being sure the event-handling code (and the code that the event handlers call) work.

44 Events: A Short Example To compare the three event handling techniques, let s see a *brief* example how all three might work on a common problem. TEXT AREA BUTTON Goal: Create a simple Frame that holds a TextArea and Button. The Button toggles the ability to edit the TextArea Panel subclass My Program The Panel holding the Button and TextArea is placed in a Frame subclass, which handles its own disposal

45 import java.awt.*; import java.awt.event.*; public class MyFrame extends Frame implements WindowListener{ public static final int iwidth = 300, iheight = 500; public MyFrame() { this.setsize(iwidth, iheight); this.addwindowlistener(this); BorderLayout border = new BorderLayout(); this.setlayout(border); } public void windowclosing (WindowEvent e) { e.getwindow().setvisible(false); e.getwindow().dispose(); System.exit(0); } public void windowactivated(windowevent e) {} public void windowclosed(windowevent e) {} public void windowdeactivated(windowevent e) {} public void windowdeiconified(windowevent e) {} public void windowiconified(windowevent e) {} public void windowopened(windowevent e) {} }// class MyFrame Constructor WindowListener Frames are not self-disposing! (Setting Frame invisible first eliminate flicker.)

46 import java.awt.*; import java.awt.event.*; public class MyFrame extends Frame { public static final int iwidth = 300, iheight = 500; public MyFrame() { this.setsize(iwidth, iheight); this.addwindowlistener (new WindowAdapter() { Frames are not self-disposing! (Setting Frame invisible first eliminate flicker.) public void windowclosing (WindowEvent e) { e.getwindow().setvisible(false); e.getwindow().dispose(); System.exit(0); } }); BorderLayout border = new BorderLayout(); this.setlayout(border); } }// class MyFrame Anonymous Inner Class : used as a short cut. For your code, use listeners

47 import java.awt.*; public class Driver { public static void main (String arg[]){ Notepad note = new Notepad(); MyFrame f = new MyFrame(); f.add(note, BorderLayout.CENTER); f.show(); }//main }//class Driver A simple driver. Notice that so far, we ve abstracted the Frame subclass into something very generic and reusable--it S NOT JUST TIED TO THIS PROGRAM!

48 Variation #1: Listener Events (MouseListener) import java.awt.*; import java.awt.event.*; class Notepad extends Panel implements MouseListener { Button toggle; TextArea scratch; boolean bwritable; public Notepad() { super("wasted inheritance"); this.setlayout (new BorderLayout()); scratch = new TextArea(20,20); Panel buttonpanel = new Panel(); toggle = new Button ("Freeze/Unfreeze"); buttonpanel.add(toggle); add(scratch, BorderLayout.CENTER); add(buttonpanel, BorderLayout.SOUTH); toggle.addmouselistener(this); bwritable = false; }// constructor The Driver and MyFrame classes were generic enough to work with any version of this example. Here, however, we need to create a specific event handler.

49 /*... Continued from class Notepad extends Panel implements MouseListener... */ public void setwritable(boolean bwritable){ this.bwritable = bwritable; }//setwritable public boolean getwritable() { return bwritable; }//getwritable Implement the method one needs; the rest are no-ops public TextArea gettextarea(){ return scratch; }//gettextarea public void mousepressed(mouseevent e) { gettextarea().setenabled(getwritable()); setwritable(!getwritable()); }//mousepressed public void mousereleased(mouseevent e) {;} public void mouseclicked(mouseevent e) {;} public void mouseentered(mouseevent e) {;} public void mouseexited(mouseevent e) {;} }//class Notepad

50 Driver main Notepad note MyFrame f My Program Notepad extends Panel Button toggle TextArea scratch boolean bwritable TEXT AREA BUTTON

51 Variation #2: Adapter Events (MouseAdapter) import java.awt.*; import java.awt.event.*; class Notepad extends Panel { /* NOTE: NO INTERFACE! */ Button toggle; TextArea scratch; boolean bwritable; public void setwritable(boolean bwritable){ this.bwritable = bwritable; }//setwritable public boolean getwritable(){ return bwritable; }//getwritable public TextArea gettextarea(){ return scratch; }//gettextarea

52 /*... Continued from class Notepad extends Panel */ public Notepad(){ super(); this.setlayout (new BorderLayout()); scratch = new TextArea(20,20); Panel buttonpanel = new Panel(); toggle = new Button ("Freeze/Unfreeze"); buttonpanel.add(toggle); add(scratch, BorderLayout.CENTER); add(buttonpanel, BorderLayout.SOUTH); Note use of anonymous inner class. toggle.addmouselistener(new MouseAdapter() { public void mousepressed(mouseevent e) { gettextarea().setenabled(getwritable()); setwritable(!getwritable()); } });/* end of anonymous inner class */ bwritable = false; }// constructor

53 Variation #3: Another Way! import java.awt.*; import java.awt.event.*; public class AnotherHandler extends MouseAdapter { Notepad np; public AnotherHandler(Notepad np) { this.np = np; } // Constructor public void mousepressed(mouseevent e) { np.gettextarea().setenabled(np.getwritable()); np.setwritable(!np.getwritable()); } } // AnotherHandler

54 Variation #3: Another Way import java.awt.*; import java.awt.event.*; class Notepad extends Panel { /* NOTE: NO INTERFACE! */ Button toggle; TextArea scratch; boolean bwritable; public void setwritable(boolean bwritable){ this.bwritable = bwritable; }//setwritable public boolean getwritable(){ return bwritable; }//getwritable public TextArea gettextarea(){ return scratch; }//gettextarea

55 /*... Continued from "class Notepad extends Panel" */ public Notepad(){ super(); this.setlayout (new BorderLayout()); scratch = new TextArea(20,20); Panel buttonpanel = new Panel(); toggle = new Button ("Freeze/Unfreeze"); buttonpanel.add(toggle); add(scratch, BorderLayout.CENTER); add(buttonpanel, BorderLayout.SOUTH); AnotherHandler ah = new AnotherHandler(this); toggle.addmouselistener(ah); bwritable = false; }// constructor

56 Variation #4: Semantic Events (ActionListener) import java.awt.*; import java.awt.event.*; class Notepad extends Panel implements ActionListener { Button toggle; TextArea scratch; boolean bwritable; public Notepad(){ super(); this.setlayout (new BorderLayout()); scratch = new TextArea(20,20); Panel buttonpanel = new Panel(); toggle = new Button ("Freeze/Unfreeze"); buttonpanel.add(toggle); add(scratch, BorderLayout.CENTER); add(buttonpanel, BorderLayout.SOUTH); toggle.addactionlistener(this); bwritable = false; }// constructor

57 /*... Continued from "class Notepad extends Panel implements ActionListener"... */ public void setwritable(boolean bwritable){ this.bwritable = bwritable; }//setwritable public boolean getwritable(){ return bwritable; }//getwritable public TextArea gettextarea() { return scratch; }//gettextarea public void actionperformed (ActionEvent e) { gettextarea().setenabled(getwritable()); setwritable(!getwritable()); }//actionperformed }//class Notepad

58 Event Handlers: Final Review This simple example shows that sometimes, semantic event handlers perform the task perfectly. In the drawing applet example, the semantic event handler was unable to offer help. In this example, it works perfectly, and was easier to follow and implement. Note that a few of the examples in this notepad demonstration used anonymous inner classes. You will not be required to use anonymous inner classes in this course; however, they are used a lot and if you have never seen them before they can be startling...be prepared.

59 Cross Class Communication Working with events often requires us to have upstream references, so that event handlers can call methods in the classes that created them.

60 Another (Familiar?) Example To illustrate cross-class communication,let s make a simple program that s similar the examples used in previous slides. We ll use a Frame and a class to handle events. When the mouse enters a button, the button will flash, and will update the Frame s title.

61 The Madness HAS-A Method FlashDemo (a JFrame) EventHandler (a MouseListener) Makes an array of JButtons sets their event handler, passing in this and the button. Saves the frame and button reference so it can call back to the frame to set its title, and change the button The two classes have to talk to each other, and therefore need references to each other.

62 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FlashDemo extends JFrame implements WindowListener { JButton[][] buttons; public FlashDemo () { this.addwindowlistener(this); buttons = new JButton[4][4]; JPanel panel = new JPanel(); panel.setlayout(new GridLayout(4,4,10, 10)); Key Method Call } for (int i=0; i < buttons.length; i++) for (int j=0; j < buttons[i].length; j++){ buttons[i][j] = new JButton ("[" + i +"][" + j +"]"); EventHandler eh = new EventHandler (this, buttons[i][j]); buttons[i][j].addmouselistener(eh); panel.add(buttons[i][j]); } this.getcontentpane().add(panel); this.setsize(350,350);

63 /* class FlashDemo (cont d) */ public void windowclosing(windowevent e) { System.exit(0); } public void windowactivated(windowevent e) {} public void windowclosed(windowevent e) {} public void windowdeactivated(windowevent e) {} public void windowdeiconified(windowevent e) {} public void windowiconified(windowevent e) {} public void windowopened(windowevent e) {} public static void main (String[] arg){ new FlashDemo().show(); } } // FlashDemo

64 import java.awt.event.*; import java.awt.*; import javax.swing.*; public class EventHandler implements MouseListener { FlashDemo theframe; JButton button; Color highlight; Color original; public EventHandler (FlashDemo theframe, JButton button) { this.theframe = theframe; this.button = button; highlight = Color.yellow; original = button.getbackground(); } We SAVE reference to the frame that made this class, and the button it has to control

65 /* class EventHandler (cont d) */ public void mouseclicked(mouseevent e) {} public void mouseentered(mouseevent e) { theframe.settitle ("You are now in Button #" + button.getlabel()); button.setbackground(highlight); } public void mouseexited(mouseevent e) { theframe.settitle(""); button.setbackground(original); } public void mousepressed(mouseevent e) {} public void mousereleased(mouseevent e) {} } // EventHandler

66 Questions?

The AWT Event Model 9

The AWT Event Model 9 The AWT Event Model 9 Course Map This module covers the event-based GUI user input mechanism. Getting Started The Java Programming Language Basics Identifiers, Keywords, and Types Expressions and Flow

More information

Chapter 1 GUI Applications

Chapter 1 GUI Applications Chapter 1 GUI Applications 1. GUI Applications So far we've seen GUI programs only in the context of Applets. But we can have GUI applications too. A GUI application will not have any of the security limitations

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing. COSC User Interfaces Module 3 Sequential vs. Event-driven Programming Example Programs DemoLargestConsole.java DemoLargestGUI.java Demo programs will be available on the course web page. GUI Program Organization

More information

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline Sequential vs. Event-driven Programming Reacting to the user GUI Program Organization Let s digress briefly to examine the organization of our GUI programs We ll do this in stages, by examining three example

More information

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing CSEN401 Computer Programming Lab Topics: Graphical User Interface Window Interfaces using Swing Prof. Dr. Slim Abdennadher 22.3.2015 c S. Abdennadher 1 Swing c S. Abdennadher 2 AWT versus Swing Two basic

More information

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events Objectives Event Handling Animation Discussion of Roulette Assignment How easy/difficult to refactor for extensibility? Was it easier to add to your refactored code? Ø What would your refactored classes

More information

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing Rui Moreira Some useful links: http://java.sun.com/docs/books/tutorial/uiswing/toc.html http://www.unix.org.ua/orelly/java-ent/jfc/

More information

GUI in Java TalentHome Solutions

GUI in Java TalentHome Solutions GUI in Java TalentHome Solutions AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.* Calculator

More information

Graphical Interfaces

Graphical Interfaces Weeks 11&12 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The

More information

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals

More information

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120e) Programming Languages and Techniques (CIS120e) Lecture 29 Nov. 19, 2010 Swing I Event- driven programming Passive: ApplicaHon waits for an event to happen in the environment When an event occurs, the applicahon

More information

Graphical Interfaces

Graphical Interfaces Weeks 9&11 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The

More information

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI Statics and GUI Dynamics 2 Statics: what s drawn on the screen Components buttons, labels, lists, sliders, menus,... Containers: components that contain

More information

Advanced Java Programming (17625) Event Handling. 20 Marks

Advanced Java Programming (17625) Event Handling. 20 Marks Advanced Java Programming (17625) Event Handling 20 Marks Specific Objectives To write event driven programs using the delegation event model. To write programs using adapter classes & the inner classes.

More information

GUI Programming: Swing and Event Handling

GUI Programming: Swing and Event Handling GUI Programming: Swing and Event Handling Sara Sprenkle 1 Announcements No class next Tuesday My Fourth of July present to you: No quiz! Assignment 3 due today Review Collections: List, Set, Map Inner

More information

Java. GUI building with the AWT

Java. GUI building with the AWT Java GUI building with the AWT AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses the controls defined by your OS therefore

More information

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Course: CMPT 101/104 E.100 Thursday, November 23, 2000 Course: CMPT 101/104 E.100 Thursday, November 23, 2000 Lecture Overview: Week 12 Announcements Assignment 6 Expectations Understand Events and the Java Event Model Event Handlers Get mouse and text input

More information

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything. Java - Applications C&G Criteria: 5.3.2, 5.5.2, 5.5.3 Java applets require a web browser to run independently of the Java IDE. The Dos based console applications will run outside the IDE but have no access

More information

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616

OBJECT ORIENTED PROGRAMMING. Java GUI part 1 Loredana STANCIU Room B616 OBJECT ORIENTED PROGRAMMING Java GUI part 1 Loredana STANCIU loredana.stanciu@upt.ro Room B616 What is a user interface That part of a program that interacts with the user of the program: simple command-line

More information

Packages: Putting Classes Together

Packages: Putting Classes Together Packages: Putting Classes Together 1 Introduction 2 The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance) Extending interfaces The features in basic

More information

Unit 7: Event driven programming

Unit 7: Event driven programming Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Unit 7: Event driven programming 1 1. Introduction 2.

More information

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA Assignment No. 4 SD Module-1 Advanced JAVA R C (4) V T Total (10) Dated Sign Title: Transform the above system from command line system to GUI based application Problem Definition: Write a Java program

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

SD Module-1 Advanced JAVA. Assignment No. 4

SD Module-1 Advanced JAVA. Assignment No. 4 SD Module-1 Advanced JAVA Assignment No. 4 Title :- Transform the above system from command line system to GUI based application Problem Definition: Write a Java program with the help of GUI based Application

More information

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects Chapter 12 Window Interfaces Using Swing Objects Event-Driven Programming and GUIs Swing Basics and a Simple Demo Program Layout Managers Buttons and Action Listeners Container Classes Text I/O for GUIs

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Overview

More information

Programming Mobile Devices J2SE GUI

Programming Mobile Devices J2SE GUI Programming Mobile Devices J2SE GUI University of Innsbruck WS 2009/2010 thomas.strang@sti2.at Graphical User Interface (GUI) Why is there more than one Java GUI toolkit? AWT write once, test everywhere

More information

Handling Mouse and Keyboard Events

Handling Mouse and Keyboard Events Handling Mouse and Keyboard Events 605.481 1 Java Event Delegation Model EventListener handleevent(eventobject handleevent(eventobject e); e); EventListenerObject source.addlistener(this);

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 2011 Extending JFrame Dialog boxes Overview Ge

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Ge?ng user input Overview Displaying message or error Listening for

More information

Systems Programming Graphical User Interfaces

Systems Programming Graphical User Interfaces Systems Programming Graphical User Interfaces Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: José Jesús García Rueda Systems Programming GUIs based on Java

More information

G51PRG: Introduction to Programming Second semester Applets and graphics

G51PRG: Introduction to Programming Second semester Applets and graphics G51PRG: Introduction to Programming Second semester Applets and graphics Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous two lectures AWT and Swing Creating components and putting

More information

Swing from A to Z Some Simple Components. Preface

Swing from A to Z Some Simple Components. Preface By Richard G. Baldwin baldwin.richard@iname.com Java Programming, Lecture Notes # 1005 July 31, 2000 Swing from A to Z Some Simple Components Preface Introduction Sample Program Interesting Code Fragments

More information

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1) Objectives Write code to handle events that occur in a GUI 11 GUI Event Handling Describe the concept of adapter classes, including how and when to use them Determine the user action that originated the

More information

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn Java & Graphical User Interface II Wang Yang wyang AT njnet.edu.cn Outline Review of GUI (first part) What is Event Basic Elements of Event Programming Secret Weapon - Inner Class Full version of Event

More information

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphic User Interfaces. - GUI concepts - Swing - AWT Graphic User Interfaces - GUI concepts - Swing - AWT 1 What is GUI Graphic User Interfaces are used in programs to communicate more efficiently with computer users MacOS MS Windows X Windows etc 2 Considerations

More information

Introduction to the JAVA UI classes Advanced HCI IAT351

Introduction to the JAVA UI classes Advanced HCI IAT351 Introduction to the JAVA UI classes Advanced HCI IAT351 Week 3 Lecture 1 17.09.2012 Lyn Bartram lyn@sfu.ca About JFC and Swing JFC Java TM Foundation Classes Encompass a group of features for constructing

More information

GUI Event Handlers (Part I)

GUI Event Handlers (Part I) GUI Event Handlers (Part I) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University 1 Agenda General event

More information

COMPSCI 230. Software Design and Construction. Swing

COMPSCI 230. Software Design and Construction. Swing COMPSCI 230 Software Design and Construction Swing 1 2013-04-17 Recap: SWING DESIGN PRINCIPLES 1. GUI is built as containment hierarchy of widgets (i.e. the parent-child nesting relation between them)

More information

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60

กล ม API ท ใช. Programming Graphical User Interface (GUI) Containers and Components 22/05/60 กล ม API ท ใช Programming Graphical User Interface (GUI) AWT (Abstract Windowing Toolkit) และ Swing. AWT ม ต งต งแต JDK 1.0. ส วนมากจะเล กใช และแทนท โดยr Swing components. Swing API ปร บปร งความสามารถเพ

More information

Programming Language Concepts: Lecture 8

Programming Language Concepts: Lecture 8 Programming Language Concepts: Lecture 8 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 8, 11 February 2009 GUIs and event

More information

CHAPTER 2. Java Overview

CHAPTER 2. Java Overview Networks and Internet Programming (0907522) CHAPTER 2 Java Overview Instructor: Dr. Khalid A. Darabkh Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package

More information

CS 2113 Software Engineering

CS 2113 Software Engineering CS 2113 Software Engineering Java 5 - GUIs Import the code to intellij https://github.com/cs2113f18/template-j-5.git Professor Tim Wood - The George Washington University Class Hierarchies Abstract Classes

More information

Interacción con GUIs

Interacción con GUIs Interacción con GUIs Delegation Event Model Fuente EVENTO Oyente suscripción Fuente Oyente suscripción EVENTO Adaptador EVENTO java.lang.object java.util.eventobject java.awt.awtevent java.awt.event. ActionEvent

More information

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 143 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN CSSE 220 Event Based Programming Check out EventBasedProgramming from SVN Interfaces are contracts Interfaces - Review Any class that implements an interface MUST provide an implementation for all methods

More information

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently. UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING 1. What are Threads? A thread is a single path of execution of code in a program. A Multithreaded program contains two or more parts that can run concurrently.

More information

Lecture 19 GUI Events

Lecture 19 GUI Events CSE 331 Software Design and Implementation Lecture 19 GUI Events The plan User events and callbacks Event objects Event listeners Registering listeners to handle events Anonymous inner classes Proper interaction

More information

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014 Introduction to GUIs Principles of Software Construction: Objects, Design, and Concurrency Jonathan Aldrich and Charlie Garrod Fall 2014 Slides copyright 2014 by Jonathan Aldrich, Charlie Garrod, Christian

More information

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

More information

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 5: Will be an open-ended Swing project. "Programming Contest"

More information

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun! Swing = Java's GUI library Topic 9: Swing Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Why are we studying Swing? 1. Useful & fun! 2. Good application of OOP techniques

More information

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Starting Out with Java: From Control Structures Through Objects Sixth Edition Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 12 A First Look at GUI Applications Chapter Topics 12.1 Introduction 12.2 Creating Windows 12.3 Equipping GUI Classes

More information

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is INDEX Event Handling. Key Event. Methods Of Key Event. Example Of Key Event. Mouse Event. Method Of Mouse Event. Mouse Motion Listener. Example of Mouse Event. Event Handling One of the key concept in

More information

CS11 Java. Fall Lecture 3

CS11 Java. Fall Lecture 3 CS11 Java Fall 2014-2015 Lecture 3 Today s Topics! Class inheritance! Abstract classes! Polymorphism! Introduction to Swing API and event-handling! Nested and inner classes Class Inheritance! A third of

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Hal Perkins Spring 2017 GUI Event-Driven Programming 1 The plan User events and callbacks Event objects Event listeners Registering listeners to handle events Anonymous

More information

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming

GUI Design. Overview of Part 1 of the Course. Overview of Java GUI Programming GUI Design Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu /~spring Overview of Part 1 of the Course Demystifying

More information

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

Example: CharCheck. That s It??! What do you imagine happens after main() finishes? Event-Driven Software Paradigm Today Finish Programming Unit: Discuss Graphics In the old days, computers did exactly what the programmer said Once started, it would run automagically until done Then you

More information

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation KF5008 Program Design & Development Lecture 1 Usability GUI Design and Implementation Types of Requirements Functional Requirements What the system does or is expected to do Non-functional Requirements

More information

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Frames, GUI and events Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling Introduction to Swing The Java AWT (Abstract Window Toolkit)

More information

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI 51 6!! GUI(Graphical User Interface) java.awt javax.swing (component) GUI... (container) (listener)... MouseListener, ActionLister (adapter)... MouseAdapter, ActionAdapter 6.1 GUI(Graphics User Interface

More information

BASICS OF GRAPHICAL APPS

BASICS OF GRAPHICAL APPS CSC 2014 Java Bootcamp Lecture 7 GUI Design BASICS OF GRAPHICAL APPS 2 Graphical Applications So far we ve focused on command-line applications, which interact with the user using simple text prompts In

More information

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13 CONTENTS Chapter 1 Getting Started with Java SE 6 1 Introduction of Java SE 6... 3 Desktop Improvements... 3 Core Improvements... 4 Getting and Installing Java... 5 A Simple Java Program... 10 Compiling

More information

Event Driven Programming

Event Driven Programming Event Driven Programming 1. Objectives... 2 2. Definitions... 2 3. Event-Driven Style of Programming... 2 4. Event Polling Model... 3 5. Java's Event Delegation Model... 5 6. How to Implement an Event

More information

Class 16: The Swing Event Model

Class 16: The Swing Event Model Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present

More information

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CSE 331 Software Design and Implementation. Lecture 19 GUI Events CSE 331 Software Design and Implementation Lecture 19 GUI Events Leah Perlmutter / Summer 2018 Announcements Announcements Quiz 7 due Thursday 8/9 Homework 8 due Thursday 8/9 HW8 has a regression testing

More information

PIC 20A GUI with swing

PIC 20A GUI with swing PIC 20A GUI with swing Ernest Ryu UCLA Mathematics Last edited: November 22, 2017 Hello swing Let s create a JFrame. import javax. swing.*; public class Test { public static void main ( String [] args

More information

Advanced Java Programming

Advanced Java Programming Advanced Java Programming Shmulik London Lecture #5 GUI Programming Part I AWT & Basics Advanced Java Programming / Shmulik London 2006 Interdisciplinary Center Herzeliza Israel 1 Agenda AWT & Swing AWT

More information

Jonathan Aldrich Charlie Garrod

Jonathan Aldrich Charlie Garrod Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduction to GUIs Jonathan Aldrich Charlie Garrod School of Computer Science 1 Administrivia Homework

More information

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs) CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox

More information

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling Handout 12 CS603 Object-Oriented Programming Fall 15 Page 1 of 12 Handout 14 Graphical User Interface (GUI) with Swing, Event Handling The Swing library (javax.swing.*) Contains classes that implement

More information

(Incomplete) History of GUIs

(Incomplete) History of GUIs CMSC 433 Programming Language Technologies and Paradigms Spring 2004 Graphical User Interfaces April 20, 2004 (Incomplete) History of GUIs 1973: Xerox Alto 3-button mouse, bit-mapped display, windows 1981:

More information

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4. 1 - What if the main method is declared as private? 1. The program does not compile 2. The program compiles but does not run 3. The program compiles and runs properly ( From Lectuer # 2) 4. The program

More information

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7... Table of Contents Chapter 1 Getting Started with Java SE 7 1 Introduction of Java SE 7... 2 Exploring the Features of Java... 3 Exploring Features of Java SE 7... 4 Introducing Java Environment... 5 Explaining

More information

Java: Graphical User Interfaces (GUI)

Java: Graphical User Interfaces (GUI) Chair of Software Engineering Carlo A. Furia, Marco Piccioni, and Bertrand Meyer Java: Graphical User Interfaces (GUI) With material from Christoph Angerer The essence of the Java Graphics API Application

More information

State Application Using MVC

State Application Using MVC State Application Using MVC 1. Getting ed: Stories and GUI Sketch This example illustrates how applications can be thought of as passing through different states. The code given shows a very useful way

More information

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC). The Abstract Windowing Toolkit Since Java was first released, its user interface facilities have been a significant weakness The Abstract Windowing Toolkit (AWT) was part of the JDK form the beginning,

More information

Java AWT Windows, Text, & Graphics

Java AWT Windows, Text, & Graphics 2 AWT Java AWT Windows, Text, & Graphics The Abstract Windows Toolkit (AWT) contains numerous classes and methods that allow you to create and manage applet windows and standard windows that run in a GUI

More information

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical

More information

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/

More information

MIT AITI Swing Event Model Lecture 17

MIT AITI Swing Event Model Lecture 17 MIT AITI 2004 Swing Event Model Lecture 17 The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the user. But how do GUIs interact with users? How do applications

More information

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements: More Frameworks Acknowledgements: K. Stirewalt. Johnson, B. Foote Johnson, Fayad, Schmidt Overview eview of object-oriented programming (OOP) principles. Intro to OO frameworks: o Key characteristics.

More information

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages. 1 CS257 Computer Science I Kevin Sahr, PhD Lecture 14: Graphical User Interfaces Command-Line Applications 2 The programs we've explored thus far have been text-based applications A Java application is

More information

Building Graphical User Interfaces. GUI Principles

Building Graphical User Interfaces. GUI Principles Building Graphical User Interfaces 4.1 GUI Principles Components: GUI building blocks Buttons, menus, sliders, etc. Layout: arranging components to form a usable GUI Using layout managers. Events: reacting

More information

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

Dr. Hikmat A. M. AbdelJaber

Dr. Hikmat A. M. AbdelJaber Dr. Hikmat A. M. AbdelJaber GUI are event driven (i.e. when user interacts with a GUI component, the interaction (event) derives the program to perform a task). Event: click button, type in text field,

More information

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components CS112-Section2 Hakan Guldas Burcin Ozcan Meltem Kaya Muge Celiktas Notes of 6-8 May Graphics Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how

More information

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University Java Swing Recitation 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University Announcements Project 8 is out Milestone due on Dec 3rd, 10:00 pm Final due on Dec 10th, 10:00 pm No classes,

More information

Swing from A to Z Using Focus in Swing, Part 2. Preface

Swing from A to Z Using Focus in Swing, Part 2. Preface Swing from A to Z Using Focus in Swing, Part 2 By Richard G. Baldwin Java Programming, Lecture Notes # 1042 November 27, 2000 Preface Introduction Sample Program Interesting Code Fragments Summary What's

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ GRAPHICAL USER INTERFACES 2 HelloWorld Reloaded

More information

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) ) COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) ) Course Evaluations Please do these. -Fast to do -Used to

More information

Introduction to concurrency and GUIs

Introduction to concurrency and GUIs Principles of Software Construction: Objects, Design, and Concurrency Part 2: Designing (Sub)systems Introduction to concurrency and GUIs Charlie Garrod Bogdan Vasilescu School of Computer Science 1 Administrivia

More information

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam

University of Cape Town ~ Department of Computer Science. Computer Science 1016S / 1011H ~ November Exam Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1016S / 1011H ~ 2009 November Exam Question

More information

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Graphics and Events Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Draw shapes, text in various fonts, and colors. 2) Build

More information

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! ADVANCED OBJECT-ORIENTATION 2 Object-Oriented Design classes often do not exist in isolation from each

More information

SINGLE EVENT HANDLING

SINGLE EVENT HANDLING SINGLE EVENT HANDLING Event handling is the process of responding to asynchronous events as they occur during the program run. An event is an action that occurs externally to your program and to which

More information

Together, the appearance and how user interacts with the program are known as the program look and feel.

Together, the appearance and how user interacts with the program are known as the program look and feel. Lecture 10 Graphical User Interfaces A graphical user interface is a visual interface to a program. GUIs are built from GUI components (buttons, menus, labels etc). A GUI component is an object with which

More information