How can I set the insets of a JFrame?
34,381
Solution 1
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.
Solution 2
Overriding the Insets
of JFrame
would not be the soultion to your actual problem.
To answer your question, you cannot set the Insets of JFrame
. You should extend JFrame and override the getInsets
method to give the insets you require.
Solution 3
You have to create an Object of LayOutConstraint and set its Insets. Like in below example I have used GridBagLayout() and used GridBagConstraint() object.
GridBagConstraints c = new GridBagConstraints();
JPanel panel = new JPanel(new GridBagLayout());
c.insets = new Insets(5, 5, 5, 5); // top, left, bottom, right
c.anchor = GridBagConstraints.LINE_END;
// Row 1
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.LINE_START;
panel.add(isAlgoEnabledLabel, c);
Author by
JavaNewbie_M107
I'm a high school computer student learning Java.
Updated on July 09, 2022Comments
-
JavaNewbie_M107 almost 2 years
Is there a way to set the insets of a
JFrame
? I triedframe.getContentPane().getInsets().set(10, 10, 10, 10);
and
frame.getInsets().set(10, 10, 10, 10);
but none of them seem to work.
-
Mark Vizcarra about 11 yearsI didn't use the method
getContentPane
, I created aJPanel
namecontentPanel
to have access tosetBorder
method. -
Alon Gouldman about 5 yearsthis is not a good way, because the insets are ruined in case of resize