How can I set the insets of a JFrame?

JavaNewbie_M107 picture JavaNewbie_M107 · Jan 2, 2013 · Viewed 33.2k times · Source

Is there a way to set the insets of a JFrame? I tried

frame.getContentPane().getInsets().set(10, 10, 10, 10);

and

frame.getInsets().set(10, 10, 10, 10);

but none of them seem to work.

Answer

Mark Vizcarra picture Mark Vizcarra · Jan 2, 2013
JPanel contentPanel = new JPanel();

Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10);

contentPanel.setBorder(padding);

yourFrame.setContentPane(contentPanel);

So basically, contentPanel is the main container of your frame.