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

ToolTips consume VK_ESCAPE, even when not shown

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.2.0, 1.2.2
    • client-libs
    • generic, x86
    • generic, windows_nt



      Name: skT88420 Date: 07/21/99


      Run the attached program. As can be seen, using tooltips on JButtons
      and probably other GUI controls, cause VK_ESCAPE keystrokes to
      be consumed by the tooltip. It is correct to consume VK_ESCAPE
      keystrokes ONLY IF THE TIP is visible, not when it is invisible.

      E:\dev>java -version
      java version "1.2.2"
      HotSpot VM (1.0fcs, mixed mode, build E)

      E:\dev>java -fullversion
      java full version "JDK-1.2.2-W"

      E:\dev>
      /*
      This demonstrates that if you put a tooltip on a JButton, VK_ESCAPE will
      no longer close the JDialog containing the JButton.
      Bug verified against 1.2.2 on WinNT 4.0sp3 by Tim Beres -- ###@###.###.
      */
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class ToolTipEscapeBug extends JDialog {
        public ToolTipEscapeBug(){
          super((Frame)null, true);

          // Add VK_ESCPAPE key listener
          JRootPane pane = this.getRootPane();
          pane.registerKeyboardAction(
            new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                ToolTipEscapeBug.this.setVisible(false);
                System.exit(-1);
              }
            },
            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            JComponent.WHEN_IN_FOCUSED_WINDOW
          );

          // Handle closure ourself
          this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

          // Some GUI
          JButton okBtn = new JButton("Meesa OK");
          okBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
              System.exit(0);
            }
          });

          this.getContentPane().setLayout(new BorderLayout());
          this.getContentPane().add(okBtn, BorderLayout.CENTER);

          // If the next line is commented out, the dialog closes when you press
          // the ESCAPE key in WinNT
          // Note: Try not to point out that the ESCAPE key removes the tooltip.
          // Wait for a few seconds, allow the tooltip to go away by itself, then
          // press escape.
          okBtn.setToolTipText("Meesa a bug"); // tool tips consume escape key

          this.setSize(100, 100);
          this.setLocation(100, 100);
          this.validate();
        }

        public static void main(String[] args) {
          ToolTipEscapeBug bug = new ToolTipEscapeBug();
          bug.setVisible(true); // modal
        }

      }
      (Review ID: 88206)
      ======================================================================

      Name: skT88420 Date: 07/28/99


      Some Swing components use escape keystrokes to cancel an
      operation (e.g. undisplaying popup from comboboxes,
      undisplaying tooltips, etc) but they consume the escape keystroke
      even when they do nothing (e.g. combobox without the popup
      displayed). The components affected are JComboBox, JTree, JTable
       and any component with a tooltip set. This prevents a
      keyboard action to trap escape on a window/dialog/frame for
      cancel when an affected component has the focus.

      The escape actions should be changed from ActionListeners to
      AbstractActions in the UI classes with an isEnabled() method that
      only returns true when the particular action would have an
      effect. This would still present a problem with a combobox with
      a tooltip set as only one action is allowed per keystroke.

      In the following code escape has the following effects:
      First combobox - undisplays the popup but has no effect on the frame action
      Second combobox - undisplays the tooltip but has no effect on the popup or frame action
      First textfield - undisplays the tooltip but has no effect on the frame action
      Second textfield - fires the frame action

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      class escape1 {
        public static void main(String args[]) {
          KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
          final ActionListener action = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              System.out.println("Action performed");
            }
          };
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
          frame.getContentPane().add(panel);
          JLabel label = new JLabel("Hello");
          panel.add(label);
          JComboBox combo1 = new JComboBox();
          combo1.addItem("1");
          combo1.addItem("2");
          combo1.addItem("3");
          combo1.addItem("4");
          combo1.addItem("5");
          panel.add(combo1);
          JComboBox combo2 = new JComboBox();
          combo2.addItem("1");
          combo2.addItem("2");
          combo2.addItem("3");
          combo2.addItem("4");
          combo2.addItem("5");
          panel.add(combo2);
          combo2.setEditable(true);
          combo2.setToolTipText("hello");
          JTextField text1 = new JTextField(10);
          panel.add(text1);
          JTextField text2 = new JTextField(10);
          text2.setToolTipText("goodbye");
          panel.add(text2);
          ((JComponent)(frame.getContentPane())).registerKeyboardAction(action, keystroke,
                                       JComponent.WHEN_IN_FOCUSED_WINDOW);
          frame.pack();
          frame.show();
        }
      }

      java -version
      java version "1.2.2"
      Classic VM (build JDK-1.2.2.-W, native threads, symcjit)

      java -fullversion
      java full version "JDK-1.2.2-W"
      (Review ID: 93158)
      ======================================================================

      Name: skT88420 Date: 08/04/99


      I compile and run following program under JDK1.2 (Windows NT4.0). 'Esc' key
      used as exit action for dialog. I bound ActionListener to JRootPane using
      registerKeyboardAction. But when component with tootip has focus, this approach
      doesn't work, because JTooTip steals 'Esc' key event even when tooltip text
      is NOT VISIBLE - it's confusing users.
      If You uncomment lines with 'setToolTipText' You will see that.
      I think that's BUG - JToolTip must be transparent until it's not activated.

      /**
       * EXAMPLE.
       * Application - top entry.
       */

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;


      class EscapeTest {

      JDialog dlgMain;


      // Top entry

      public static void main( String args[] )
      {
      EscapeTest est = new EscapeTest();


      est.dlgMain.setTitle( "Escape test" );
      est.dlgMain.setSize( 400, 200 );
      est.dlgMain.setLocation( 200, 200 );

      est.dlgMain.show();
      }

      // Constructor

      public EscapeTest()
      {


      // Create dialog

      dlgMain = new JDialog();


      // Set action listener - exit when 'Escape' pressed - normal modal dialog behavior.
      // This approach is advised by John Zukowski (article in JavaWorld).

      dlgMain.getRootPane().registerKeyboardAction(
      new EscapeDlg(),
      KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ),
      JComponent.WHEN_IN_FOCUSED_WINDOW );


      // Add components

      {
      Container cntMain = dlgMain.getContentPane();

      JButton jbTop = new JButton( "North button" );
      JTextField tfBot = new JTextField( 20 );


      cntMain.add( jbTop, BorderLayout.NORTH );

      // If You add tool tip to the button,
      // ESCAPE won't work when this component has focus.
      // JToolTip steals ESCAPE key event even when tooltip text is NOT showing on screen !

      // jbTop.setToolTipText( "This button does nothing" );

      jbTop.addActionListener(

      new ActionListener() {

      public void actionPerformed( ActionEvent evt )
      {
      System.out.println( "Button pressed." );
      }
      } );


      // Method to fix problem partially - explicitly set action to EVERY component with tooltip.
      // JTooltip doesn't steal 'Esc' key event in this case. But if tooltip text is VISIBLE and
      // You press 'Esc', - DIALOG will disappear, not tooltip text !

      // jbTop.registerKeyboardAction(
      // new EscapeDlg(),
      // KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ),
      // JComponent.WHEN_FOCUSED );


      cntMain.add( tfBot, BorderLayout.SOUTH );

      // If You add tool tip to the text field,
      // ESCAPE won't work when this component has focus.

      // tfBot.setToolTipText( "Enter information here" );
      }


      // Way to exit

      dlgMain.addWindowListener( new WindowAdapter() {

      public void windowClosing( WindowEvent evt )
      {
      dlgMain.dispose();
      System.exit( 0 );
      }
      } );
      }

      // 'Esc' listener

      private class EscapeDlg
      implements ActionListener {

      public void actionPerformed( ActionEvent evt )
      {
      dlgMain.dispose();
      System.exit( 0 );
      }

      }
      }
      (Review ID: 85181)
      ======================================================================

      Name: skT88420 Date: 09/17/99


      JComboBox should not consume VK_ESCAPE key event when popup is not visible. I don't see any use for the JComboBox to consume this event when its popup is not visible.

      I use this key for triggering the cancel button action in dialogs just like the default button implementation in the JRootPane class.

      KeyEvent.VK_ENTER has the same problem. But for VK_ENTER, CTRL_MASK + VK_ENTER works. In Windows, VK_ESCAPE + CTRL_MASK activates the Windows Start Menu so it didn't work for my dialog.

      This problem also apply to JTextField for VK_ENTER only.
      (Review ID: 95416)
      ======================================================================

      Name: apC97674 Date: 11/29/99


          The reproducible parts of the bug for JDK1.3:
      - the JComboBox always consume the pressing ESC;
      - the testcases behaviour on pressing ESC when tooltip
        is shown differs for WinNT and for Solaris.

      ###@###.###

      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: