1

OK, several questions about the test code below... I don't have the full working program to post, but I'm hoping people know Swing well enough to take a stab at it. This is a JLabel inside of a JPanel(BoxLayout), and I'm working on the sizing of the label in the lower right.

What I get with the code as shown is a status box 300 width by 30 height. I have fiddled with the preferred size and the label minimum size, and it does not seem to behave in any rational way.

  1. Why does the JPanel Preferred Size affect the height but not the width? If I change the x dimension in setPreferredSize() to 0 or 500, it still comes out 300 from the label.
  2. Why does the JLabel Minimum Size affect the width but not the height? If I comment the setPreferredSize() call and increase the label height to 30, nothing happens.
  3. I started out with JPanel setMinimumSize (commented), but it no longer has any effect - why does the JPanel require setPreferredSize()?
  4. If I change the label text from "" to " ", this increases the height of the label. Since the label is not controlling the height here, why does this have any effect at all?

By the way, the createRigidArea() call is to force the separator to the right, rather than sticking to the left hand side of the screen. If there are any less kludgy ideas for this, I'd be grateful.

    private JComponent makeStatusBarTest() {
    JPanel statusPanel = new JPanel();
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.LINE_AXIS));
    statusPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
//  statusPanel.setMinimumSize(new Dimension(0, 30));
    statusPanel.setPreferredSize(new Dimension(500, 30));
    JLabel statusLabel = new JLabel();
    Border emptyBorder = BorderFactory.createEmptyBorder(5, 10, 5, 10);
    statusLabel.setBorder(emptyBorder);
    statusLabel.setText("");
    statusLabel.setMinimumSize(new Dimension(300, 20));
    statusPanel.add(statusLabel);
    statusPanel.add(new JSeparator(SwingConstants.VERTICAL));
    statusPanel.add(Box.createRigidArea(new Dimension(5000,0)));

    return statusPanel;
}
orbfish
  • 7,381
  • 14
  • 58
  • 75
  • 1
    Don't show us "the full program", but on the same token posting a *small* program that compiles and runs and demonstrates the problem, an [sscce](http://sscce.org) will help us be much better able to help you. – Hovercraft Full Of Eels Feb 21 '12 at 19:02
  • 3
    @orbfish please what's is new `Dimension(5000,0)));` how much (I can count three) monitors you want to painted and with zero height – mKorbel Feb 21 '12 at 19:04
  • @HovercraftFullOfEels - there seems to be a new sscce religion, people keep linking to it. I said I was not able to post a working program. – orbfish Feb 28 '12 at 18:33
  • @orbfish: I wouldn't call it a "religion", and no one (and certainly not I) is requiring you or anyone to do this (especially since you've gotten an acceptable answer), but my experience here has been that for complex problems, especially Swing layout problems, it's a heck of a lot easier for us to sort them out if the original poster can create and post an SSCCE. – Hovercraft Full Of Eels Feb 28 '12 at 21:06
  • So my feeling is, why not request it? It's not meant as an insult or personal affront and shouldn't be taken that way, and it does increase the chances of getting a decent and quick solution. But if you don't want to do it, I for one will still sleep easy at night. – Hovercraft Full Of Eels Feb 28 '12 at 21:06
  • @orbfish: OK, I re-worded my SSCCE request to better reflect these sentiments here: [small-gaps-appear-when-combining-arcs-and-rectangles](http://stackoverflow.com/questions/9491389/small-gaps-appear-when-combining-arcs-and-rectangles). Better? – Hovercraft Full Of Eels Feb 28 '12 at 23:52

1 Answers1

2

I can explain #1 and #2:

From the BoxLayout javadocs: "BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout)."

In other words, BoxLayout uses the internal components (in your case, statusLabel) to decide the widths, but the JPanel itself (within reason) to decide the heights.

You can usually use Glue instead of RigidArea to move stuff around, but I agree that it takes some getting used to.

#4 is Swing being too efficient - if the JLabel is empty the text rectangle is 0x0. Ultimately determined in SwingUtilities.layoutCompoundLabelImpl().

I think #3 is because BoxLayout is trying to respect the preferred size of the internal components. Since setMinimumSize, arguably, overrides their preferred sizes.

user949300
  • 15,364
  • 7
  • 35
  • 66
  • Thank you for the answers. So, the default horizontal layout of BoxLayout is delegating its sizing to internal components. I tried Glue by the way, had no effect, must be missing something. Still don't get PreferredSize vs. MinimumSize, but this seems to be component-by-component or even layout-by-layout, so the user can despair on ever figuring this out. – orbfish Feb 28 '12 at 18:57
  • If you set a minimumSize, and the user then resizes the panel to that minimumSize, it may no longer be possible for the Panel to "honor" the preferred width of the components. That's how I think about it. But I'm not sure what is really going on inside... :-) – user949300 Feb 28 '12 at 21:14