2

I'm trying to integrate keybindings into a program I'm making, but as that program is long, I'm trying to learn on a smaller similarly coded program that I found on StackOverflow.

Here's the code I'm using:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Demo {

 @SuppressWarnings("serial")
 private void initGUI() {
  JPanel content = new JPanel(new FlowLayout());
  content.add(new JLabel("Test:"));

  AbstractAction buttonPressed = new AbstractAction() {
   @Override
   public void actionPerformed(ActionEvent e) {
    System.out.println(e.getActionCommand());
    System.out.println(e.getSource());
    if ("a".equals(e.getActionCommand()))
     content.setBackground(new Color(227, 19, 19));
    if ("b".equals(e.getActionCommand()))
     content.setBackground(new Color(0, 225, 19));
   }
  };

  JButton submit = new JButton("Submit");
  submit.addActionListener(buttonPressed);

  submit.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
          javax.swing.KeyStroke.getKeyStroke(
            java.awt.event.KeyEvent.VK_A,
            java.awt.event.InputEvent.CTRL_DOWN_MASK),
          "A_pressed");
  submit.getActionMap().put("A_pressed", buttonPressed);
      
      submit.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
        javax.swing.KeyStroke.getKeyStroke(
          java.awt.event.KeyEvent.VK_B, 0),
        "B_pressed");
  submit.getActionMap().put("B_pressed", buttonPressed);

  content.add(submit);

  JFrame frame = new JFrame("Demo");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setContentPane(content);
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
 }

 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {

   @Override
   public void run() {
    new Demo().initGUI();
   }
  });
 }
}

What I what to happen is to press CTRL+A and have the background change color. However, when I do this, System.out.println(e.getActionCommand()) is returning a character that looks like a question mark in a box like it's an unknown character or something. The program works if you press B, but adding the modifier CTRL is not working correctly.

Is the problem something I'm not doing right? Is the program working correctly and I don't know how to compare e.getActionCommand() and what ever string CTRL+A returns as a ActionEvent? Please help.

IAmTheBee
  • 29
  • 3

1 Answers1

0

in my knowledge if an Action doesn't have its actionCommmand explicitly set it will be set when the ActionEvent is created. in your code the actionCommmand will be the string representation of the charKey of your keyStroke (b and ctrl+a not a) so i could suggest to put a different action for every action :

class simpleAction extends AbstractAction {

        public simpleAction ( String name ) {
            super ();
            putValue ( Action.ACTION_COMMAND_KEY, name );
        }

        @Override
        public void actionPerformed ( ActionEvent e ) {
            System.out.println ( "getActionCommand----->" + e.getActionCommand () );
            System.out.println ( "getSource----->" + e.getSource () );

            if ( "a".equals ( e.getActionCommand () ) ) {
                content.setBackground ( new Color ( 227, 19, 19 ) );
            }
            if ( "b".equals ( e.getActionCommand () ) ) {
                content.setBackground ( new Color ( 0, 225, 19 ) );
            }

        }
    }

    JButton submit = new JButton ( "Submit" );
    submit.addActionListener ( new simpleAction ( "Submit" ) );

    submit.getInputMap ( javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ).put (
            javax.swing.KeyStroke.getKeyStroke ( java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_DOWN_MASK ), "A_pressed" );
    submit.getActionMap ().put ( "A_pressed", new simpleAction ( "a" ) );

    submit.getInputMap ( javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ).put (
            javax.swing.KeyStroke.getKeyStroke (
                    java.awt.event.KeyEvent.VK_B, 0 ),
            "B_pressed" );
    submit.getActionMap ().put ( "B_pressed", new simpleAction ( "b" ) );

or a second solution is check the string representation for the ctrl+a

so you change

 if ( "a".equals ( e.getActionCommand () ) ) {
                content.setBackground ( new Color ( 227, 19, 19 ) );
            }

by

 if ( "\u0001".equals ( e.getActionCommand () ) ) {
                content.setBackground ( new Color ( 227, 19, 19 ) );
            }
Billydan
  • 395
  • 1
  • 7
  • 25
  • You put the same thing twice for the second solution. Also, why does the 2nd solution work? Is "\u0001" the unicode character for CTRL+A? How do I modify it so I can use different letters? – IAmTheBee Oct 12 '15 at 18:19