7

I tried setPrefferedSize and setSize methods, but the dialog still opens at minimum size.

private void method() {
    commandDialog.setPreferredSize(new Dimension(100,100));
    - - - 
    - - -   //Components added to dialogPanel

    commandDialog.add(dialogPanel);

    // Tried this as well: commandDialog.setSize(40, 40);     
    commandDialog.validate();
    commandDialog.setVisible(true);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
dev
  • 2,474
  • 7
  • 29
  • 47

2 Answers2

20

Don't forget to call pack() on the dialog before setVisible(true).

Also, many here will recommend that rather than setting the dialog's preferredSize, overriding it or its content pane's getPreferredSize() method as a cleaner and safer way of having it size itself correctly.


Edit
Holger posted:

Overriding getPreferredSize() is not better if it is just used to return the same constant size.

I think that we all agree that having getPreferredSize() return a dumb constant size is usually to be avoided (although it is stable). Most of the time I avoid setting size/preferredSize or overriding getPreferredSize, but rather try to have my components and their own preferred sizes as well as the layout managers all size themselves, which again is initiated by the pack() method. Occasionally I will need to take a more active roll in trying to set the size of a component such as if I need to size a component to a background image, and then I'll override getPreferredSize() and use the image dimensions for the dimension returned, or will use some type of program logic to help figure out the best size. But again we all can agree that the less you futz with this, the better.


Edit 2
Holger posted:

... but the question is about a JDialog and dialogs never adapt themselves to their preferred size automatically.

Again, they and all top-level windows do when you call pack() on them. Which was why I recommend this, and why this was my primary answer to his question.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • +1, `setPreferredSize(Dimension)` is not actually an evil ;) but overriding `getPreferredSize(Dimension)` allows us to adjust preferred size relating to it's content. If no adjustment is required i don't see any problem with `setPreferredSize(Dimesion)` :) – Sage Nov 29 '13 at 21:04
  • @sage Other then the fact that if you use `setPreferredSize` you can reduce the effectiveness of components to adjust themselves to changes of the platform they are running on...which my predecessor did and I've still trying to fix (even after 3 years), so yes, `setPreferredSize` in the hands of the inexperienced and uneducated is evil - IMHO – MadProgrammer Nov 29 '13 at 21:14
  • Overriding `getPreferredSize()` is not better if it is just used to return the same constant size. – Holger Nov 29 '13 at 21:17
  • @Holger, i think what mad man(programmer) ;) is saying that, we actually need almost every time to adjust preferred size with content. Especially setting preferred size to text component is no go most of the time – Sage Nov 29 '13 at 21:19
  • @ Hovercraft Full Of Eels: I think, we have a little misunderstanding here. You are talking about general rules for components, but the question is about a `JDialog` and dialogs never adapt themselves to their preferred size automatically. They are always resized explicitly, regardless of whether you use `pack` or `setSize` for it. I never use `setPreferredSize` for components and override `getPreferredSize` very seldom. But for windows and dialogs I use `setSize` where it seems reasonably. They are special: when the user resizes them, (s)he is overriding the preferred size as well. – Holger Nov 29 '13 at 21:29
  • @Holger: there's no misunderstanding. Please see **Edit 2**. – Hovercraft Full Of Eels Nov 29 '13 at 21:33
6

Did you try

commandDialog.setSize(new Dimension(100, 100));
commandDialog.setResizable(false);

?

Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
  • Many things are better not to try, some of which you have recommended. :) Check the other answer below – Sage Nov 29 '13 at 21:05
  • @Sage yes what i recommended is bad but if it solves the problem why not use that :) – Anirban Nag 'tintinmj' Nov 29 '13 at 21:07
  • 1
    @tintinmj: because it does not respect the layout managers preferred sizes which will often mess up a GUI's display. You would need `pack()` to request that all of the layout managers recursively layout their children, and by doing this, the size set by `setSize()` is ignored. The bottom line is: you just shouldn't recommend this. – Hovercraft Full Of Eels Nov 29 '13 at 21:09
  • I don’t see why setting the preferred size to hard-coded values and let pack use them should be better than setting the dialog to a hard-coded size directly. *Both* are overriding the `LayoutManager`’s results. – Holger Nov 29 '13 at 21:10
  • 1
    @Holger: **again** pack does not use the hard coded size set via the `setSize(...)` method. – Hovercraft Full Of Eels Nov 29 '13 at 21:11
  • Bottom line I also don't like what I said. Hope someone will come up with better answer. Also wait for OP to say something. – Anirban Nag 'tintinmj' Nov 29 '13 at 21:12
  • @Hovercraft Full Of Eels: The other (your) proposal is to set the *preferred size*. Why should that be better? The entire question is about overriding the `LayoutManager`’s proposed size. – Holger Nov 29 '13 at 21:13
  • 1
    @Holger: It's better because that is what 95% of layout managers will use when sizing components and ultimately the GUI, but it has dangers in that it can be overridden by other coders. That is why many here, including kleopatra, one of the Swing gurus on this site and one of the authors of the SwingX utilities, recommend that the `getPreferredSize()` method be overridden instead. – Hovercraft Full Of Eels Nov 29 '13 at 21:15
  • 1
    @tintinmj: The risk of `setResizable(false)` is illustrated [here](http://stackoverflow.com/a/12532237/230513). – trashgod Nov 30 '13 at 01:28