A new property "displayedMnemonicIndex" is introduced
in classes AbstractButton and JLabel. It tells which
character to underline. So for example to underline
the capital 'A' in 'Save As', one should call:
A new method, BasicGraphicsUtils.drawStringUnderlineChar(),
is also needed. It takes an int parameter telling which
character to underline. Unfortunately we can't call this
method drawString(), because such a method with the same
signature already exists.
New methods added:
The diffs will be attached, as there are too many to list them here.
Please see attached fix.zip file.
Agreed. When looking for the character to underline it should first do a case sensitive search, then case insensitive.
The problem is that mnemonic character is underlined by the
BasicGraphicsUtils.drawString() method, which performs case-
insensitive search. Since this is a public method, we can't
simply change it to performs case-sensitive search.
As this is an often requested feature, the following will be added to JLabel and AbstractButton:
* Provides a hint to the look and feel as to which character in the
* text should be decorated to represent the mnemonic. Not all look and
* feels may support this. A value of -1 indicates either there is no
* mnemonic, the mnemonic character is not contained in the string, or
* the developer does not wish the mnemonic to be displayed.
* The value of this is updated as the properties relating to the
* mnemonic change (such as the mnemonic itself, the text...).
* You should only ever have to call this if
* you do not wish the default character to be underlined. For example, if
* the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A'
* to be decorated, as 'Save <u>A</u>s', you would have to invoke
* <code>setDisplayedMnemonicIndex(5)</code> after invoking
* @since 1.4
* @param index Index into the String to underline
* @exception IllegalArgumentException will be thrown if <code>index</code>
* is >= length of the text, or < -1
* bound: true
* attribute: visualUpdate true
* description: the index into the String to draw the keyboard character
* mnemonic at
public void setDisplayedMnemonicIndex(int index)
* Returns the character, as an index, that the look and feel should
* provide decoration for as representing the mnemonic character.
* @since 1.4
* @return index representing mnemonic character
* @see #setDisplayedMnemonicIndex
public int getDisplayedMnemonicIndex();
Add the following to javax.swing.plaf.basic.BasicGraphicsUtils:
* Draw a string with the graphics <code>g</code> at location
* (<code>x</code>, <code>y</code>)
* just like <code>g.drawString</code> would.
* The character at index <code>underlinedIndex</code>
* in text will be underlined. If <code>index</code> is beyond the
* bounds of <code>text</code> (including < 0), nothing will be
* @param g Graphics to draw with
* @param text String to draw
* @param underlinedIndex Index of character in text to underline
* @param x x coordinate to draw at
* @param y y coordinate to draw at
* @since 1.4
public static void drawStringUnderlineCharAt(Graphics g, String text,
int underlinedIndex, int x, int y);