PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications.

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
1 L46 Advanced GUI Component (1). 2 OBJECTIVES  To create and manipulate sliders, and menus,
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Concurrent Programming and Threads Threads Blocking a User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Chapter 4© copyright Janson Industries More GUI ▮ Events, Actions, and Listeners ▮ The Visual Editor ▮ More GUI component properties Non-graded assg.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
TCU CoSc Programming with Java The JFrame Class.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Modular Event Handling
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Web Design & Development Lecture 12
Web Design & Development Lecture 13
Events, Event Handlers, and Threads
Tonga Institute of Higher Education
Presentation transcript:

PROG Object Oriented Programming II PROG Object Oriented Programming II Window Events Multi-screen Applications

Window Events Events that are triggered by various interactions with the Jframe Saving data to a file when the user closes the window Passing data from one window to another Terminate a process/thread when a window is minimized Restart a process/thread when a window is restored 10/7/2015Wendi Jollymore, ACES2

Window Listeners WindowListener* Handles most window events Active/inactive, minimize/restore, open/close WindowFocusListener For JWindow/Window objects These don’t have the active/inactive event handlers WindowStateListener Used for events that deal with the “state” of the window This is the only listener that can detect “maximized” windows 10/7/2015Wendi Jollymore, ACES3 java.awt.event

Window Event Objects 10/7/2015Wendi Jollymore, ACES4 WindowEvent objects are created when a window event is triggered with a registered window This is the param type in the window event handler methods Contains a few methods that are useful for detecting the window’s current or previous state i.e if it was active and just became inactive

Event Handler Methods 10/7/2015Wendi Jollymore, ACES5 WindowListener methods: windowActivated() When the window becomes the active window windowDeactivated() When the window is no longer the active window windowIconified() Just after the window is minimized windowDeiconified() Just after the window is restored

Event Handler Methods 10/7/2015Wendi Jollymore, ACES6 WindowListener methods, continued windowOpened() Just after a window is loaded from memory Not necessarily the first time it’s loaded During program run you can unload window from memory using dispose() windowClosing() When request is made to close the window windowClosed() Just after a window has been disposed of

10/7/2015Wendi Jollymore, ACES7 Demonstration Create this screen: 1.Implement WindowListener interface. 2.Register your JFrame with the listener. addWindowListener() 3.Add all WindowListener events 4.Append line to text area in each event describing the event occurring 5.See code in notes WindowExample JTextArea txtDisplay Label lblData, JTextField txtData, JButton cmdWin2

10/7/2015Wendi Jollymore, ACES8 windowClosing/windowClosed windowClosing() occurs when a request is made to close the window Doesn’t actually close the window Test this: Add a confirm dialog to confirm user exit in the windowClosing() event If yes, System.exit(0); otherwise, do nothing. What happens?

10/7/2015Wendi Jollymore, ACES9 windowClosing/windowClosed JFrame.defaultCloseOperation property EXIT_ON_CLOSE - terminates the program Same as System.exit(0) HIDE - hides the frame, but keeps it in memory Same as the frame’s setVisible(false) method DO_NOTHING – nothing: the frame stays visible and stays in memory Continued…

10/7/2015Wendi Jollymore, ACES10 windowClosing/windowClosed JFrame.defaultCloseOperation property Continued: DISPOSE – unloads the frame from memory This causes the form to hide Its resources are freed up Same as the frame's dispose() method Will terminate the program if no other windows are in memory!

10/7/2015Wendi Jollymore, ACES11 Demonstration, continued What is defaultCloseOperation set to now? For this demo, set defaultCloseOperation to DO_NOTHING This allows us to control when the program exits. Try the program again! Note what appears in the text area as you play with the Close(X) button.

Multiple Windows Most applications consist of multiple windows/frames One window spawns or instantiates another window Parent creates/spawns child (not an inheritance relationship, just another use of the same terms) 10/7/2015Wendi Jollymore, ACES12

Multiple Windows Example: 10/7/2015Wendi Jollymore, ACES13 public void actionPerformed(ActionEvent event) { if (event.getSource() == cmdWin2) { WindowTwo win2 = new WindowTwo(); // any code you might want to add to set up // second window; this could also go in the // 2nd window’s constructor. win2.setVisible(true); } else if // whatever other code you have // for event handler } public void actionPerformed(ActionEvent event) { if (event.getSource() == cmdWin2) { WindowTwo win2 = new WindowTwo(); // any code you might want to add to set up // second window; this could also go in the // 2nd window’s constructor. win2.setVisible(true); } else if // whatever other code you have // for event handler }

10/7/2015Wendi Jollymore, ACES14 Demonstration Create this secondary screen: Window2 JLabel lblInput JTextArea txtInput, JButton cmdBack Label lblData, JTextArea txtData We’ll add code for this window later.

10/7/2015Wendi Jollymore, ACES15 Demonstration, continued Go back to main WindowExample screen Implement the ActionListener Add the actionPerformed() method Register cmdWindow2 with the action listener actionPerformed: check for cmdWindow2 add the statements to instantiate Window2 and make it visible Test it Click the button, flip back and forth between the windows Click Close(X) on 2 nd form! What happens?

10/7/2015Wendi Jollymore, ACES16 Demonstration, continued Window2’s defaultCloseOperation is set to exit on close What do we change it to? If you will be flipping back and forth between windows, use HIDE Makes the program faster Not re-loading window every time If you need to delete any reference to the 2 nd window object, use DISPOSE For this demo, use DISPOSE for the 2 nd Window object

10/7/2015Wendi Jollymore, ACES17 Demonstration, continued Try this: Click the button to open Window2. Click it again and again while Window2 is still open Oops! How do we make sure we only open one Window2 at a time? One way is to hide the main window Add the code to hide the main window after showing the second window!

10/7/2015Wendi Jollymore, ACES18 Demonstration, continued Note that this means we need a way to bring back the main form from Window2 Back button: Dispose of child form Show main form To do this, Window2 needs some way to access the main window Give Window2 a reference to the main window Use a private data member! Use constructors to set the window reference

10/7/2015Wendi Jollymore, ACES19 Demonstration, continued Now we make a method in Window2: This should be invoked from: cmdBack event handler windowClosing() Set defaultCloseOperation to DO_NOTHING private void goBackToMain() { parent.setVisible(true); this.dispose(); } private void goBackToMain() { parent.setVisible(true); this.dispose(); }

10/7/2015Wendi Jollymore, ACES20 Passing Data Between Windows Often you’ll want to send data to a second window Or have a form receive data from another window There are a variety of ways to do this Some use the technique of referencing one form within another Do the tutorial in the notes!the tutorial