2

When I put something in content pane that has flow layout manager i get free space between that component and borders of content pane.

The default flow layout has hgap and wgap non-zero, but setting them to zero DOESN'T solve the problem.

NOTE: Please help me in this layout, and not suggest to use another layout, cause this is excerpt from larger app.

Setting negative hgap and wgap can help but this is surely not the solution. Here is the code to inspect:

import java.awt.*;
import javax.swing.*;

public class MainWindow extends JFrame{

public MainWindow (){
    JPanel contentPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

    JPanel wp = new JPanel();
    wp.setPreferredSize(new Dimension(500, 500));
    wp.setBackground(Color.DARK_GRAY);

    contentPane.add(wp);

    setContentPane(contentPane);

    pack();
    setResizable(false);
    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
        new MainWindow();
}
}
croraf
  • 4,332
  • 2
  • 31
  • 50
  • 1
    setting hgap and vgap to `0` should actually do the trick. Please post a [SSCCE](http://sscce.org) which is capable of producing your issue – Sage Dec 07 '13 at 12:05
  • hgap and vgap that are set to zero is the posted code with commented line uncommented, and without the first line in constructor. Just uncoment that and you'll see that it does not solve. – croraf Dec 07 '13 at 12:08
  • i have seen that, what i actually meant the component code you are adding. As i am not having(and actually anyone won't have) any issue with `hgap` and `vgap` if they were set to zero. So we need to see the your working version which is able to reproduce your described issue and yet small in size. Only then we can believe you – Sage Dec 07 '13 at 12:12
  • It is not because of `FlowLayout` I think. You got that effect because you are setting new contentPane. Try workaround: just add directly your inner panel to your frame, without using new content pane. `add(wp)` – ferrerverck Dec 07 '13 at 12:15
  • @sage Hmmmm, ok this edited code is my SSCCE. If you compile you'll see the space between borders – croraf Dec 07 '13 at 12:15
  • @ferrerverck I think if I add directly the problem is solved, but... this means using BorderLayout, cause i'm then adding to default content pane which is using BorderLayout. You might say what's the problem then, the problem is that when using border layout in the original code i have the same problem, so i need the solution in FlowLayout. So your solution is the same as writing contentPane = new JPanel(new BorderLayout()); which i explicitly asked not to do:) – croraf Dec 07 '13 at 12:17
  • *"this means using BorderLayout"* No it doesn't `this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)));` or `this.getContentPane().add(contentPane);`. – Andrew Thompson Dec 07 '13 at 23:36
  • add(wp) - what ferrerverck suggested - is the same as getContentPane().add(wp), using default content pane, which means using BorderLayout – croraf Dec 08 '13 at 00:45

1 Answers1

0

I found the answer on another topic. You must write setResizable(false) BEFORE calling pack(). Haven't found the explanation why is this necessary, I assume that it's because pack() is adding some extra space for the frame. How to solve this if you don't want to make the window resizable I don't know. NOTE: You have the same problem with other LayoutManagers such as BorderLayout (the manager with pack adds extra space).

croraf
  • 4,332
  • 2
  • 31
  • 50
  • 1
    `I assume that it's because pack() is adding some extra space for the frame.` - No, pack() does not add extra space to the frame. The setResizable(false) method takes away space from the frame because it no longer needs to paint a resizable border. `How to solve this if you don't want to make the window unresizable I don't know` You already know the answer, you stated it at the beginning of your answer. – camickr Dec 07 '13 at 16:40
  • Oh, sorry, I meant when the window is resizable:D. Can you please explain further, what are resizable borders, I don't understand that? – croraf Dec 07 '13 at 16:50
  • 1
    When the window is resizable there is no problem. There is no extra space so I don't understand your comment. Also when the window is resizeable and you move your mouse over the edge of the window the cursor changes shape to indicate you can drag the edge. So the border around the widow is about 4 pixels bigger to support this. – camickr Dec 07 '13 at 16:53
  • Aha i didn't even realized that the problem does not occure when resizable is true:O. I still don't understand why the border needs to be wider to support resize? Do Windows GUI for example support the same thing? – croraf Dec 07 '13 at 16:56
  • Why would it make that space in my context when it does not make it when the window is resizable??? – croraf Dec 07 '13 at 17:03
  • 1
    There is no technical reason for this, it is just a UI design. I guess a wider border give a better visual clue to the user that the window can be resized. The size calculation is static. Some code is executed when you invoke the pack() method to determine the size of the frame and the size of the components added to the frame. If you then make the frame non-resizable the frame size is not adjust properly. Some people might consider this a bug. – camickr Dec 07 '13 at 17:04