Search code examples
javaswingjscrollpanejdialogscrollpane

How to handle oversized JDialog


I'm developing a JDialog which will have three sections, one on the top of the other: customer general information, list of customer addresses and list of orders.

The list of customer addresses may grow arbitrarily large and this causes the dialog to grow beyond the screen height.

Thus, calling pack() before displaying it does not work because the packed dialog height is still too big for the screen height.

This calls for a scroll pane. But I would have to set the dialog's preferred size to take advantage of it, and defining a size for it depends on the display resolution. I'm not sure how to do that.

How should I handle this scenario?

the oversized dialog


Solution

  • First off, you can use Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); to get the screen size and then size the dialog proportionally (and place the content in a JScrollPane).

    Second, I'd suggest adding some kind of collapse on items so you only see the ones you are actually interested in.