Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4508646

Tooltip generated by JToolBar.add(Action) wrong

XMLWordPrintable

      Name: nt126004 Date: 09/28/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)


      If you have an Action with a Mnemonic set then the Mnemonic is used to add
      an incorrect shortcut in the tooltip for the button added to a JToolBar using
      JToolBar.add(Action). It's also used to underline the mnemonic letter in the
      text of the button.

      Now I know that the use of JToolBar.add(Action) is discouraged, but there
      isn't a good workaround (see bug #4461866).

      e.g.

      AbstractAction quit = new AbstractAction("Quit") {
        public void actionPerformed(ActionEvent e) {
           //do something
        }
      };

      quit.putValue(Action.MNEMONIC_KEY, "Q");
      quit.putValue(Action.SHORT_DESCRIPTION, "Quit the application");
      JToolBar tb = new ToolBar();
      tb.add(quit);

      The Tooltip for the resulting button will look something like....

      -------------------------------
      | Quit the Application Alt-Q |
      -------------------------------

      This is incorrect. If, for example, you had a menu using mnemonic Q, the
      menu will open when you do Alt-Q.

      Suggest that the tooltip for JToolBar buttons contains only the
      SHORT_DESCRIPTION, since JToolBar buttons can be thought of as 'mouse shortcuts'

      This doesn't stop the toolbar button working, but is misleading to the user.


      ------------------------- Test case -----------
      import java.awt.*;
      import java.awt.event.*;

      import javax.swing.*;

      public class ToolTipTestCase extends JFrame {


         Action openAction;
         Action findAction;
         Action closeAction;
         Action quitAction;


         ToolTipTestCase() {


            initActions();
            initMenuBar();

            Container cp = getContentPane();

            cp.setLayout(new BorderLayout());

            cp.add(buildToolBar(), BorderLayout.NORTH);

            cp.add(buildExplanation(), BorderLayout.CENTER);

            pack();

         }

        
          
         private void initActions() {

            openAction = new AbstractAction("Open") {
                  public void actionPerformed(ActionEvent e) {
                     // do something
                  }
               };

            openAction.putValue(Action.SHORT_DESCRIPTION, "open");
            openAction.putValue(Action.MNEMONIC_KEY,
                                new Integer(KeyEvent.VK_O));

            closeAction = new AbstractAction("Close") {
                  public void actionPerformed(ActionEvent e) {
                     // do something
                  }
               };

            closeAction.putValue(Action.SHORT_DESCRIPTION, "close");
            closeAction.putValue(Action.MNEMONIC_KEY,
                                 new Integer(KeyEvent.VK_C));

            findAction = new AbstractAction("Find") {
                  public void actionPerformed(ActionEvent e) {
                     JOptionPane.showMessageDialog(ToolTipTestCase.this,
                                                   "Got Find");
                  }
               };

            findAction.putValue(Action.SHORT_DESCRIPTION, "find");
            findAction.putValue(Action.MNEMONIC_KEY,
                                new Integer(KeyEvent.VK_F));
            findAction.putValue(Action.ACCELERATOR_KEY,
                                KeyStroke.getKeyStroke(KeyEvent.VK_F,
                                                       java.awt.event.InputEvent.CTRL_MASK));

            quitAction = new AbstractAction("Quit") {
                  public void actionPerformed(ActionEvent e) {
                     // do something
                  }
               };

            quitAction.putValue(Action.SHORT_DESCRIPTION, "quit");
            quitAction.putValue(Action.MNEMONIC_KEY,
                                new Integer(KeyEvent.VK_Q));

         }


         private void initMenuBar() {

            JMenuItem open = new JMenuItem();
            open.setAction(openAction);

            JMenuItem close = new JMenuItem();
            close.setAction(closeAction);
           
            JMenuItem find = new JMenuItem();
            find.setAction(findAction);
           
            JMenuItem quit = new JMenuItem();
            quit.setAction(quitAction);

            JMenu file = new JMenu("File");
            file.setMnemonic(KeyEvent.VK_F);
            file.add(open);
            file.add(close);
            file.add(find);
            file.add(quit);
          
            JMenuBar mb = new JMenuBar();
            mb.add(file);

            setJMenuBar(mb);
         }

         private JToolBar buildToolBar() {

            JToolBar tb = new JToolBar();

            //add(Action) discouraged, but no good workaround (bug #4461866)
            tb.add(openAction);
            tb.add(closeAction);
            tb.add(findAction);
            tb.add(quitAction);

            return tb;
         }

         private JTextArea buildExplanation() {
            JTextArea ta = new JTextArea();
            ta.setText("Look at the File menu.\n" +
                       "The File menu can be opened with alt-F then the items selected using " +
                       "the underlined letter\n\n" +
                       "e.g. Alt-F followed by F to select find (try it)\n\n" +
                       "Note that the Find menu item correctly displays a keyboard short cut Ctrl-F\n\n" +
                       "Now look at the toolbar. The same actions are repeated.\n" +
                       "Note that the button text underlines the mnemonic letter\n" +
                       "This is incorrect. suggest that button text on ToolBar buttons does not\n" +
                       "display the mnemonic text\n\n" +
                       "Hover the mouse over the Find button and you will get a tooltip\n" +
                       "That looks something like\n\n" +
                       " ------------\n" +
                       " | find Alt-F |\n" +
                       " ------------\n\n" +
                       "Which implies that you can do a find by executing Alt-F\n" +
                       "Of course if you do Alt-F, the file menu opens.\n" +
                       "This is misleading.\n\n" +
                       "Suggest that tooltips on ToolBar items contain the\n" +
                       "accelerator shortcut if present (Ctrl-F in this case) or\n" +
                       "just the tooltip text if no accelerator is present\n\n" +
                       "so in this case the Open, Close and Quit tooltips should\n" +
                       "show no accelerator\n\n" +
                       "Note that Ctrl-F correctly executes the Find Action(try it)\n\n" +
                       "regards\n\nNeale Swinnerton, Isismanor Ltd."
                      );

            return ta;
         }
         
         public static void main(String[] args) {

            JFrame f = new ToolTipTestCase();
            f.setVisible(true);
         }
      }
      (Review ID: 132707)
      ======================================================================

            alexp Alexander Potochkin (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: