JDialog with parent Frame iconified

Hi,

I'm trying to write an application that pops up a dialog box but also displays an icon in taskbar. Following is the sample code that I wrote. The problem is that as soon as the dialog box is closed the window stays put. Even setting is setVisible(false) seems to have no effect on it. If the commented line is uncommented, then it seems to behave perfectly. Is it a bug? A window being visible even though setVisible(false) is called. It works fine if setVisible(true) is called before setVisible(false). Any ideas?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class TestJFrame extends JFrame {

牋牋public static void main(String[] args) {

牋牋牋牋TestJFrame tjf = new TestJFrame();

牋牋牋牋JDialog jd = new JDialog(tjf, true);

牋牋牋牋tjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

牋牋牋牋int frameState = tjf.getExtendedState();

牋牋牋牋tjf.setExtendedState(Frame.ICONIFIED);

//牋牋牋tjf.setVisible(true);

牋牋牋牋jd.setVisible(true);

牋牋牋牋tjf.setVisible(false);

牋牋牋牋tjf.setExtendedState(frameState);

牋牋}

}

Regards,

Peeyush.

[1149 byte] By [peeyushg1a] at [2007-10-2 13:19:32]
# 1
Basically, calling isVisible() before setVisible(false) [when the commented line stays put] seems to return false and the window is VISIBLE!!
peeyushg1a at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...
# 2

Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] when posting code so the code is readable.

Maybe something like this will work:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class TestJFrame extends JFrame {

public static void main(String[] args)

{

TestJFrame tjf = new TestJFrame();

tjf.setLocation(-200, 0);

tjf.setVisible( true );

JDialog jd = new JDialog(tjf, true);

jd.setSize(300, 300);

jd.setLocationRelativeTo( null );

jd.setVisible( true );

tjf.dispose();

}

}

camickra at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for your reply. That is a great workaround but not the ideal solution. Suppose we had two monitors with the right one as the primary, this window would show up on the left monitor. Another thing is that I do not want to dispose off that window. I showed a very simplified version of the code (so as not to distract away from the core topic) but in reality that is a singleton instance of the window that pops up various dialogs based on some user input in other windows and I wanted to show those dialogs with icons in taskbar as well.

The code (I'm pasting again with some modifications and formating this time :) ) works if I uncomment the commented line. My question was why it does not work if that line remained commented.

The problem is that with the line commented you would see the output I'm INVISIBLE

even though the window is visible. Is that a bug? If you try the following code with and without uncommenting the line tjf.setVisible(true);

you will see what I mean.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class TestJFrame extends JFrame {

public static void main(String[] args) {

TestJFrame tjf = new TestJFrame();

tjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

int frameState = tjf.getExtendedState();

tjf.setExtendedState(Frame.ICONIFIED);

// tjf.setVisible(true);

JDialog jd = new JDialog(tjf, true);

jd.setSize(300, 300);

jd.setLocationRelativeTo(null);

jd.setVisible(true);

if (tjf.isVisible()) {

System.out.println("I'm VISIBLE! Going to be invisible");

tjf.setVisible(false);

}

System.out.println("I'm INVISIBLE");

// Restore the original state

tjf.setExtendedState(frameState);

}

}

peeyushg1a at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...
# 4

Well, I don't understand you requirement so I can't help you anymore. I few more comments before I go.

> Suppose we had two monitors with the right one as the primary

Change the location to (0, -200);

> Another thing is that I do not want to dispose off that window.

> I showed a very simplified version...

Likewise, I showed a simple version. you don't have to dispose of the window if you don't want to.

When I run your code on XP using JDK1.4.2 this is what happens:

a) dialog is displayed

b) because the dialog is modal no code below the jd.setVisible(..) executes.

c) when I close the dialog the output "I'm invisible" displays on the console as expected since the dialog is closed and the code following the setVisible(..) now executes.

camickra at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...
# 5

I'm sorry if I wasn't clear. Here's the requirement. I have a primary window with an icon in the taskbar and it needs to pop up a modal dialog such that I see another icon in the taskbar.

Running the code does what you said that is,

a) dialog is displayed

b) because the dialog is modal no code below the jd.setVisible(..) executes.

c) when I close the dialog the output "I'm invisible" displays on the console as expected since the dialog is closed and the code following the setVisible(..) now executes.

But, even though the output "I'm INVISIBLE" shows, the parent window is actually VISIBLE!

This kind of behaviour does not happen if the commented line of code is uncommented. In this case, the parent window actually becomes INVISIBLE.

My question here really was that is this the expected kind of behaviour in the first scenario or is it a bug? Why I call it a bug is because the window is displayed even though the output says it is invisible.

I'm using 1.5.0_04 on XP but I have tried it with 1.4.2_10 as well on 2000.

I hope I was able to better explain it this time.

peeyushg1a at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...
# 6

> My question here really was that is this the expected kind of behaviour in the first scenario or is it a bug?

Well, the frame isn't visible so I wouldn't expect to see your "I"m visible...going to invisible message. So that works the way I would expect it.

The reason your frame becomes visible is because you reset the state of the frame back to normal. So the change of state from Iconified to Normal appears to be causing the frame to become visible. If you remove either of the setExtendedState() method calls the frame will remain invisible. I don't see why you need to reset the state anyway. As I understand your requirement its just a dummy JFrame so an Icon will display in the task bar, Therefore it should close when the dialog closes.

I don't have a problem with the way its working. Why change the state from iconified to normal if you don't expect to see the frame? So the fact that it becomes visible seems reasonable to me.

camickra at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...
# 7

I really appreciate all your help.

> The reason your frame becomes visible is because you

> reset the state of the frame back to normal. So the

> change of state from Iconified to Normal appears to

> be causing the frame to become visible.

Then why doesn't it go inside the 'if' statement printing 'I'm VISIBLE... going to be invisible'.

> If you remove

> either of the setExtendedState() method calls the

> frame will remain invisible.

Well, again not entirely true. I tried that. The frame remains minimized but by definition of invisible, it should disappear from the task bar. Instead, clicking on it brings up the window.

> I don't see why you need

> to reset the state anyway. As I understand your

> requirement its just a dummy JFrame so an Icon will

> display in the task bar, Therefore it should close

> when the dialog closes.

In my requirements, the frame that is used to display dialogs is a singleton and it can display different types of dialogs depending on what was invoked from the main window. So I cannot really close it. Instead, I have to make it invisible so that when another dialog needs to be displayed, it again appears on the task bar.

peeyushg1a at 2007-7-13 10:53:50 > top of Java-index,Desktop,Core GUI APIs...