Set the JProgressBar width to a fixed size
I have it JProgressBar
on mine JToolBar
. The problem is that it JProgressBar
takes up all the remaining space on JToolBar
instead of a fixed width.
I've tried using:
Dimension prefSize = goButton.getPreferredSize();
prefSize.width = 100;//some width
progressBar.setPreferredSize(prefSize);
progressBar.setVisible(true);
No success. JProgressBar
saves all the remaining space.
How can I get it to JProgressBar
have a fixed width?
Here's a small example of my problem:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JToolBar;
public class SimpleToolbar extends JFrame {
class ToolBar extends JToolBar {
private final JLabel labelSomething;
private final JLabel labelAnything;
private final JButton goButton;
private final JButton cancelButton;
private final JButton pauseButton;
private final JProgressBar progressBar;
public ToolBar()
{
setBorder(BorderFactory.createEtchedBorder());
setFloatable(false);
goButton = new JButton("Go");
cancelButton = new JButton("Cancel");
pauseButton = new JButton("Pause");
labelAnything = new JLabel("Anything");
labelSomething = new JLabel("Something");
progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
Dimension prefSize = goButton.getPreferredSize();
prefSize.width = 100;//some width
progressBar.setPreferredSize(prefSize);
progressBar.setVisible(true);
add(goButton);
add(cancelButton);
add(pauseButton);
addSeparator();
add(labelAnything);
add(labelSomething);
addSeparator();
add(progressBar);
}
}
protected ToolBar toolBar;
public SimpleToolbar() {
super();
setSize(600, 350);
toolBar = new ToolBar();
getContentPane().add(toolBar, BorderLayout.NORTH);
setVisible(true);
}
public static void main(String[] a) {
new SimpleToolbar();
}
}
+3
source to share