10

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.

JavaNewbie_M107
  • 2,007
  • 3
  • 21
  • 36
  • 1
    If using a `JPanel` for the content pane, simply `panel.setBorder(new EmptyBorder(10,10,10,10));` – Andrew Thompson Jan 02 '13 at 06:10
  • @AndrewThompson If the OP is asking about setting the insets on the frame why not just override `getInsets()`? – Dan Nov 18 '16 at 15:53
  • @Dan 1) [Is “Don't do it” a valid answer?](http://meta.stackexchange.com/questions/8891/is-dont-do-it-a-valid-answer) 2) [Composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance) 3) It is rare that we can make an application using a single layout, so may as well create a main panel as content pane. 4) But apart from those three compelling reasons, I guess not. – Andrew Thompson Nov 18 '16 at 16:14

5 Answers5

28
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.

Mark Vizcarra
  • 1,165
  • 1
  • 11
  • 15
  • I didn't use the method `getContentPane`, I created a `JPanel` name `contentPanel` to have access to `setBorder` method. – Mark Vizcarra Mar 29 '13 at 07:49
4

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.

basiljames
  • 4,777
  • 4
  • 24
  • 41
2

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);
Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
0

As this question does not have a definitive answer yet you can do it like basiljames said here. The correct way to do it would be to extend a 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();
    }
}
Community
  • 1
  • 1
Dan
  • 7,286
  • 6
  • 49
  • 114
0

you can create a main JPanel and insert everything else into it.

Then you can use BorderFactory to create EmptyBorder or LineBorder.

see this answer: https://stackoverflow.com/a/17925693/8953378

Alon Gouldman
  • 3,025
  • 26
  • 29