Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4138746
Votes 3
Synopsis SetMnemonic should be case-sensitive
Category java:classes_swing
Reported Against 1.2 , 1.1.8 , 1.2.1 , 1.2beta3
Release Fixed 1.4(merlin-beta)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 4248272 , 4249401
Submit Date 14-MAY-1998
Description
In the entries: Mark Done, Mark ToDo, Mark Rejected,
setting mnemonics to D, T, and R, respectivly produce:
   Mark Done
        -
   Mark ToDo
        -
   Mark Rejected
     -
The specified mnemonic was upper case R, though, and that
would be the preferred character to underline. 




In the following example (and similarly with other components) you cannot specify the correct character to underline:

JLabel label = new JLabel ("Save As...");
label.setDisplayedMnemonic ('A');

The 'A' in 'As' should be underlined (that's the intention at least), but the 'a' in 'Save' is what gets underlined.

The problem (aside from the lack of an API to specify exactly what should be underlined) is in the javax.swing.plaf.basic.BasicGraphicUtils drawString method which underlines the first character (upper OR lowercase) that matches the mnemonic character.
(Review ID: 94042)
======================================================================
Work Around




The only work around that I know of is to pick your text and mnemonic character carefully.
(Review ID: 94042)
======================================================================
Evaluation
Agreed. When looking for the character to underline it should first do a case sensitive search, then case insensitive.
sky 1998-05-29

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.

  xxxxx@xxxxx   2000-07-25

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.
     * <p>
     * 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
     * <code>setMnemonic(KeyEvent.VK_A)</code>.
     *
     * @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
     *
     * @beaninfo
     *        bound: true
     *    attribute: visualUpdate true
     *  description: the index into the String to draw the keyboard character
     *               mnemonic at
     */
    public void setDisplayedMnemonicIndex(int index)
                                          throws IllegalArgumentException;


    /**
     * 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
     * underlined.
     *
     * @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);

  xxxxx@xxxxx   2000-08-09
Comments
  
  Include a link with my name & email   

Submitted On 24-JUL-1998
EMistakeS
What happens using setMnemonic
with a different language 
(for example Japanese, Chinese...)?


Submitted On 04-FEB-2000
kuhse
Come on, this can be fixed by adding 2 or 3 lines
to BasicGraphicsUtils. Why does it take Sun
engineers more than 1.5 years to accomplish
this simple task?


Submitted On 01-MAR-2001
earljavadev
Good idea Alok!  Or in the hexadecimal format in which 
characters are listed in the Unicode charts,

((char) 0X0430) is equivalent to ((char) 1072)

See http://www.unicode.org/charts/


Submitted On 01-MAR-2001
earljavadev
Oops, even cleaner is the Java literal '\u0430'.

For Unicode information in the tutorial, see
http://java.sun.com/docs/books/tutorial/i18n/text/convertint
ro.html


Submitted On 01-MAR-2001
Menghrajani
Here is my workarround. Use CYRILLIC letters, some are 
pretty similar to english ones...

For &quot;Save As...&quot; this is what you should do:
&quot;S&quot;+((char) 1072)+&quot;ve As...&quot;

Alok Menghrajani
alok.menghrajani@epfl.ch


Submitted On 18-MAY-2001
bassclar
If AbstractButton is going to have this this
new 'DisplayedMnemonicIndex' property, then
it should be supported by the Action interface.

For example:

  Action a = new AbstractAction() {
        putValue(NAME, "Save As...");
        putValue(SMALL_ICON, new ImageIcon(icons/saveas.gif));
        putValue(MNEMONIC_KEY, new Integer('A'));
        putValue(DISPLAYED_MNEMONIC_INDEX, new Integer(5)); // <----- need to be able to do this
        putValue(SHORT_DESCRIPTION, "Save to a new file name");
        // could also set values for ACTION_COMMAND_KEY and ACCELERATOR_KEY
        setEnabled(true);
      };
  JButton saveAsButton = new JButton(a);
  JMenuItem saveAsMenuItem = new JMenuItem(a);


Submitted On 09-MAY-2002
jesperhertel
I simply can't understand why you couldn't implement the
fine Windows style of preceeding the wanted character with
an ampersand "&" in the label. This makes it very easy 
for the programmer to add shortcut keys to any widget with 
a label (without using a separate call to setMnemonic), and
to state exactly which character in the label it should 
be. "Save As..." with Alt-A as shortcut would simply be 
stated "Save &As...". Very simple, and very easy to use. 

I guess this also solves the problem with different 
languages?

Of course, the & character itself should be escapable.



PLEASE NOTE: JDK6 is formerly known as Project Mustang