Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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?

9 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 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!

11 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.

12 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

13 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 }

14 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.

15 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?

16 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

17 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!

18 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

19 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(); }

20 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


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

Similar presentations


Ads by Google