Remember to call pack() in the dialog box before setVisible(true) .
In addition, many here recommend this instead of setting the preferredSize dialog by overriding his or his getPreferredSize() content panel as a cleaner and safer way to getPreferredSize() it correctly.
Edit
Holger posted:
An override of getPreferredSize () is not better if it is simply used to return the same constant size.
I think we all agree that if getPreferredSize() dull constant size, it should generally be avoided (although it is stable). In most cases, I avoid setting the size / preferred size or overriding getPreferredSize, but rather I try to ensure that my components and their own preferred sizes, as well as layout managers, are all on their own, which is again triggered by the pack() method. Sometimes I will need to make a more active throw, trying to set the size of the component, for example, if I need to put the component in the background image, and then override getPreferredSize() and use the image sizes to measure returns, or will use some kind of program logic to help determine the best size. But then again, we can all agree that the less you do it, the better.
Edit 2
Holger posted:
... but the question is, JDialog and dialogs never automatically adapt to their preferred sizes.
Again, they do all the top-level windows when you call pack() on them. That is why I recommend it, and why it was my main answer to his question.
source share