Is there any way to set JFrame inserts? I tried
JFrame
frame.getContentPane().getInsets().set(10, 10, 10, 10);
and
frame.getInsets().set(10, 10, 10, 10);
but none of them work.
JPanel contentPanel = new JPanel(); Border padding = BorderFactory.createEmptyBorder(10, 10, 10, 10); contentPanel.setBorder(padding); yourFrame.setContentPane(contentPanel);
Basically, the contentPanel is the main container of your frame.
contentPanel
Overriding Insets of JFrame would not be the Insets of your real problem. To answer your question, you cannot set JFrame inserts. You must extend the JFrame and override the getInsets method to provide the inserts you need.
Insets
getInsets
You need to create a LayOutConstraint object and set its inserts. As in the following example, I used GridBagLayout () and used the 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);
Since this question has no final answer, you can do it like basiljames here . The proper way to do this is to extend the JFrame and then override the getInsets() method.
getInsets()
for example
import javax.swing.JFrame; import java.awt.Insets; public class JFrameInsets extends JFrame { @Override public Insets getInsets() { return new Insets(10, 10, 10, 10); } private JFrameInsets() { super("Insets of 10"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setMinimumSize(getSize()); setVisible(true); } public static void main(String[] args) { new JFrameInsets(); } }