Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Different Window.dispose() behavior when Window is visible/not visible
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st May 17:25
dfsamuel
External User
 
Posts: 1
Default Different Window.dispose() behavior when Window is visible/not visible



I have a problem with dispose() and setVisible() interactions.
Basically my problem is that dipose() works differently if the window
is visible or not where there are dialogs owned by the window. If the
window is visible and you call dispose on it, then all owned windows
dissapear, if you call dispose when the window is not visible, then
the dialogs don't dissapear. Why is this different? Shouldn't
dispose() get rid of the dialogs regardless of visibility??

More importantly, how can I get rid of these dialogs when the window
is not visible? That is my desired result.

Thanks,

-Forrest

Please see the test case below:

///////////////////////////////////////////////////////////////
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.*;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class DisposeTest {

public static void main(String[] args) {
JFrame frame = new JFrame("JFrame");
frame.getContentPane().add(new JLabel("FRAME"));
frame.setSize(150, 75);
frame.setLocation(getCenterOfScreen());
frame.setVisible(true);

JDialog dlg = new JDialog(frame, "Dialog");
dlg.getContentPane().add(new JLabel("DIALOG"));
dlg.setSize(50, 50);
dlg.setLocationRelativeTo(frame);
dlg.setVisible(true);

test1(frame);
test2(frame);
test3(frame);
}
/**
* In this test the frame is initialy visible with it's dialog also
visible.
* I then call setVisible(false) and dispose() on the frame. When I
call setVisible(true)
* the frame AND dialog are shown.
* @param frame
*/
private static void test1(JFrame frame) {
sleep(5000);
System.out.println("HIDE frame setVisible(false)");
frame.setVisible(false);

sleep(5000);
System.out.println("DISPOSE frame dispose()");
frame.dispose();

sleep(5000);
System.out.println("SHOW frame setVisible(true)");
frame.setVisible(true);
}

/**
* In this test the frame is initialy visible with it's dialog also
visible.
* I then call setVisible(false) and setVisible(true) on the frame.
When I call setVisible(true)
* the frame AND dialog are shown.
* @param frame
*/
private static void test2(JFrame frame) {
sleep(5000);
System.out.println("HIDE frame setVisible(false)");
frame.setVisible(false);

sleep(5000);
System.out.println("SHOW frame setVisible(true)");
frame.setVisible(true);
}

/**
* In this test the frame is initialy visible with it's dialog also
visible.
* I then call dispose() on the frame. When I call setVisible(true)
* the frame is shown but the dialog is gone.
* @param frame
*/
private static void test3(JFrame frame) {
sleep(5000);
System.out.println("DISPOSE frame dispose()");
frame.dispose();

sleep(5000);
System.out.println("SHOW frame setVisible(true)");
frame.setVisible(true);
}

public final static Point getCenterOfScreen() {
int x = (int)Toolkit.getDefaultToolkit().getScreenSize().g etWidth()
/ 2;
int y = (int)Toolkit.getDefaultToolkit().getScreenSize().g etHeight()
/ 2;
return new Point(x, y);
}

public final static void sleep(long time){
try {Thread.sleep(time);} catch (InterruptedException e)
{e.printStackTrace();}
}
}
///////////////////////////////////////////////////////////////
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666