Re: vertical BoxLayout, JLabel, and JButton and alignment
Andrew Thompson wrote:
Duane Evenson wrote:
..
Sorry, missing info...
Don't be sorry, be smart. Post an SSCCE.
<http://www.physci.org/codes/sscce.html>
I'm not the OP, Duane, but I think he means this:
Compile, run, resize it.
---------------------8<-------------------------------------
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class VerticalOddity {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new VerticalOddity();
}
});
}
VerticalOddity() {
String[] list = {"apples", "pears", "oranges"};
JComboBox box = new JComboBox(list);
JButton button = new JButton("Button");
JLabel label1 = new JLabel("label 1");
JLabel label2 = new JLabel("label 2");
// X marks the spot
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
p.add(box);
p.add(button);
p.add(label1);
p.add(label2);
JFrame f = new JFrame("Vertical Layout Oddity");
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
------------------------------8<------------------------------------------
Part of the answer is to add the following at spot "X" above
JComponent[] cs = new JComponent[] {box,button,label1,label2};
for (JComponent c: cs)
c.setAlignmentX(Component.LEFT_ALIGNMENT);
However I'm a little intriuged at the eagerness of JComboBox to expand!