Why is JDialog returning wrong size?

I have a JDialog and want to check whether the user has resized it. Here is an example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass TestDialogextends JDialog{

Dimension initialSize;

TestDialog(){

JButton button =new JButton("OK");

button.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

System.out.println("initial size: " + initialSize +" now: " + TestDialog.this.getSize());

}

});

getContentPane().add(button);

pack();

show();

initialSize = getSize();

}

publicstaticvoid main (String[] args){

new TestDialog();

}

}

When running the program, I usually get the following output when clicking the button, without having resized the dialog myself:

initial size: java.awt.Dimension[width=63,height=55] now: java.awt.Dimension[width=116,height=51]

The first size is incorrect, the second one is correct.

How come the dialog gives two different sizes, without any resizing inbetween?

How can I get the correct initial size of the JDialog?

Note: Putting the initialSize = getSize(); into a SwingUtilities.invokeLater() did not help.

Your help is appreciated,

Daniel

[2125 byte] By [dantams] at [2007-9-27 14:49:43]
# 1
I haven't tried to run your code, but just looking at it ... I don't like that you're getting the initial size before you're out of the constructor. My initial thought is that the object (ie: dialog box) is not yet instantiated & you won't get a valid size.
jlemm at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 2

Which jdk are you using?

I tried your code with java 1.4.0-rc-b91 and java 1.3.1-b24 under windows xp. And both of them had the following output:

TestDialog

initial size: java.awt.Dimension[width=123,height=61] now: java.awt.Dimension[width=123,height=61]

Michael

UniqueOS at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 3

UniqueOS: I am using Sun JDK 1.4.0 on Linux. I had noticed that I needed to run the program several times, most times it would give wrong answers, but sometimes it would give the correct output.

jlemm: Well, all the GUI code is already executed before I capture the size, and I need the original size before it is resized. However, I will try to find another way of capturing the orignal size, outside the constructor (maybe in a ComponentListener) and see if that helps.

- Daniel

dantams at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 4

I now implemented it so that the size is captured by a ComponentListener, which checks a boolean to ensure the size has not been captured already. This way it works.

However, I would still like to know why my initial implementation does not work. BTW, the dialog constructor is invoked by the event-dispatching thread. Does that ring any alarm bells for anyone?

- Daniel

dantams at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 5

Sorry if I confused anyone. In my own project, where I tried to use the method shown in my first post, the constructor of the dialog is invoked by the event-dispatching thread.

In the above example it would be equivalent to public static void main(String[] args) {

SwingUtilites.invokeLater(new Runnable() {

public void run() {

new TestDialog();

}

});

}

dantams at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 6
I've built a workaround to the problem, but I still do not understand why my original code does not work.Can anyone of the Swing gurus in this forum enlighten me? Otherwise I will have to report it as a bug.- Daniel
dantams at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 7
Well, I guess I will have to report it as a bug. You get a dollar each for the effort.- Daniel
dantams at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...
# 8
The bug has now been registered in the bug database. It can be found here: http://developer.java.sun.com/developer/bugParade/bugs/4734367.html.- Daniel
dantams at 2007-7-5 22:49:47 > top of Java-index,Archived Forums,Swing...