vertical BoxLayout, JLabel, and JButton and alignment

D

Duane Evenson

If a button is in a jPanel with vertical BoxLayout, as soon as a label is
added to the panel, the button aligns itself with the left edge of the
label. Also the label doesn't align itself with the left edge of the
panel--it's indented an amount.

Why?

I want the button to stay put in the center after a label is added to the
panel. I also want the labels to start at the left edge of the panel.
 
D

Duane Evenson

If a button is in a jPanel with vertical BoxLayout, as soon as a label is
added to the panel, the button aligns itself with the left edge of the
label. Also the label doesn't align itself with the left edge of the
panel--it's indented an amount.

Why?

I want the button to stay put in the center after a label is added to the
panel. I also want the labels to start at the left edge of the panel.

Sorry, missing info...
The panel also needs a JComboBox for the components to act wierd, although
the box itself doesn't move.
 
R

RedGrittyBrick

Andrew said:
Duane Evenson wrote:
..

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!
 
R

RedGrittyBrick

RedGrittyBrick said:
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);

Oops, whilst boggling at the JComboBox I failed to notice I hadn't
really addressed the OP's problem

The Op might enjoy reading
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html#features

I suspect the OP wants something more like
----------------------------8<-------------------------------------
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
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() {

// smallest components
JComboBox box = new JComboBox(new String[]
{"apples", "pears", "oranges"});
JButton button = new JButton("Button");
JLabel label1 = new JLabel("label 1");
JLabel label2 = new JLabel("label 2");

// collect all but button into a left aligned container
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.PAGE_AXIS));
for (JComponent c: new JComponent[] {box, label1, label2}) {
c.setAlignmentX(JComponent.LEFT_ALIGNMENT);
p1.add(c);
}

// assemble container and button into a centered container
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
for (JComponent c: new JComponent[] {p1, button}) {
c.setAlignmentX(JComponent.CENTER_ALIGNMENT);
p.add(c);
}

// add outer container to a frame
JFrame f = new JFrame("Vertical Layout Oddity");
f.add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
-------------------------------8<---------------------------------------

I still haven't worked out why the JComboBox has an inordinate
propensity for growth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top