My own BoxLayout woes

P

public boolean

In the below code, I have an instruction label and below it an input
area with a label and a JTextField. I want whichever is smaller to be
centered above or below the other.

The problem is that the Box.createHorizontalGlue() method call no longer
appears to work. This is very strange, since I haven't changed,
reinstalled, updated, or anything my JDK and it used to work.

But it does not, now, expand into the space to the left and right of the
surrounded items and cause them to be centered the way it did in all of
my previous projects involving Swing.

I also tried adding them to the workPanel at EAST and WEST without results.

Why won't the smaller item center itself? What has changed since previously?


JPanel instructions = new JPanel();
instructions.setLayout(new BoxLayout(instructions,
BoxLayout.LINE_AXIS));
JPanel insbox = new JPanel();
insbox.setLayout(new BoxLayout(insbox, BoxLayout.PAGE_AXIS));
JLabel label = new JLabel("<html><body><div align=\"center\">" +
"Lorem ipsum dolor sit amet, consectetuer<br/>adipiscing" +
" elit. Nullam mauris nunc, auctor sit amet,<br/>" +
"tincidunt nec, ultricies vitae, nulla. Donec<br/>" +
"pulvinar nulla vel nibh.<br/><br/>Aliquam imperdiet " +
" nunc quis arcu.<br/>Praesent leo. Nam dictum.</div>" +
"</body></html>");
insbox.add(label);
insbox.add(Box.createRigidArea(new Dimension(0,10)));
instructions.add(Box.createHorizontalGlue()); // Has no effect.
instructions.add(insbox);
instructions.add(Box.createHorizontalGlue()); // Has no effect.
workPanel.setLayout(new BorderLayout());
workPanel.add(instructions, BorderLayout.CENTER);
extLabelWP = new JLabel("Enter the foobar bazquux here:");
mtFieldWP = new JTextField();
JPanel fieldPanel = new JPanel();
workPanel.add(fieldPanel, BorderLayout.SOUTH);
fieldPanel.setLayout(new BoxLayout(fieldPanel,
BoxLayout.LINE_AXIS));
fieldPanel.add(Box.createHorizontalGlue()); // Has no effect.
fieldPanel.add(extLabelWP);
fieldPanel.add(Box.createRigidArea(DATA_HGAP_SPANNER));
fieldPanel.add(mtFieldWP);
fieldPanel.add(Box.createHorizontalGlue()); // Has no effect.
extLabelWP.setDisplayedMnemonic(KeyEvent.VK_E);
extLabelWP.setLabelFor(mtFieldWP);
 
P

public boolean

public said:
In the below code, I have an instruction label and below it an input
area with a label and a JTextField. I want whichever is smaller to be
centered above or below the other.

OT but technical question: Is it possible for a newsgroup post to appear
for the guy who posted it but not for anyone else? Because I think that
may have happened to me.
 
H

Harold \Curly\ Phillips

public boolean said:
In the below code, I have an instruction label and below it an input area
with a label and a JTextField. I want whichever is smaller to be centered
above or below the other.

The problem is that the Box.createHorizontalGlue() method call no longer
appears to work.

You've made an incorrect assumption. The method call succeeds in creating
horizontal glue - the method call "works". The problem in your program lies
elsewhere.
This is very strange, since I haven't changed, reinstalled, updated, or
anything my JDK and it used to work.

But it does not, now, expand into the space to the left and right of the
surrounded items and cause them to be centered the way it did in all of my
previous projects involving Swing.

I also tried adding them to the workPanel at EAST and WEST without
results.

You have failed to understand BorderLayout. It has special rules for how
available space is allocated to its central area.
Why won't the smaller item center itself? What has changed since
previously?

It does center itself. There's a difference between the size and postion of
the label and of it's text. The text is left aligned within the label's
canvas. Just because the html contains a "center" doesn't mean the text as a
whole is centered on the label. The label is centered within the central
area of the BorderLayout but it is also taking up all available space in the
center of the BorderLayout.
JPanel instructions = new JPanel();
instructions.setLayout(new BoxLayout(instructions,
BoxLayout.LINE_AXIS));
JPanel insbox = new JPanel();
insbox.setLayout(new BoxLayout(insbox, BoxLayout.PAGE_AXIS));
JLabel label = new JLabel("<html><body><div align=\"center\">" +
"Lorem ipsum dolor sit amet, consectetuer<br/>adipiscing" +
" elit. Nullam mauris nunc, auctor sit amet,<br/>" +
"tincidunt nec, ultricies vitae, nulla. Donec<br/>" +
"pulvinar nulla vel nibh.<br/><br/>Aliquam imperdiet " +
" nunc quis arcu.<br/>Praesent leo. Nam dictum.</div>" +
"</body></html>");

label.setBorder(new LineBorder(Color.BLUE)); // HP
label.setMaximumSize(label.getPreferredSize()); // HP
insbox.add(label);
insbox.add(Box.createRigidArea(new Dimension(0,10)));
instructions.add(Box.createHorizontalGlue()); // Has no effect.
instructions.add(insbox);
instructions.add(Box.createHorizontalGlue()); // Has no effect.
workPanel.setLayout(new BorderLayout());
workPanel.add(instructions, BorderLayout.CENTER);
extLabelWP = new JLabel("Enter the foobar bazquux here:");
mtFieldWP = new JTextField();
JPanel fieldPanel = new JPanel();
workPanel.add(fieldPanel, BorderLayout.SOUTH);
fieldPanel.setLayout(new BoxLayout(fieldPanel,
BoxLayout.LINE_AXIS));
fieldPanel.add(Box.createHorizontalGlue()); // Has no effect.
fieldPanel.add(extLabelWP);
fieldPanel.add(Box.createRigidArea(DATA_HGAP_SPANNER));
fieldPanel.add(mtFieldWP);
fieldPanel.add(Box.createHorizontalGlue()); // Has no effect.
extLabelWP.setDisplayedMnemonic(KeyEvent.VK_E);
extLabelWP.setLabelFor(mtFieldWP);

By adding two dozen lines Paul could have made this into an SSCCE.
 
P

public boolean

Harold said:
You've made an incorrect assumption.

Excuse me?

How rude!

I have, of course, done nothing of the sort.
You have failed

Excuse me?

How rude!

I have, of course, done nothing of the sort.
It does center itself.

It doesn't.

label.setBorder(new LineBorder(Color.BLUE)); // HP

Don't want a border.
label.setMaximumSize(label.getPreferredSize()); // HP

Shouldn't matter. I haven't needed this in similar situations
previously. Though it's not behaving as in similar situations previously...
By adding two dozen lines Paul could have made this into an SSCCE.

Who is Paul?
 
P

public boolean

John said:
Paul again. How boring.

Huh?

If you're talking about me, my name is John.

Regardless, your post appears to have nothing to do with anything else
in this thread, nor to do with Java.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top