Java keypad event creation. What type of event to create? KeyEvent or ActionEvent?

Posted on 2014-08-23
Last Modified: 2014-08-28
I'm writing a Java program that will ultimately target an embedded system.

The embedded system will have a physical keypad.

Pressing a button on the physical keypad creates an interrupt which runs the interrupt code which can do... whatever would be appropriate for it to do. (It provides a different number for each keypad button, so we can tell which keypad button was pressed.) Currently I'm writing the emulation code that emulates this keypad.

I'm not sure if I should be generating a KeyEvent, or an ActionEvent, or some other sort of event.

I have a window (JFrame) that emulates the keypad, showing buttons for each key on the keypad.

I have another window (another JFrame) which is the view. This window should react to the keypad buttons in the other window. I expect this view window will have some sort of listener listening for keypad button pressed events.

To further complicate, it would be nice if the emulation program would also respond to the computer keyboard; e.g. the keypad emulation window has Up, Down, Left, Right arrow keys, so pressing the arrow keys on the computer keyboard should do the same thing as clicking the arrow button in the keypad emulation window.

To further confuse, I just realized there are two different ActionEvents in Java
I'm not using javafx.

So my questions are:
what sort of "event" should my keypad emulation window generate?
How can my View Window subscribe to these events?
(Supposedly Keypad knows nothing about View, and View knows nothing about details of Keypad. Probably a Controller knows about both of them and connects them together.)

How can I get the Computer Keyboard Arrow Keys to also effect the same result as the arrow key buttons in the Keypad Emulation Window?

Can you provide a simple example?
Question by:deleyd
    LVL 37

    Accepted Solution

    >> what sort of "event" should my keypad emulation window generate?
    KeyEvent for sure
    >> How can my View Window subscribe to these events?
    Your window that emulates the keypad should implement KeyEventDispatcher
    Your window that should listen to those KeyEvents should implement KeyListener and should subscribe itself to receive the key events dispatched by the emulating window by calling:
            <your emulating window>.addKeyListener(this);

    Open in new window

    >> How can I get the Computer Keyboard Arrow Keys to also effect the same result
    >> as the arrow key buttons in the Keypad Emulation Window?
    By also let the same listening window subscribe itself to the class that generates the "real" key events:
            <your other class generating "real" key events>..addKeyListener(this);

    Open in new window

    >> Can you provide a simple example?
    Sorry, I don't have the time for that. But maybe these can help you:

    Author Comment

    Is there another way to set the parent InputMap and ActionMap of controls to be the JPanel they are all on? Or do I have to go through each control separately and manually set the parent InputMap and ActionMap?
    int rows = 3;
    int cols = 3;
    JPanel panel = new JPanel(new GridLayout(rows, cols));
    InputMap inputMapPanel = panel.getInputMap();
    ActionMap actionMapPanel = panel.getActionMap();
    KeyStroke keyUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
    KeyStroke keyDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
    inputMapPanel.put(keyUp, "DoSomething");
    inputMapPanel.put(keyDown, "DoThis");

    Open in new window

    LVL 37

    Expert Comment

    An alternative can be seen in this code of my class EscDialog being a JDialog that closes when ESC is pressed.
    But it should react on pressing ESC, no matter what control is currently focused.
    Therefor the KeyListener is added recursively. I think that's what you want if I understand your code above well.

    import javax.swing.JDialog;
    import java.awt.*;
    import java.awt.event.*;
     * a JDialog that disappears when ESC is pressed
    public class EscDialog extends JDialog implements ContainerListener, KeyListener {
        public EscDialog(Frame frame, String title, boolean modal) {
            super(frame, title, modal);
        public EscDialog(Frame frame, boolean modal) {
            super(frame, modal);
        //The following function is recursive and is intended for internal use only.
        //It is private to prevent anyone calling it from other classes
        //The function takes a Component as an argument and adds this JDialog as a KeyListener to it.
        //Besides it checks if the component is actually a Container and if it is, 
        //there  are 2 additional things to be done to this Container :
        // 1 - add this Dialog as a ContainerListener to the Container
        // 2 - call this function recursively for every child of the Container.
        private void addListenersRecursively(Component c) {
            //To be on the safe side, try to remove KeyListener first 
            //just in case it has been added before.
            //If not, it won't do any harm
            //Add KeyListener to the Component passed as an argument
            if (c instanceof Container) {
                //Component c is a Container. The following cast is safe.
                Container container = (Container) c;
                //To be on the safe side, try to remove ContainerListener first 
                //just in case it has been added before.
                //If not, it won't do any harm
                //Add ContainerListener to the Container.
                //Get the Container's array of children Components.
                Component[] children = container.getComponents();
                //For every child repeat the above operation.
                for (int i = 0; i < children.length; i++) {
        //The following function is the same as the function above with the exception that it does exactly the opposite - 
        // removes this Dialog from the listener lists of Components.
        private void removeListenersRecursively(Component c) {
            if (c instanceof Container) {
                Container container = (Container) c;
                Component[] children = container.getComponents();
                for (int i = 0; i < children.length; i++) {
        //ContainerListener interface
        //This function is called whenever a Component or a Container is added 
        //to another Container belonging to this Dialog
        public void componentAdded(ContainerEvent e) {
        //This function is called whenever a Component or a Container is removed from another Container belonging to this Dialog
        public void componentRemoved(ContainerEvent e) {
        //KeyListener interface
        //This function is called whenever a Component belonging to this Dialog 
        //(or the Dialog itself) gets the KEY_PRESSED event
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                //Key pressed is the ESCAPE key.
        //We need the following 2 functions to complete imlementation of KeyListener
        public void keyReleased(KeyEvent e) {
        public void keyTyped(KeyEvent e) {
        //Default response to ESCAPE key pressed goes here
        public void performEscapeAction(KeyEvent evt) {

    Open in new window


    Author Closing Comment

    Thank you I think I have it now.

    For anyone else reading this, I found how to make a class an "ActionTalker" here:

    The parts are:

    private EventListenerList actionListenerList = new EventListenerList();

          public void addActionListener(ActionListener actionListener) {
                actionListenerList.add(ActionListener.class, actionListener);

          public void removeActionListener(ActionListener actionListener) {
                actionListenerList.remove(ActionListener.class, actionListener);

          protected void fireActionPerformed(ActionEvent actionEvent) {
                EventListener listenerList[] = actionListenerList.getListeners(ActionListener.class);
                for (int i = 0, n = listenerList.length; i < n; i++) {
                      ((ActionListener) listenerList[i]).actionPerformed(actionEvent);
    LVL 37

    Expert Comment

    Thanx 4 axxepting

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now