prog 24168 object oriented programming ii prog 24168 object oriented programming ii window events...
TRANSCRIPT
PROG 24168
Object Oriented Programming II
PROG 24168
Object Oriented Programming II
Window Events
Multi-screen Applications
Window EventsWindow Events
Events that are triggered by various interactions with the Jframe
Saving data to a file when the user closes the windowPassing data from one window to anotherTerminate a process/thread when a window is minimizedRestart a process/thread when a window is restored
04/21/23 Wendi Jollymore, ACES 2
Window ListenersWindow Listeners
WindowListener*Handles most window eventsActive/inactive, minimize/restore, open/close
WindowFocusListenerFor JWindow/Window objects
These don’t have the active/inactive event handlers
WindowStateListenerUsed for events that deal with the “state” of the window
This is the only listener that can detect “maximized” windows
04/21/23 Wendi Jollymore, ACES 3
java.awt.event
Window Event ObjectsWindow Event Objects
04/21/23 Wendi Jollymore, ACES 4
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 statei.e if it was active and just became inactive
Event Handler MethodsEvent Handler Methods
04/21/23 Wendi Jollymore, ACES 5
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 MethodsEvent Handler Methods
04/21/23 Wendi Jollymore, ACES 6
WindowListener methods, continuedwindowOpened()
Just after a window is loaded from memoryNot necessarily the first time it’s loadedDuring 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
04/21/23 Wendi Jollymore, ACES 7
DemonstrationDemonstration
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 occurring5. See code in notes
WindowExample
JTextArea txtDisplay
Label lblData,JTextField txtData,JButton cmdWin2
04/21/23 Wendi Jollymore, ACES 8
windowClosing/windowClosedwindowClosing/windowClosed
windowClosing() occurs when a request is made to close the window
Doesn’t actually close the windowTest this:
Add a confirm dialog to confirm user exit in the windowClosing() eventIf yes, System.exit(0); otherwise, do nothing.What happens?
04/21/23 Wendi Jollymore, ACES 9
windowClosing/windowClosedwindowClosing/windowClosed
JFrame.defaultCloseOperation propertyEXIT_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 memoryContinued…
04/21/23 Wendi Jollymore, ACES 10
windowClosing/windowClosedwindowClosing/windowClosed
JFrame.defaultCloseOperation propertyContinued:DISPOSE – unloads the frame from memory
This causes the form to hide Its resources are freed upSame as the frame's dispose() methodWill terminate the program if no other windows are in memory!
04/21/23 Wendi Jollymore, ACES 11
Demonstration, continuedDemonstration, 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 WindowsMultiple Windows
Most applications consist of multiple windows/frames
One window spawns or instantiates another windowParent creates/spawns child
(not an inheritance relationship, just another use of the same terms)
04/21/23 Wendi Jollymore, ACES 12
Multiple WindowsMultiple Windows
Example:
04/21/23 Wendi Jollymore, ACES 13
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 }}
04/21/23 Wendi Jollymore, ACES 14
DemonstrationDemonstration
Create this secondary screen:Window2
JLabel lblInputJTextArea txtInput,JButton cmdBack
Label lblData,JTextArea txtData
We’ll add code for this window later.
04/21/23 Wendi Jollymore, ACES 15
Demonstration, continuedDemonstration, continued
Go back to main WindowExample screenImplement the ActionListener Add the actionPerformed() methodRegister cmdWindow2 with the action listeneractionPerformed: check for cmdWindow2
add the statements to instantiate Window2 and make it visible
Test itClick the button, flip back and forth between the windowsClick Close(X) on 2nd form! What happens?
04/21/23 Wendi Jollymore, ACES 16
Demonstration, continuedDemonstration, 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 fasterNot re-loading window every time
If you need to delete any reference to the 2nd window object, use DISPOSE
For this demo, use DISPOSE for the 2nd Window object
04/21/23 Wendi Jollymore, ACES 17
Demonstration, continuedDemonstration, 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 windowAdd the code to hide the main window after showing the second window!
04/21/23 Wendi Jollymore, ACES 18
Demonstration, continuedDemonstration, continued
Note that this means we need a way to bring back the main form from Window2
Back button:Dispose of child formShow main form
To do this, Window2 needs some way to access the main windowGive Window2 a reference to the main window
Use a private data member!Use constructors to set the window reference
04/21/23 Wendi Jollymore, ACES 19
Demonstration, continuedDemonstration, continued
Now we make a method in Window2:
This should be invoked from:cmdBack event handlerwindowClosing()
Set defaultCloseOperation to DO_NOTHING
private void goBackToMain() { parent.setVisible(true); this.dispose(); }
private void goBackToMain() { parent.setVisible(true); this.dispose(); }
04/21/23 Wendi Jollymore, ACES 20
Passing Data Between WindowsPassing 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 thisSome use the technique of referencing one form within another
Do the tutorial in the notes!