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

Keyboard shortcuts in Swing involving KeyEvent.VK_0 => KeyEvent.VK_9 do not work

XMLWordPrintable

    • x86_64
    • linux_ubuntu

      FULL PRODUCT VERSION :
      openjdk version "1.8.0_91"
      OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~16.04.1-b14)
      OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

      (Note : this issue also occurs with the Oracle JRE/JDK)

      ADDITIONAL OS VERSION INFORMATION :
      Linux matthieu-ubuntu 4.4.0-24-generic #43-Ubuntu SMP Wed Jun 8 19:27:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      KeyStroke shortcuts that involve any of the following keys do not work. (tested with JMenuItem accelerators and JTextComponent's Keymap, both do not work)

      * KeyEvent.VK_0
      * KeyEvent.VK_1
      * KeyEvent.VK_2
      * KeyEvent.VK_3
      * KeyEvent.VK_4
      * KeyEvent.VK_5
      * KeyEvent.VK_6
      * KeyEvent.VK_7
      * KeyEvent.VK_8
      * KeyEvent.VK_9



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Use a JMenuItem accelerator with one of the Virtual Keys that correspond to a number and create a JTextArea with an action bound to a KeyStroke that includes one of the Virtual Keys that correspond to a number that is added to the JTextArea's keymap.

      2) Compile and run

      3) For testing the JMenuItem : use the shortcut that was previously set.
      For testing the JTextArea : click inside the JTextArea and use the shortcut.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JMenuItem : the ActionListener(s) of the JMenuItem should be triggered once the shortcut is used.

      JTextArea : The Action we set for the keymap of the JTextArea should be triggered when the shortcut is used and the JTextArea is focused.

      ACTUAL -
      Nothing happens

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.text.*;

      public class MyExample extends JFrame {

          MyExample() {
              initGUI();
          }

          private void initGUI() {
              // Set up the window
              JPanel pane = (JPanel) getContentPane();
              pane.setLayout(new BorderLayout());

              JTextComponent component = new JTextArea();
              pane.add(component, BorderLayout.CENTER);
              Keymap keymap = component.getKeymap();
              keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_1, InputEvent.CTRL_DOWN_MASK), new MyAction());

              JMenuBar menuBar = new JMenuBar();
              JMenu menu = new JMenu("Test menu");
              menuBar.add(menu);
              JMenuItem menuItem = new JMenuItem("Test menu item");
              menuItem.addActionListener(new MyAction());
              menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, InputEvent.CTRL_DOWN_MASK));
              menu.add(menuItem);
              
              this.setJMenuBar(menuBar);
              
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              setSize(600,300);

          }

          public static void main(String[] args) {
              EventQueue.invokeLater(() -> {
                  MyExample me = new MyExample();
                  me.setVisible(true);
              });
          }

          class MyAction extends AbstractAction{

              @Override
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Action performed");
              }

          }
      }
      ---------- END SOURCE ----------

            ssadetsky Semyon Sadetsky (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: