prog 24168 object oriented programming ii prog 24168 object oriented programming ii window events...

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

Upload: bennett-cummings

Post on 12-Jan-2016

260 views

Category:

Documents


0 download

TRANSCRIPT

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

PROG 24168

Object Oriented Programming II

PROG 24168

Object Oriented Programming II

Window Events

Multi-screen Applications

Page 2: 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

Page 3: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 4: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 5: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 6: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 7: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 8: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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?

Page 9: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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…

Page 10: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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!

Page 11: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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.

Page 12: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 13: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 14: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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.

Page 15: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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?

Page 16: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 17: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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!

Page 18: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 19: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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

Page 20: PROG 24168 Object Oriented Programming II PROG 24168 Object Oriented Programming II Window Events Multi-screen Applications

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!