I am trying to build a component similar to a JProgressBar which would show three states instead of two as shown here .
I have built this by adding three JLabel components(with three different background colors) to a panel with layout set as GridBagLayout. I have assigned corresponding weights to these labels, so that they get resized according to the size of the parent panel. Below is the corresponding sample code:
public class CustomProgressBar {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
double firstLabelWeight = 0.10d;
double secondLabelWeight = 0.20d;
double thirdLabelWeight = 1d - firstLabelWeight - secondLabelWeight;
JLabel firstLabel = new JLabel();
firstLabel.setOpaque(true);
firstLabel.setBackground(Color.GREEN);
JLabel secondLabel = new JLabel();
secondLabel.setOpaque(true);
secondLabel.setBackground(Color.YELLOW);
JLabel thirdLabel = new JLabel();
thirdLabel.setOpaque(true);
thirdLabel.setBackground(Color.RED);
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = firstLabelWeight;
gbc.weighty = 1d;
panel.add(firstLabel, gbc);
gbc.weightx = secondLabelWeight;
gbc.weighty = 1d;
panel.add(secondLabel, gbc);
gbc.weightx = thirdLabelWeight;
gbc.weighty = 1d;
panel.add(thirdLabel, gbc);
panel.setBackground(Color.BLACK);
panel.setPreferredSize(new Dimension(157, 50));
frame.add(panel);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Now the problem with this approach is that when I try to resize the JFrame, the above three labels are not filling the complete horizontal space in the parent panel and we see black strips to the ends of the panel as shown here .
I have tried setting gbc.fill and gbc.anchor parameters as well, but it still doesn't work.
Any help with this problem is greatly appreciated! Thankyou :)
In order to overcome the rounding problems, you could override the layout for the panel in this way:
class MyLayout extends GridBagLayout
{
private Component owner;
public MyLayout(Component owner)
{
this.owner = owner;
}
@Override
protected void adjustForGravity(GridBagConstraints constraints,
Rectangle rect)
{
// Adjust position and width of first (GREEN) label if necessary
if ((rect.x > 0) && (rect.x <= 2))
{
rect.width += rect.x;
rect.x = 0;
}
// Adjust width of last (RED) label if necessary
int gap = owner.getWidth() - rect.x - rect.width;
if ((gap > 0) && (gap <= 2))
rect.width += gap;
}
} // class MyLayout
Then of course, set the layout of the panel in this way:
panel.selLayout(new MyLayout(panel));
If you construct the GridBagConstraints
providing negative Insets
, this will not show the underlying Panel
. The change will be the following
GridBagConstraints gbc = new GridBagConstraints(-1, -1, 1, 1, 0, 0, 10, 0, new Insets(-1, -1, -1, -1), 0, 0);
Updated Example:
public class CustomProgressBar {
public static void main(String[] args) {
JFrame frame = new JFrame("test");
JPanel panel = new JPanel();
double firstLabelWeight = 0.10d;
double secondLabelWeight = 0.20d;
double thirdLabelWeight = 1d - firstLabelWeight - secondLabelWeight;
JLabel firstLabel = new JLabel();
firstLabel.setOpaque(true);
firstLabel.setBackground(Color.GREEN);
JLabel secondLabel = new JLabel();
secondLabel.setOpaque(true);
secondLabel.setBackground(Color.YELLOW);
JLabel thirdLabel = new JLabel();
thirdLabel.setOpaque(true);
thirdLabel.setBackground(Color.RED);
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(-1, -1, 1, 1, 0, 0, 10, 0, new Insets(-1, -1, -1, -1), 0, 0);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = firstLabelWeight;
gbc.weighty = 1d;
panel.add(firstLabel, gbc);
gbc.weightx = secondLabelWeight;
gbc.weighty = 1d;
panel.add(secondLabel, gbc);
gbc.weightx = thirdLabelWeight;
gbc.weighty = 1d;
panel.add(thirdLabel, gbc);
panel.setBackground(Color.BLACK);
panel.setPreferredSize(new Dimension(157, 50));
frame.add(panel);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.