2 Replies Latest reply: Feb 13, 2013 6:06 AM by Alan.M RSS

    How to preven JButton of generated actions when the user keep pressing down

    Alan.M
      How to preven JButton of generated actions when the user keep pressing down the key or the short cut
      The code below to show the issue when the user keep pressing Alt+ O
      We want to prevent the JButton from generating multi actions just one action only
      A sample of code shows the behaviour which has to be prevented. Continue pressing "Alt +O" and you will see the standard ouptput will print time stamps
      Please notice, I am NOT interested in Mouse press which has a solution by adding a threshold ( setMultiClickThreshhold(long threshhold) on the JButton  as an attribute.

       
      public class TestPanel extends JPanel
      {
      
         private JButton btn;
      
         public TestPanel()
         {
            btn = new JButton("Open");
            this.add(btn);
            registerCommand(new MyAction(), InputEvent.ALT_MASK,
                  KeyEvent.VK_O, btn, btn.getText(), 0);
         }
      
         public static void registerCommand(AbstractAction action,
               int mask,
               int shortCommand,
               JComponent component,
               String actionName,
               int mnemonicIndex)
         {
            InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
            KeyStroke knappKombination = KeyStroke.getKeyStroke(shortCommand, mask);
      
            if ((component instanceof AbstractButton)
                  && mnemonicIndex >= 0
                  && mnemonicIndex < actionName.length()
                  && (shortCommand >= KeyEvent.VK_A && shortCommand <= KeyEvent.VK_Z))
            {
               ((AbstractButton) component).setDisplayedMnemonicIndex(mnemonicIndex);
            }
      
            if (inputMap != null)
            {
               ActionMap actionMap = component.getActionMap();
               inputMap.put(knappKombination, actionName);
               if (actionMap != null)
               {
                  actionMap.put(actionName, action);
               }
            }
         }
      
         public static class MyAction extends AbstractAction
         {
      
            /**
             * 
             */
            private static final long serialVersionUID = 1L;
      
            @Override
            public void actionPerformed(ActionEvent e)
            {
               System.out.println(System.currentTimeMillis());
      
            }
      
         }
      
         public static void main(String... args)
         {
            SwingUtilities.invokeLater(new Runnable()
            {
               public void run()
               {
                  JFrame frame = new JFrame("Testing");
                  JPanel panel = new TestPanel();
                  frame.getContentPane().add(panel);
                  frame.setPreferredSize(new Dimension(500, 500));
                  frame.setMinimumSize(new Dimension(500, 500));
                  frame.pack();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);
               }
            });
         }
      
      }
      Edited by: user12130673 on 2013-feb-13 03:01