Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

The XHTML label is highly flexible, with the
theoretical ability to appear anywhere within the same form as its associated form control:

...

Code Block
/* Bind a TextField to the input element: */
TextField somethingField = new TextField("something");
somethingField.setOutputMarkupId(true);
/* This could equally pull a label out of a resource. */
somethingField.setLabel(new Model("Something or other: "));

/* Bind a FormComponentLabel component to the label element: 
 * This will automatically update the "for" attribute to match the id of somethingField
 */
FormComponentLabel somethingLabel = new FormComponentLabel("somethingLabel",somethingField);

/* Add the field to the containing label.
 * This will automatically update the "for" attribute to match:
 */
somethingLabel.add(somethingField);
Label somethingLabelSpan = new Label("somethingLabelSpan",somethingField.getLabel());

/* Get rid of the span tag, leaving the label only: */
somethingLabelSpan.setRenderBodyOnly(true);

/* Add the label span to the containing label: */
somethingLabel.add(somethingLabelSpan);

/* Finally, add the containing label to the form: */
this.add(somethingLabel);