1

I am creating a java swing app which makes use of the JProgressBar control. The control looks ok on Linux and Windows but it is too big for my likings on Mac. I would like to change its height.

I am constructing the entire layout via boxes, i.e. createHorizontalBox, etc. I understand that whatever I put in the box it should take its entire space.

Here is my code (it is ugly):

    this.iconLabel = new JLabel();
    this.nameLabel = new JLabel();
    this.statusProgressbar = new JProgressBar();
    this.pausePush = new PushButton();
    this.resumePush = new PushButton();
    this.actionPush = new PushButton();
    this.statusLabel = new JLabel("...");
    this.clientTask = null;

    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    this.setOpaque(false);
    this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    this.add(this.iconLabel);

    Box container = Box.createVerticalBox();
    Box box = Box.createHorizontalBox();

    box.setOpaque(false);
    box.add(this.nameLabel);
    box.add(Box.createHorizontalGlue());

    container.add(box);

    this.pausePush.setVisible(false);
    this.resumePush.setVisible(false);
    this.actionPush.setVisible(false);

    box = Box.createHorizontalBox();

    box.setOpaque(false);
    box.add(this.statusProgressbar);
    box.add(this.pausePush);
    box.add(this.resumePush);
    box.add(this.actionPush);

    container.add(box);

    this.nameLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 12));
    this.statusLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 9));

    box = Box.createHorizontalBox();

    box.setOpaque(false);
    box.add(this.statusLabel);
    box.add(Box.createHorizontalGlue());

    container.add(box);

    this.add(container);

I am struggling to find the right way to layout all the components such as the JProgressBar height can be controlled.

Can you help?

UPDATE: Here is a small program which demonstrates the problem

package test;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        frame.setSize(500, 500);

        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setOpaque(false);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel nameLabel = new JLabel("...");
        JProgressBar statusProgressbar = new JProgressBar();
        JLabel statusLabel = new JLabel("...");

        Box container = Box.createVerticalBox();
        Box box = Box.createHorizontalBox();

        box.setOpaque(false);
        box.add(nameLabel);
        box.add(Box.createHorizontalGlue());

        container.add(box);

        statusProgressbar.setPreferredSize(new Dimension(0, 5)); // NOT WORKING

        box = Box.createHorizontalBox();

        box.setOpaque(false);
        box.add(statusProgressbar);

        container.add(box);

        nameLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 9));
        statusLabel.setFont(new Font(UIManager.getFont("TabbedPane.font").getFamily(), 0, 9));

        box = Box.createHorizontalBox();

        box.setOpaque(false);
        box.add(statusLabel);
        box.add(Box.createHorizontalGlue());

        container.add(box);

        panel.add(container);

        frame.add(panel);
        frame.setVisible(true);
    }

}
Pass
  • 1,501
  • 4
  • 21
  • 39

2 Answers2

4
  1. A Box layout will attempt to stretch a component to its maximum size if space is available. So make sure that the preferred height and maximum height are the same values so it doesn't get stretched.

  2. The size of the progress bar may be determined by the LAF. The Metal LAF uses "ProgressBar.horizontalSize". Check out the UIManager Defaults and you might be able to customize it for the Mac.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
camickr
  • 321,443
  • 19
  • 166
  • 288
  • this.statusProgressbar.setPreferredSize(statusProgressbarDimension); this.statusProgressbar.setMaximumSize(statusProgressbarDimension); // Did the trick when I tested it individually in the test program. However, the GUI gets a bit screwed up when I try to update the progress bar. – Pass May 28 '11 at 18:12
  • 1
    @Pass, the proper solution would be to override the getMaximumSize() method to return the proper value which would be the maximum width along with the preferred height. – camickr May 28 '11 at 18:34
2

Layouts generally honor the preferred size of a component.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    @Pass: we've no way of knowing what works or what doesn't for you since we can't compile and run your posted code. As suggested above, consider creating and posting a small compilable and runnable example, an [SSCCE](http://SSCCE.org) – Hovercraft Full Of Eels May 28 '11 at 17:40