开发者

Controlling JProgressBar height

开发者 https://www.devze.com 2023-03-09 03:58 出处:网络
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 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.clien开发者_StackOverflowtTask = 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);
    }

}


  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.


Layouts generally honor the preferred size of a component.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号