public final class MnemonicUtils
extends java.lang.Object
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>a&b</html> | <html>a<u>b</u></html> | 'B' | -1 | 'b' is underlined |
| <html>R&D D&ep.</html> | <html>R&D D<u>e</u>p.</html> | 'B' | -1 | HTML & doesn't mark |
| Modifier and Type | Class and Description |
|---|---|
private static class |
MnemonicUtils.MnemonicText |
| Modifier and Type | Field and Description |
|---|---|
(package private) static char |
MNEMONIC_MARKER
The single mnemonic marker.
|
| Modifier | Constructor and Description |
|---|---|
private |
MnemonicUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
configure(javax.swing.AbstractButton target,
java.lang.String markedText)
Configures the text, mnemonic and mnemonic index for
target
using the given text that can be marked with the mnemonic marker '&'. |
static void |
configure(javax.swing.Action target,
java.lang.String markedText)
Configures the text, mnemonic and mnemonic index for
target
using the given text that can be marked with the mnemonic marker '&'. |
static void |
configure(javax.swing.JLabel target,
java.lang.String markedText)
Configures the text, mnemonic and mnemonic index for
target
using the given text that can be marked with the mnemonic marker '&'. |
private static void |
configure0(javax.swing.AbstractButton button,
MnemonicUtils.MnemonicText mnemonicText) |
private static void |
configure0(javax.swing.Action action,
MnemonicUtils.MnemonicText mnemonicText) |
private static void |
configure0(javax.swing.JLabel label,
MnemonicUtils.MnemonicText mnemonicText) |
(package private) static int |
mnemonic(java.lang.String markedText)
Extracts and returns the mnemonic from the given marked text.
|
(package private) static int |
mnemonicIndex(java.lang.String markedText)
Finds and returns the index of the mnemonic in the marked text.
|
static java.lang.String |
plainText(java.lang.String markedText)
Returns the plain text for the given marked text by removing
the mnemonic marker and marker quotes - if any.
|
static final char MNEMONIC_MARKER
public static void configure(javax.swing.AbstractButton target,
java.lang.String markedText)
target
using the given text that can be marked with the mnemonic marker '&'.
For example if markedText is "Save &as", the text
will be set to "Save as", the mnemonic is 'a', and the
mnemonic index is 5.target - the button to be configuredmarkedText - the text with optional mnemonic markerjava.lang.NullPointerException - if target is nullpublic static void configure(javax.swing.Action target,
java.lang.String markedText)
target
using the given text that can be marked with the mnemonic marker '&'.
For example if markedText is "Save &as", the text
will be set to "Save as", the mnemonic is 'a', and the
mnemonic index is 5.target - the Action to be configuredmarkedText - the text with optional mnemonic markerjava.lang.NullPointerException - if target is nullpublic static void configure(javax.swing.JLabel target,
java.lang.String markedText)
target
using the given text that can be marked with the mnemonic marker '&'.
For example if markedText is "Save &as", the text
will be set to "Save as", the mnemonic is 'a', and the
mnemonic index is 5.target - the label to be configuredmarkedText - the text with optional mnemonic markerjava.lang.NullPointerException - if target is nullpublic static java.lang.String plainText(java.lang.String markedText)
MnemonicUtils class comment for information
about how to mark a mnemonic and how to quote a marker.
MnemonicUtils.plainText("Save") == "Save"
MnemonicUtils.plainText("&Save") == "Save"
MnemonicUtils.plainText("&Look&Feel") == "Look&Feel"
MnemonicUtils.plainText("Look & Feel") == "Look & Feel"
MnemonicUtils.plainText("R&&D D&epartment") == "R&D Department"
MnemonicUtils.plainText("<html>a&b</html>") == "<html>a<u>b</u></html>"
markedText - the text that may contain a mnemonic markerstatic int mnemonic(java.lang.String markedText)
markedText - the text with optional mnemonic marker0 if no mnemonic is markedstatic int mnemonicIndex(java.lang.String markedText)
markedText - the text with optional mnemonic marker-1 if there's no mnemonic
or the text is HTMLprivate static void configure0(javax.swing.AbstractButton button,
MnemonicUtils.MnemonicText mnemonicText)
private static void configure0(javax.swing.Action action,
MnemonicUtils.MnemonicText mnemonicText)
private static void configure0(javax.swing.JLabel label,
MnemonicUtils.MnemonicText mnemonicText)