1

I'm building dynamic JDialogs. I have some JPanels, and when you choose a menu option I create a modal JDialog, add a JPanel and display it:

JDialog dlg = new JDialog(this, title, true);
dlg.getContentPane().add(panel);
dlg.pack();
dlg.setLocationRelativeTo(null);
dlg.setVisible(true);

My app is working well, but there is no space between JDialog's border and the added panel, and it looks very "full/stretched" (I'm sorry, I don't know how to say this). Maybe the problem is that I'm calling JDialog.pack(), but at runtime I don't know the size of the panel, so I create the JDialog without dimensions, and after adding the content I pack it.

And my question is: What can I do to add some space around the panel?

One possible solution is adding a BorderLayout, putting the panel at center, and 4 fixed size panels at each side... But this seems an ugly hack...

mKorbel
  • 109,525
  • 20
  • 134
  • 319
marc casas
  • 133
  • 2
  • 9
  • 3
    [A wild URL appears!](http://stackoverflow.com/questions/5328405/jpanel-padding-in-java) –  Jul 30 '13 at 21:40
  • 2
    For more general solutions to crammed space problems: http://stackoverflow.com/questions/17874717/providing-white-space-in-a-swing-gui – kiheru Jul 30 '13 at 21:51

1 Answers1

6

Simply add an empty border to your panel:

panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255