How can I set the insets of a JFrame?
Asked Answered
W

5

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.

Weswesa answered 2/1, 2013 at 5:54 Comment(3)
If using a JPanel for the content pane, simply panel.setBorder(new EmptyBorder(10,10,10,10));Lachus
@AndrewThompson If the OP is asking about setting the insets on the frame why not just override getInsets()?Downstate
@Downstate 1) Is “Don't do it” a valid answer? 2) 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.Lachus
O
29
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.

Orgell answered 2/1, 2013 at 6:13 Comment(1)
I didn't use the method getContentPane, I created a JPanel name contentPanel to have access to setBorder method.Orgell
C
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.

Chi answered 2/1, 2013 at 6:39 Comment(0)
C
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);
Cantonment answered 2/1, 2013 at 6:1 Comment(0)
D
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();
    }
}
Downstate answered 18/11, 2016 at 15:17 Comment(1)
this is not a good way, because the insets are ruined in case of resizeWakeful
W
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://mcmap.net/q/574921/-how-to-add-padding-to-a-jpanel-with-a-border

Wakeful answered 23/4, 2019 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.