Class HtmlLabelField

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.HtmlLabelField

public class HtmlLabelField extends FormField
The HtmlLabelField can be used to convert part of a label into a hyperlink, or which can be used to embed multiple separate hyperlinks within the same label. This is a more advanced version of the standard LabelField, which only supports a single hyperlink for the entire label.

USAGE: To use the HtmlLabelField, you supply an html-formatted label text, and a custom Action which will be triggered when the hyperlink is clicked. the command which is supplied to the Action will match the href value specified in the html label text. For example:

 final String html = "<html>Would you like to "
                     + "<a href='proceed'>proceed</a>"
                     + " or <a href='cancel'>cancel</a>?</html>";
 HtmlLabelField labelField = new HtmlLabelField(html, myCustomAction);
 

The Action that you supply will receive an actionPerformed() call when the link is clicked, and the action command will be "proceed" or "cancel" in this example. Here's what the Action might look like:

     Action myCustomAction = new AbstractAction() {
         @Override
         public void actionPerformed(ActionEvent e) {
             String command = e.getActionCommand();
             if ("proceed".equals(command)) {
                 // Do whatever you need to do when the proceed link is clicked
             }
             else if ("cancel".equals(command)) {
                 // Do whatever you need to do when the cancel link is clicked
             }
         }
     };
 
Since:
swing-extras 2.6
Author:
scorbo2
  • Constructor Details

    • HtmlLabelField

      public HtmlLabelField(String labelText, Action linkAction)
      For creating a HtmlLabelField with no field label. The supplied linkAction will receive an actionPerformed() when any link is clicked, and the action command will be the href value of the clicked link. If the supplied linkAction is null, no action will be performed when links are clicked.
    • HtmlLabelField

      public HtmlLabelField(String fieldLabelText, String labelText, Action linkAction)
      For creating a HtmlLabelField with a field label. The supplied linkAction will receive an actionPerformed() when any link is clicked, and the action command will be the href value of the clicked link. If the supplied linkAction is null, no action will be performed when links are clicked.
  • Method Details

    • hasValidationLabel

      public boolean hasValidationLabel()
      Overridden here as we generally don't want to show a validation label on a label. Will return true only if one or more FieldValidators have been explicitly assigned. Yes! You can assign a FieldValidator to a HtmlLabelField if you really want to, and it will perform validation if so. This generally makes no sense, as HtmlLabelFields do not allow user input, and so validation is disabled by default.
      Overrides:
      hasValidationLabel in class FormField
    • getText

      public String getText()
      Returns the current label text in html format.
      Returns:
      The text of the label in html format.
    • getLinkAction

      public Action getLinkAction()
      Returns the Action that is triggered when links are clicked.
    • setText

      public HtmlLabelField setText(String text, Action action)
      Sets new label text in html format, and wires it up with the given Action. This will replace any previously assigned Action and label text.
      Parameters:
      text - The new label text in html format.
      action - The Action to trigger when links are clicked.
    • setFont

      public HtmlLabelField setFont(Font font)
      Sets the font to use for the label text. This is shorthand for ((JEditorPane)getFieldComponent()).setFont()
      Parameters:
      font - The new Font to use.
    • getFont

      public Font getFont()
      Returns the current Font used for the label text.