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);
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);