How can I set the jframe inserts?

Is there any way to set JFrame inserts? I tried

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

and

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

but none of them work.

+8
java swing jframe insets
source share
4 answers
 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.

+16
source share

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.

+3
source share

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); 
0
source share

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.

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(); } } 
0
source share

All Articles