Question

I know I'm bound to take flak for it, but I'm attempting to set up a JFrame that uses a null layout in its content pane so that I can use absolute positioning for my components.

The problem I'm having is that there is always this gap in between the bottom and right sides of my components and the edge of the window. I thought maybe I was just positioning/sizing things wrong, so I tested using a single JPanel and giving it the same bounds as the content pane, but there was still the gap.

It's equal on both sides and looks like it's the twice width of the window's border. Here's the code:

public static void main(String[] args) {
        JFrame mainWindow = new JFrame("test");
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel root = new JPanel();
        root.setSize(1280, 720);
        root.setPreferredSize(new Dimension(1280, 720));
        root.setBackground(Color.GREEN);
        root.setLayout(null);
        mainWindow.setContentPane(root);

        JPanel p1 = new JPanel();
        p1.setBounds(root.getBounds());
        p1.setBackground(Color.RED);
        root.add(p1);

        mainWindow.pack();
        mainWindow.setVisible(true);
        mainWindow.setResizable(false);
}

I thought that maybe not using a layout manager was the problem, so I tried leaving the root JPanel with its default flow layout and giving p1 a preferred size (of 1280x720). But, that had no effect other than centering p1 in the window. I've also tried not changing the content pane but rather adding root to it and then adding p1 to root (exactly like in the above code, except calling mainWindow.getContentPane().add(root); instead of mainWindow.setContentPane(root);) but there was no change.

The only thing I can think of at this point is that something is going on when the JFrame tries to size itself. Any ideas?

Was it helpful?

Solution

The problem I'm having is that there is always this gap in between the bottom and right sides of my components and the edge of the window

mainWindow.setResizable(false);

The above statement should be executed BEFORE the frame is packed.

I know I'm bound to take flak for it, but I'm attempting to set up a JFrame that uses a null layout in its content pane so that I can use absolute positioning for my components

Don't use a null layout unless your application supports dragging of components to random position. In this case the layout manager can't predict the location of the component. Otherwise use a layout manager.

OTHER TIPS

Not quite sure if this is what you are looking for but it works with a BorderLayout:

public static void main(String[] args) {
    JFrame mainWindow = new JFrame("test");
    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel root = new JPanel(new BorderLayout());
    root.setSize(1280, 720);
    root.setPreferredSize(new Dimension(1280, 720));
    root.setBackground(Color.GREEN);

    JPanel p1 = new JPanel(null);
    p1.setBounds(root.getBounds());
    p1.setBackground(Color.RED);
    root.add(p1);

    mainWindow.setContentPane(root);
    mainWindow.pack();
    mainWindow.setVisible(true);
    mainWindow.setResizable(false);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top