Class MnemonicUtils

java.lang.Object
com.jgoodies.common.swing.MnemonicUtils

public final class MnemonicUtils extends Object
Configures the text, mnemonic and mnemonic index in Actions, JLabels, and AbstractButtons. The term marked text is used for Strings that contain a marker character that marks both the mnemonic and its index in the resulting plain text.

To set a mnemonic, add an ampersand ('&') before the character that should be the mnemonic, for example "&Save". If your text has multiple ampersands, only the first one is used to mark the mnemonic; the other ampersands appear as normal text. For example "&Look&Feel" has the first 'L' has mnemonic. If you want to use the ampersand itself in the text, use two consecutive ampersands for any ampersand that precede the one that is used to mark the mnemonic. For example the marked text "R&&D D&epartment" has "R&D Department" as text and the mnemonic is 'E'.

Examples:

Marked Text Plain Text Mnemonic Mnemonic Index Comment
Save Save 0 -1 No mnemonic
&Save Save 'S' 0  
Save &as Save as 'A' 5 Second 'a' marked
Look&Feel LookFeel 'F' 4 & should be quoted
Look&&Feel Look&Feel 0 -1 & is quoted
&Look&Feel Look&Feel L 0 Second & needs no quote
Look & Feel Look & Feel 0 -1 Whitespace cannot be marked
R&&D D&epartment R&D Department 'E' 5 First & is quoted
<html>ainvalid input: '&b'</html> <html>a<u>b</u></html> 'B' -1 'b' is underlined
<html>R&amp;D Dinvalid input: '&ep'.</html> <html>R&amp;D D<u>e</u>p.</html> 'B' -1 HTML &amp; doesn't mark
  • Field Details

    • MNEMONIC_MARKER

      static final char MNEMONIC_MARKER
      The single mnemonic marker. Future versions of this class shall support other markers, for example the underline char ('_').
      See Also:
  • Constructor Details

    • MnemonicUtils

      private MnemonicUtils()
  • Method Details

    • configure

      public static void configure(AbstractButton target, String markedText)
      Configures the text, mnemonic and mnemonic index for target using the given text that can be marked with the mnemonic marker 'invalid input: '&amp''. For example if markedText is "Save invalid input: '&as'", the text will be set to "Save as", the mnemonic is 'a', and the mnemonic index is 5.
      Parameters:
      target - the button to be configured
      markedText - the text with optional mnemonic marker
      Throws:
      NullPointerException - if target is null
    • configure

      public static void configure(Action target, String markedText)
      Configures the text, mnemonic and mnemonic index for target using the given text that can be marked with the mnemonic marker 'invalid input: '&amp''. For example if markedText is "Save invalid input: '&as'", the text will be set to "Save as", the mnemonic is 'a', and the mnemonic index is 5.
      Parameters:
      target - the Action to be configured
      markedText - the text with optional mnemonic marker
      Throws:
      NullPointerException - if target is null
    • configure

      public static void configure(JLabel target, String markedText)
      Configures the text, mnemonic and mnemonic index for target using the given text that can be marked with the mnemonic marker 'invalid input: '&amp''. For example if markedText is "Save invalid input: '&as'", the text will be set to "Save as", the mnemonic is 'a', and the mnemonic index is 5.
      Parameters:
      target - the label to be configured
      markedText - the text with optional mnemonic marker
      Throws:
      NullPointerException - if target is null
    • plainText

      public static String plainText(String markedText)
      Returns the plain text for the given marked text by removing the mnemonic marker and marker quotes - if any. If the marked text is HTML, the plain text has the mnemonic character underlined. See the MnemonicUtils class comment for information about how to mark a mnemonic and how to quote a marker.
       MnemonicUtils.plainText("Save")             == "Save"
       MnemonicUtils.plainText("invalid input: '&Save'")            == "Save"
       MnemonicUtils.plainText("invalid input: '&Look'invalid input: '&Feel'")       == "Lookinvalid input: '&Feel'"
       MnemonicUtils.plainText("Look invalid input: '&' Feel")      == "Look invalid input: '&' Feel"
       MnemonicUtils.plainText("Rinvalid input: '&'invalid input: '&D' Dinvalid input: '&epartment'") == "Rinvalid input: '&D' Department"
       MnemonicUtils.plainText("<html>ainvalid input: '&b'</html>") == "<html>a<u>b</u></html>"
       
      Parameters:
      markedText - the text that may contain a mnemonic marker
      Returns:
      the text without mnemonic marker and marker quotes
    • mnemonic

      static int mnemonic(String markedText)
      Extracts and returns the mnemonic from the given marked text.
      Parameters:
      markedText - the text with optional mnemonic marker
      Returns:
      the mnemonic or 0 if no mnemonic is marked
    • mnemonicIndex

      static int mnemonicIndex(String markedText)
      Finds and returns the index of the mnemonic in the marked text.
      Parameters:
      markedText - the text with optional mnemonic marker
      Returns:
      the mnemonic index or -1 if there's no mnemonic or the text is HTML
    • configure0

      private static void configure0(AbstractButton button, MnemonicUtils.MnemonicText mnemonicText)
    • configure0

      private static void configure0(Action action, MnemonicUtils.MnemonicText mnemonicText)
    • configure0

      private static void configure0(JLabel label, MnemonicUtils.MnemonicText mnemonicText)