I have a panel that consists of a button (X), a label (Y), and a progress bar (Z). Ideally I would like to lay them out like so:
| X-X Y---------------Y Z---Z ============= | <-- expanded-size panel
^ extra space
| X-X Y------Y Z---Z | <-- reduced-size panel
The diagram above shows:
- When the panel expands, extra space should go to the label (Y) so that the label can completely show its text.
- The progress bar (Z) should always remain next to the label (Y).
- When the panel is reduced, the label (Y)'s size should be reduced.
- The button (X) and the progress bar (Z)'s sizes should be constant, not the label (Y)'s size.
However, when I try using GroupLayout, this is what happens when the panel is expanded:
| X-X Y---------------Y ============= Z---Z | <-- expanded-size panel (bad)
The problem is that when the panel has extra space, the label (Y) gets expanded beyond what it needs, which pushes the progress bar (Z) to the right. I would prefer that the progress bar's (Z) position is next to the label (Y).
How can I accomplish this layout?
Example code ("Blah.java"):
import java.awt.*;
import javax.swing.*;
public class Blah extends JPanel {
public Blah() {
final JButton X = new JButton("X");
final JLabel Y = new JLabel("yyyyyyyyyyy");
Y.setOpaque(true);
Y.setBackground(Color.YELLOW);
final JProgressBar Z = new JProgressBar();
Z.setIndeterminate(true);
final GroupLayout l = new GroupLayout(this);
super.setLayout(l);
l.setHorizontalGroup(
l.createSequentialGroup()
.addComponent(X, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(Y, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Z, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
l.setVerticalGroup(
l.createParallelGroup()
.addComponent(X)
.addComponent(Y)
.addComponent(Z));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame("Blah");
frame.add(new Blah());
frame.pack();
frame.setVisible(true);
}
});
}
}