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

