0

Sometimes I need the label for a checkbox to be to the left of the checkbox not the right so instead of using

JCheckBox checkbox = new JCheckBox("label",false);

I do:

JCheckBox checkbox = new JCheckBox("",false);
JLabel    label    = new JLabel("label");
GroupLayout.ParallelGroup vp1 = layout.createBaselineGroup(false, false);
vp1.addComponent(checkbox);
vp1.addComponent(label);

(I am using GroupLayout) but they are not vertically aligned correctly, I've also tried

 =layout.createParallelGroup(GroupLayout.Alignment.CENTER);

which doesn't look bad but still appears different to using a single checkbox and various other options, is it possible to get the same alignment ?

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 2
    Why not use [setHorizontalTextPosition](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setHorizontalTextPosition(int))? – MadProgrammer Aug 28 '12 at 09:25
  • I agree with @MadProgrammmer, this is the responsability of setHorizontalTextPosition – gontard Aug 28 '12 at 09:42
  • my 2cents and probably not an option - if you have the slightest chance: go for an external LayoutManager – kleopatra Aug 28 '12 at 09:50
  • @MadProgrammer, brilliant didn't realize this method existed, yes that is what I should and will use. Although still (slightly) interested if this can be done with my incorrect method. – Paul Taylor Aug 28 '12 at 11:57
  • @PaulTaylor I'd follow kleopatra's advice on that. Yes it can be done. I'd drop the label and checkbox in a pane of there own and use something like `GridBagLayout`, but that might be over kill in this case, `FlowLayout` would work as well – MadProgrammer Aug 29 '12 at 03:47
  • I dont understand what kleopatra is meaning by 'external' LayoutManager – Paul Taylor Aug 29 '12 at 08:02

1 Answers1

1

you may use the function setHorizontalTextPosition(int textPosition) with the int value SwingConstants.RIGHT etc

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
  • Found a problem if you have a checkbox, and then below have something else like a Jlabel (for an entry field) and you set alignnment to LEFT then the start of the text for the checkbox is not aligned up with the text of the JLabel. With a default checkobox that has the label to the right the checkbox is not lined up either (its indented slightly) but that looks okay, however it looks funny when it is text that is not lined up with text. – Paul Taylor Aug 30 '12 at 09:41