0

I have been stock for a couple of hours now lol, not fun but this is why.. I have an app for typing which is to set the background of the button keyboard to a different colour lest say yellow (like highlighted) when typing or key is pressed each button will be highlighted and it will set the background back to white when release.. Well, my KeyListener I think that it doesn't has any focus but I seem to be losing something or not seen it where is that happening..

I had to comment out the adding of the listener because give an exception of null pointer..

I really hope someone can see what I can't or what I missing.. This is a runnable code which will make it easier for you guys to understand whats going on and be able to test it yourself and see what I mean..

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;


public  class typeTutor extends JFrame implements KeyListener
{  
    //Individual keyboard rows  
    String firstRow[] = {"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "fill", "BackSpace"};
    String secondRow[] = {"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"};
    String thirdRow[] = {"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "fill", "fill", "Enter"};
    String fourthRow[] = {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "blank", "^"};
    String fifthRow[] = {"blank", "blank", "fill", "fill", "fill", "fill", "fill", "fill", "fill", "fill", "", "<", "v", ">"};
    private JTextField textField;
    //Individual keyboard rows  
    /*
    String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","BackSpace"};
    String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"};
    String thirdRow[] = {"Caps","A","S","D","F","G","H","J","K","L",":","\"","Enter"};
    String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","   ^" };
    String fifthRow[]={"               " ,"<" ,"v",">" };
    */
    String strText = "";
    //all keys without shift key press
    String noShift="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
    //special characters on keyboard that has to be addressed during key press
    String specialChars ="~-+[]\\;',.?";
    int keycode;

    JTextArea  text = new JTextArea();

    //Jbuttons corresponding to each individual rows 
    JButton first[];
    JButton second[];
    JButton third[];
    JButton fourth[];
    JButton fifth[];



    //default color of the button to be repainted when key released 
    Color cc = new JButton().getBackground();


    //Driver main method to start the application 
    public static void main(String[] args) {
        //launch typing tutor
        typeTutor a = new typeTutor();
        a.setVisible(true);
    }

    // No argument constructor to create frame
    public typeTutor()
    {
        super("Type Tutor - My JAC444");

        JPanel contentPane = new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                Graphics2D g2d = (Graphics2D) grphcs;
                Dimension d = this.getSize();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

                GradientPaint gp = new GradientPaint(0, 0,
                        getBackground().brighter().brighter(), 0, d.height,
                        getBackground().darker().darker());

                g2d.setPaint(gp);
                g2d.fillRect(0, 0, d.width, d.height);

                super.paintComponent(grphcs);
            }
        };
        contentPane.setOpaque(false);
        setContentPane(contentPane);

        //textField = new JTextField(80);
        //text.setEditable(true);

        TextFieldHandler handler = new TextFieldHandler();

        //set the info label on top 
        JLabel info = new JLabel("<html>&nbsp;&nbsp;Type some text using your keyboard.The keys you press will be highlighted and text will be displayed.<br> &nbsp;&nbsp;Note : Clicking the buttons with your mouse will not perform any action. <br><br> </html>" );
        //set the bold font for info
        info.setFont(new Font("Verdana",Font.BOLD,12));

        //set the layout and place component in place and pack it 
        setLayout(new BorderLayout());

        //Various panel for the layout 
        JPanel jpNorth = new JPanel();
        JPanel jpCenter = new JPanel();
        jpCenter.setPreferredSize(new Dimension(10,10));
        JPanel jpKeyboard = new JPanel(new GridBagLayout());
        JPanel jpNote = new JPanel();

        add(jpNorth, BorderLayout.NORTH);
        add(jpNote);

        add(jpCenter, BorderLayout.CENTER);
        add(jpKeyboard, BorderLayout.SOUTH);

        jpNorth.setLayout(new BorderLayout());
        jpNorth.add(info, BorderLayout.WEST);
        jpNorth.add(info, BorderLayout.SOUTH);

        jpCenter.setLayout( new BorderLayout());
        jpCenter.add(text, BorderLayout.WEST);

        jpCenter.add(text, BorderLayout.CENTER);
        jpCenter.setPreferredSize(new Dimension(10,10));

        first = new JButton[firstRow.length];
        second = new JButton[secondRow.length];
        third = new JButton[thirdRow.length];
        fourth = new JButton[fourthRow.length];
        fifth = new JButton[fifthRow.length];

        addKeys(jpKeyboard, 0, firstRow, first);
        addKeys(jpKeyboard, 1, secondRow, second);
        addKeys(jpKeyboard, 2, thirdRow, third);
        addKeys(jpKeyboard, 3, fourthRow, fourth);
        addKeys(jpKeyboard, 4, fifthRow, fifth);

        //jpCenter.setPreferredSize(new Dimension(10,10));
        jpKeyboard.setPreferredSize(new Dimension(800,160));

        info.setOpaque(false);
        jpNote.setOpaque(false);
        jpNorth.setOpaque(false);
        jpCenter.setOpaque(false);
        jpKeyboard.setOpaque(false);
        /*
        //add listeners 
        //getComponent().addKeyListener(this);
        text.addKeyListener(this);
        // add listeners to all the button 
        for(JButton b : first)
            b.addKeyListener(this); 
        for(JButton b : second)
            b.addKeyListener(this); 
        for(JButton b : third)
            b.addKeyListener(this); 

        for(JButton b : fourth)
            b.addKeyListener(this); 

        for(JButton b : fifth)
            b.addKeyListener(this);
        */

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //set non re-sizable 
        this.setResizable(false);
        //set size of the content pane ie frame
        this.getContentPane().setPreferredSize(new Dimension(1100,350));
        //set location for the frame 
        //this.setLocation(150,150);
        //init and paint frame 
        initWidgets();
        pack();
        this.toFront(); // brings this Window to the front and may make it the focused Window
        this.setLocationRelativeTo(null); // Window appears center


    }


    // Method to initialize frame component 
    private void initWidgets()
    {


    }
    private class TextAreaHandler implements ActionListener
   {
      // process textfield events
      public void actionPerformed( ActionEvent event )
      {
         String string = ""; // declare string to display

         // user pressed Enter in JTextField textField1
         if ( event.getSource() == textField )
            string = String.format( "%s",
               event.getActionCommand() );
       }
    }

    // Invoked when a key has been pressed.
    // @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
    public void keyPressed(KeyEvent evt) 
    {
        keycode = evt.getKeyCode();
        strText = String.format( "%s",KeyEvent.getKeyText( evt.getKeyCode() ) );
        setBackground(Color.BLUE);
    }//end of key pressed


    //Invoked when a key has been released.
    public void keyReleased(KeyEvent evt)
    {
         keycode = evt.getKeyCode();
         strText = String.format( "%s",KeyEvent.getKeyText( evt.getKeyCode() ) );
         getBackground();
    }

    public void keyTyped(KeyEvent evt) 
    {
        strText = String.format( "%s", evt.getKeyChar() );
    }//end of key pressed

    private class TextFieldHandler implements ActionListener
    {
      // process textfield events
      public void actionPerformed( ActionEvent event )
      {
         String string = ""; // declare string to display

         // user pressed Enter in JTextField textField1
         if ( event.getSource() == text )
             strText = String.format( "%s", event.getActionCommand() );
       }
    }


    protected void addKeys(JPanel parent, int row, String[] keys, JButton[] buttons) {

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = row;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.BOTH;

        int gap = 0;
        for (int index = 0; index < keys.length; index++) {
            String key = keys[index];
            if ("blank".equalsIgnoreCase(key)) {
                gbc.gridx++;
            } else if ("fill".equalsIgnoreCase(key)) {
                gbc.gridwidth++;
                gap++;
            } else {
                //System.out.println("Add " + key);
                JButton btn = new JButton(key);
                buttons[index] = btn;
                parent.add(btn, gbc);
                gbc.gridx += gap + 1;
                gbc.gridwidth = 1;
                gap = 0;
            }
        }

    }   

}//end of class

If you guys need more clarification or have any question about it, please let me know and I will provide as much information as I can. I will really appreciate your help, you how frustrating it gets when you are stock and can't find the answer to the problem.. not fun! :(

Braj
  • 46,415
  • 5
  • 60
  • 76
valbu17
  • 4,034
  • 3
  • 30
  • 41
  • Start by taking a look at [Code Conventions for the Java Programming Language](http://www.oracle.com/technetwork/java/codeconv-138413.html) and [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) - Won't fix your immeditate problem, but will make you code more easy to read and reduce the potetional for further issues in the future... – MadProgrammer Mar 15 '14 at 00:12
  • You want to color buttons when particular key is pressed. Right? – Braj Mar 15 '14 at 00:34
  • @Braj yah.. thats correct – valbu17 Mar 15 '14 at 00:35
  • @Braj Im on it.. Im putting things together.. thanks for taking the time to answer, as soon as I tried. I will get back to you. – valbu17 Mar 15 '14 at 01:52

2 Answers2

3

The main problem is, focus. The component that the KeyListener is registered to must be focusable and have focus before it can be notified of key events.

Normally, I would suggest using the Key Bindings API, but that would require you to register a binding for each and every key you want to monitor, including it's shift state (almost doubling the key bindings).

An alternative approach would be register a AWTEventListener with the Event Queue directly.

This is very low level stuff, but basically means you will receive notification of just about every AWT based event that passes through the system. You can filter this to only include key events, so that makes your work slightly easier...

Below is a VERY simple example. Know, you're going to receive two (basic) things. The virtual key code, which is used to make different key boards and platforms to a common known key value and the key character.

Key characters are modified by the state of the shift key. So you're going to have to devise a method for looking up the key code/character and finding the button associated with it...

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

    @Override
    public void eventDispatched(AWTEvent event) {
        if (event instanceof KeyEvent) {
            KeyEvent ke = (KeyEvent) event;
            switch (ke.getID()) {
                case KeyEvent.KEY_PRESSED:
                    // set the various state variables, like color for this state
                    break;
                case KeyEvent.KEY_RELEASED:
                    // set the various state variables, like color for this state
                    break;
            }
            switch (ke.getKeyCode()) {
                case KeyEvent.VK_SHIFT:
                    if (ke.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT) {
                        // left shift
                    } else if (ke.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT) {
                        // right shift
                    }
                    break;
                case KeyEvent.VK_CAPS_LOCK:
                    break;
                case KeyEvent.VK_DELETE:
                    break;
                case KeyEvent.VK_ENTER:
                    break;
                case KeyEvent.VK_UP:
                    break;
                case KeyEvent.VK_DOWN:
                    break;
                case KeyEvent.VK_LEFT:
                    break;
                case KeyEvent.VK_RIGHT:
                    break;
                default:
                    // You could use the ke.getKeyChar() here, but remember,
                    // the caps-lock and shift will change the char returned
                    // the key code is safer, but that's a lot of work ;)
                    break;
            }
        }
    }
}, AWTEvent.KEY_EVENT_MASK);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

A simple solution for most of keys:

Steps to follow:

  • create a map of button having value as key
  • get button based on key pressed
  • change the color of returned button to blue
  • on key release do the same thing and set the color white

Code segment:

Map<Integer,JButton> map=new HashMap<Integer,JButton>();

public void keyPressed(KeyEvent evt) {
    keycode = evt.getKeyCode();
    strText = String.format("%s", KeyEvent.getKeyText(evt.getKeyCode()));

    JButton btn = map.get(keycode);
    if (btn != null) {
        map.get(keycode).setBackground(Color.BLUE);
    }

}// end of key pressed

public void keyReleased(KeyEvent evt) {
    keycode = evt.getKeyCode();
    strText = String.format("%s", KeyEvent.getKeyText(evt.getKeyCode()));

    JButton btn = map.get(keycode);
    if (btn != null) {
        map.get(keycode).setBackground(Color.WHITE);
    }
}

protected void addKeys(JPanel parent, int row, String[] keys, JButton[] buttons) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridy = row;
    gbc.gridx = 0;
    gbc.fill = GridBagConstraints.BOTH;

    int gap = 0;
    for (int index = 0; index < keys.length; index++) {
        String key = keys[index];
        if ("blank".equalsIgnoreCase(key)) {
            gbc.gridx++;
        } else if ("fill".equalsIgnoreCase(key)) {
            gbc.gridwidth++;
            gap++;
        } else {
            // System.out.println("Add " + key);
            JButton btn = new JButton(key);
            buttons[index] = btn;
            parent.add(btn, gbc);
            gbc.gridx += gap + 1;
            gbc.gridwidth = 1;
            gap = 0;

            System.out.println(key);
            btn.setBackground(Color.WHITE);
            map.put(getKeyCode(key), btn);
        }
    }

}


private int getKeyCode(String key) {
    if (key.equals("BackSpace")) {
        return KeyEvent.VK_BACK_SPACE;
    } else if (key.equals("Tab")) {
        return KeyEvent.VK_TAB;
    } else if (key.equals("Caps")) {
        return KeyEvent.VK_CAPS_LOCK;
    } else if (key.equals("Enter")) {
        return KeyEvent.VK_ENTER;
    } else if (key.equals("Shift")) {
        return KeyEvent.VK_SHIFT;
    } else if (key.equals("")) {
        return KeyEvent.VK_SPACE;
    } else if (key.equals("+")) {
        return KeyEvent.VK_EQUALS;
    }else if (key.equals(":")) {
        return KeyEvent.VK_SEMICOLON;
    }else if (key.equals("\"")) {
        return KeyEvent.VK_QUOTE;
    }else if (key.equals("?")) {
        return KeyEvent.VK_SLASH;
    }else if (key.equals("~")) {
        return KeyEvent.VK_BACK_QUOTE;
    } else if (key.equals("^")) {
        return KeyEvent.VK_UP;
    } else if (key.equals("v")) {
        return KeyEvent.VK_DOWN;
    } else if (key.equals("<")) {
        return KeyEvent.VK_LEFT;
    } else if (key.equals(">")) {
        return KeyEvent.VK_RIGHT;
    } else {
        return (int) key.charAt(0);
    }
}

Full code (if you are getting errors. changes are mentioned above)

package com.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class typeTutor extends JFrame implements KeyListener {

    Map<Integer, JButton> map = new HashMap<Integer, JButton>();

    // Individual keyboard rows
    String firstRow[] = { "~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "fill",
            "BackSpace" };
    String secondRow[] = { "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\" };
    String thirdRow[] = { "Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "fill",
            "fill", "Enter" };
    String fourthRow[] = { "Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "blank", "^" };
    String fifthRow[] = { "blank", "blank", "fill", "fill", "fill", "fill", "fill", "fill", "fill",
            "fill", "", "<", "v", ">" };
    private JTextField textField;
    // Individual keyboard rows
    /*
     * String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","BackSpace"}; String
     * secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"}; String thirdRow[]
     * = {"Caps","A","S","D","F","G","H","J","K","L",":","\"","Enter"}; String fourthRow[] =
     * {"Shift","Z","X","C","V","B","N","M",",",".","?","   ^" }; String
     * fifthRow[]={"               " ,"<" ,"v",">" };
     */
    String strText = "";
    // all keys without shift key press
    String noShift = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
    // special characters on keyboard that has to be addressed during key press
    String specialChars = "~-+[]\\;',.?";
    int keycode;

    JTextArea text = new JTextArea();

    // Jbuttons corresponding to each individual rows
    JButton first[];
    JButton second[];
    JButton third[];
    JButton fourth[];
    JButton fifth[];

    // default color of the button to be repainted when key released
    Color cc = new JButton().getBackground();

    // Driver main method to start the application
    public static void main(String[] args) {
        // launch typing tutor
        typeTutor a = new typeTutor();
        a.setVisible(true);
    }

    // No argument constructor to create frame
    public typeTutor() {
        super("Type Tutor - My JAC444");

        JPanel contentPane = new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                Graphics2D g2d = (Graphics2D) grphcs;
                Dimension d = this.getSize();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);

                GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter().brighter(),
                        0, d.height, getBackground().darker().darker());

                g2d.setPaint(gp);
                g2d.fillRect(0, 0, d.width, d.height);

                super.paintComponent(grphcs);
            }
        };
        contentPane.setOpaque(false);
        setContentPane(contentPane);

        // textField = new JTextField(80);
        // text.setEditable(true);

        TextFieldHandler handler = new TextFieldHandler();

        // set the info label on top
        JLabel info = new JLabel(
                "<html>&nbsp;&nbsp;Type some text using your keyboard.The keys you press will be highlighted and text will be displayed.<br> &nbsp;&nbsp;Note : Clicking the buttons with your mouse will not perform any action. <br><br> </html>");
        // set the bold font for info
        info.setFont(new Font("Verdana", Font.BOLD, 12));

        // set the layout and place component in place and pack it
        setLayout(new BorderLayout());

        // Various panel for the layout
        JPanel jpNorth = new JPanel();
        JPanel jpCenter = new JPanel();
        jpCenter.setPreferredSize(new Dimension(10, 10));
        JPanel jpKeyboard = new JPanel(new GridBagLayout());
        JPanel jpNote = new JPanel();

        add(jpNorth, BorderLayout.NORTH);
        add(jpNote);

        add(jpCenter, BorderLayout.CENTER);
        add(jpKeyboard, BorderLayout.SOUTH);

        jpNorth.setLayout(new BorderLayout());
        jpNorth.add(info, BorderLayout.WEST);
        jpNorth.add(info, BorderLayout.SOUTH);

        jpCenter.setLayout(new BorderLayout());
        jpCenter.add(text, BorderLayout.WEST);

        jpCenter.add(text, BorderLayout.CENTER);
        jpCenter.setPreferredSize(new Dimension(10, 10));

        first = new JButton[firstRow.length];
        second = new JButton[secondRow.length];
        third = new JButton[thirdRow.length];
        fourth = new JButton[fourthRow.length];
        fifth = new JButton[fifthRow.length];

        addKeys(jpKeyboard, 0, firstRow, first);
        addKeys(jpKeyboard, 1, secondRow, second);
        addKeys(jpKeyboard, 2, thirdRow, third);
        addKeys(jpKeyboard, 3, fourthRow, fourth);
        addKeys(jpKeyboard, 4, fifthRow, fifth);

        // jpCenter.setPreferredSize(new Dimension(10,10));
        jpKeyboard.setPreferredSize(new Dimension(800, 160));

        info.setOpaque(false);
        jpNote.setOpaque(false);
        jpNorth.setOpaque(false);
        jpCenter.setOpaque(false);
        jpKeyboard.setOpaque(false);

        // add listeners
        // getComponent().addKeyListener(this);
        text.addKeyListener(this);
        // add listeners to all the button
        /*
         * for (JButton b : first) { if (b != null) { b.addKeyListener(this); } }
         */
        /*
         * for(JButton b : second) b.addKeyListener(this); for(JButton b : third)
         * b.addKeyListener(this);
         * 
         * for(JButton b : fourth) b.addKeyListener(this);
         * 
         * for(JButton b : fifth) b.addKeyListener(this);
         */

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // set non re-sizable
        this.setResizable(false);
        // set size of the content pane ie frame
        this.getContentPane().setPreferredSize(new Dimension(1100, 350));
        // set location for the frame
        // this.setLocation(150,150);
        // init and paint frame
        initWidgets();
        pack();
        this.toFront(); // brings this Window to the front and may make it the focused Window
        this.setLocationRelativeTo(null); // Window appears center

    }

    // Method to initialize frame component
    private void initWidgets() {

    }

    private class TextAreaHandler implements ActionListener {
        // process textfield events
        public void actionPerformed(ActionEvent event) {
            String string = ""; // declare string to display

            // user pressed Enter in JTextField textField1
            if (event.getSource() == textField)
                string = String.format("%s", event.getActionCommand());
        }
    }

    // Invoked when a key has been pressed.
    // @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
    public void keyPressed(KeyEvent evt) {
        keycode = evt.getKeyCode();
        strText = String.format("%s", KeyEvent.getKeyText(evt.getKeyCode()));

        JButton btn = map.get(keycode);
        if (btn != null) {
             map.get(keycode).setBackground(Color.BLUE);
        }

    }// end of key pressed

    // Invoked when a key has been released.
    public void keyReleased(KeyEvent evt) {
        keycode = evt.getKeyCode();
        strText = String.format("%s", KeyEvent.getKeyText(evt.getKeyCode()));

        JButton btn = map.get(keycode);
        if (btn != null) {
             map.get(keycode).setBackground(Color.WHITE);
        }
    }

    public void keyTyped(KeyEvent evt) {
        strText = String.format("%s", evt.getKeyChar());
    }// end of key pressed

    private class TextFieldHandler implements ActionListener {
        // process textfield events
        public void actionPerformed(ActionEvent event) {
            String string = ""; // declare string to display

            // user pressed Enter in JTextField textField1
            if (event.getSource() == text)
                strText = String.format("%s", event.getActionCommand());
        }
    }

    protected void addKeys(JPanel parent, int row, String[] keys, JButton[] buttons) {

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = row;
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.BOTH;

        int gap = 0;
        for (int index = 0; index < keys.length; index++) {
            String key = keys[index];
            if ("blank".equalsIgnoreCase(key)) {
                gbc.gridx++;
            } else if ("fill".equalsIgnoreCase(key)) {
                gbc.gridwidth++;
                gap++;
            } else {
                // System.out.println("Add " + key);
                JButton btn = new JButton(key);
                buttons[index] = btn;
                parent.add(btn, gbc);
                gbc.gridx += gap + 1;
                gbc.gridwidth = 1;
                gap = 0;

                System.out.println(key);
                btn.setBackground(Color.WHITE);
                map.put(getKeyCode(key), btn);
            }
        }

    }

    private int getKeyCode(String key) {
        if (key.equals("BackSpace")) {
            return KeyEvent.VK_BACK_SPACE;
        } else if (key.equals("Tab")) {
            return KeyEvent.VK_TAB;
        } else if (key.equals("Caps")) {
            return KeyEvent.VK_CAPS_LOCK;
        } else if (key.equals("Enter")) {
            return KeyEvent.VK_ENTER;
        } else if (key.equals("Shift")) {
            return KeyEvent.VK_SHIFT;
        } else if (key.equals("")) {
            return KeyEvent.VK_SPACE;
        } else if (key.equals("+")) {
            return KeyEvent.VK_EQUALS;
        }else if (key.equals(":")) {
            return KeyEvent.VK_SEMICOLON;
        }else if (key.equals("\"")) {
            return KeyEvent.VK_QUOTE;
        }else if (key.equals("?")) {
            return KeyEvent.VK_SLASH;
        }else if (key.equals("~")) {
            return KeyEvent.VK_BACK_QUOTE;
        } else if (key.equals("^")) {
            return KeyEvent.VK_UP;
        } else if (key.equals("v")) {
            return KeyEvent.VK_DOWN;
        } else if (key.equals("<")) {
            return KeyEvent.VK_LEFT;
        } else if (key.equals(">")) {
            return KeyEvent.VK_RIGHT;
        } else {
            return (int) key.charAt(0);
        }
    }

}// end of class

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Let me complete it for all keys... Meanwhile you can try it. Tell me whether it is working or not. Best way is to store key code for each key in map for e.g. Enter = 13 – Braj Mar 15 '14 at 00:56
  • Make a list of these key codes and use it later in `keyPressed` or `keyReleased` to get the button from map. – Braj Mar 15 '14 at 01:02
  • A better soliton, which would solve the focus related issues of KeyListener, would be to create an Action for each key and associated it with each JButton AND key binding....but that's a lot of work, considering you need to take into consideration not only the normal state but the shift state as well... – MadProgrammer Mar 15 '14 at 01:39
  • Thanks, Yes you don't need to add any listener on buttons. Just add Key listener on text box and change the background color of button based on key pressed/released. – Braj Mar 15 '14 at 01:43
  • It means user wants same behavior on mouse click also? We can add key listener on Panel or JFrame itself if focus is lost to come back on text box. – Braj Mar 15 '14 at 01:50
  • @Braj not sure but is not doing anything.. I typed but it didn't highlight the buttons that I pressed.. – valbu17 Mar 15 '14 at 02:06
  • See full code in my post if you are getting errors. – Braj Mar 15 '14 at 02:13
  • Just remove it. Not used any where. It will work fine for numbers and alphabets. Read my previous comments to solve it for rest keys. – Braj Mar 15 '14 at 02:18
  • ok, not sure why but still not working.. Im not sure what Im missing..? I just copied and pasted the code you posted but it doesn't change the colour.. – valbu17 Mar 15 '14 at 02:23
  • @Braj is working on windows but not on mac.. why is that?? maybe that has been the problem all this time.. – valbu17 Mar 15 '14 at 02:41
  • Yes It can be a issue. I am not sure. Let me investigate. [Read it here](http://stackoverflow.com/questions/9976166/java-difference-in-key-detection-between-windows-and-mac) – Braj Mar 15 '14 at 02:45
  • @Braj can you give me an example for the other keys like enter or backspace.?? – valbu17 Mar 15 '14 at 03:04
  • Every thing is working file other than ~ + : " ? characters because it works along with shift key that is not handled in code right now. I have updated my answer. Please have a look. Should I code for you to handle keys with Shift key? – Braj Mar 15 '14 at 11:49
  • I have handled ~ + : " ? characters also and have updated in my answer. but still I Have a question that how are you handling other characters such as ` = / ; ' etc.? This **[Link](http://stackoverflow.com/questions/1248510/convert-string-to-keyevents?answertab=votes#tab-top)** might help you to understand key codes. – Braj Mar 15 '14 at 12:14
  • @Braj Works like charm.. Thanks a lot.. now, I will do my digging to see why it doesn't for mac.. let me know if you add the shift.. – valbu17 Mar 15 '14 at 14:44
  • I have already updated the code for shift key also. My question is about other characters such as ` = / ; ' etc. these are not used at all in your typing application. – Braj Mar 15 '14 at 14:52
  • @Braj not thats fine.. they are not included in the app for now.. but if you want to do it to make it more complete feel free. – valbu17 Mar 15 '14 at 18:24
  • OK let me complete it. I will come back with complete code. – Braj Mar 15 '14 at 18:30