I am making a java gui keyboard as practice

The keyboard itself works well and is made out of JButtons

It changes colors when a certain key is pressed and then releseased

I have two questions:

Is there a simple way to listen for tab being pressed?

Is there a way to shorten down my changeButton function?

            import java.awt.Color;
            import java.awt.Dimension;
            import java.awt.GridBagConstraints;
            import java.awt.GridBagLayout;
            import java.awt.event.KeyListener;
            import java.awt.event.KeyEvent;
            import javax.swing.JButton;
            import javax.swing.JPanel;

            public class Keyboard implements KeyListener{
                private static final String[] firstKeys = {"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"};
                private static final String[] secondKeys = {"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[",   "]", "\\"};
                private static final String[] thirdKeys = {"Capslock", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"};
                private static final String[] fourthKeys = {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"};
                private JButton[] row1Buttons, row2Buttons, row3Buttons, row4Buttons;
                private JButton spacebar, arrowUp, arrowLeft, arrowDown, arrowRight; //Buttons will hold keys
                private JPanel row1, row2, row3, row4, keyboard;

                public Keyboard()
                { 
                    row1 = new JPanel();
                    row1Buttons = new JButton[firstKeys.length];

                    for(int count = 0; count < firstKeys.length; count++)
                    {
                        row1Buttons[count] = new JButton(firstKeys[count]);
                        row1Buttons[count].addKeyListener(this);
                        row1.add(row1Buttons[count]);
                    }

                    row2 = new JPanel();
                    row2Buttons = new JButton[secondKeys.length];

                    //row2Buttons[0] = new JButton(secondKeys[0]); //tab
                    //row2Buttons[0].setFocusTraversalKeysEnabled(false); //Doesn't work
                    //row2Buttons[0].addKeyListener(this);
                    //row2.add(row2Buttons[0]);

                    for(int count = 0; count < secondKeys.length; count++)
                    {
                        row2Buttons[count] = new JButton(secondKeys[count]);
                        row2Buttons[count].addKeyListener(this);
                        row2.add(row2Buttons[count]);
                    }

                    row3 = new JPanel();
                    row3Buttons = new JButton[thirdKeys.length];

                    for(int count = 0; count < thirdKeys.length; count++)
                    {
                        row3Buttons[count] = new JButton(thirdKeys[count]);
                        row3Buttons[count].addKeyListener(this);
                        row3.add(row3Buttons[count]);
                    }

                    row4 = new JPanel();
                    row4Buttons = new JButton[fourthKeys.length];

                    for(int count = 0; count < fourthKeys.length; count++)
                    {
                        row4Buttons[count] = new JButton(fourthKeys[count]);
                        row4Buttons[count].addKeyListener(this);
                        row4.add(row4Buttons[count]);
                    }

                    keyboard = new JPanel(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();

                    c.gridx = 0;
                    c.gridy = 0;
                    keyboard.add(row1, c);

                    c.gridx = 0;
                    c.gridy = 1;
                    keyboard.add(row2, c);

                    c.gridx = 0;
                    c.gridy = 2;
                    keyboard.add(row3, c);

                    c.gridx = 0;
                    c.gridy = 3;
                    keyboard.add(row4, c); 

                    c.gridx = 2;
                    c.gridy = 3;
                    arrowUp = new JButton("↑");
                    arrowUp.addKeyListener(this);
                    keyboard.add(arrowUp, c);

                    c.gridx = 0;
                    c.gridy = 4;
                    spacebar = new JButton("");
                    spacebar.setPreferredSize(new Dimension(250, 25));
                    spacebar.addKeyListener(this);
                    keyboard.add(spacebar, c);

                    c.gridx = 1;
                    c.gridy = 4;
                    arrowLeft = new JButton("←");
                    arrowLeft.addKeyListener(this);
                    keyboard.add(arrowLeft, c);

                    c.gridx = 2;
                    c.gridy = 4;
                    arrowDown = new JButton("↓");
                    arrowDown.addKeyListener(this);
                    keyboard.add(arrowDown, c);

                    c.gridx = 3;
                    c.gridy = 4;
                    arrowRight = new JButton("→");
                    arrowRight.addKeyListener(this);
                    keyboard.add(arrowRight, c);
                }

                public void keyTyped(KeyEvent event)
                {
                    keyPressed(event);
                }

                public void keyPressed(KeyEvent event)
                {
                    changeButton(event, Color.DARK_GRAY);
                }

                public void keyReleased(KeyEvent event)
                {
                    changeButton(event, Color.CYAN);
                }

                private void changeButton(KeyEvent event, Color color)
                {
                            for(int count = 0; count < firstKeys.length; count++)
                    {
                        if(KeyEvent.getKeyText(event.getKeyCode()).equals(firstKeys[count]))
                        {
                            row1Buttons[count].setBackground(color);
                        }
                    }
                    for(int count = 0; count < secondKeys.length; count++)
                    {
                        if(KeyEvent.getKeyText(event.getKeyCode()).equals(secondKeys[count]))
                        {
                            row2Buttons[count].setBackground(color);
                        }
                    }
                    for(int count = 0; count < thirdKeys.length; count++)
                    {
                        if(KeyEvent.getKeyText(event.getKeyCode()).equals(thirdKeys[count]))
                        {
                            row3Buttons[count].setBackground(color);
                        }
                    }
                    for(int count = 0; count < fourthKeys.length; count++)
                    {
                        if(KeyEvent.getKeyText(event.getKeyCode()).equals(fourthKeys[count]))
                        {
                            row4Buttons[count].setBackground(color);
                        }
                    }

                    if(event.getKeyCode() == KeyEvent.VK_EQUALS) //IS there a way that I could shorten this code? ||||||||
                        row1Buttons[12].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_MINUS)
                        row1Buttons[11].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_BACK_QUOTE)
                        row1Buttons[0].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_TAB)
                        row2Buttons[0].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_OPEN_BRACKET)
                        row2Buttons[11].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_CLOSE_BRACKET)
                        row2Buttons[12].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_BACK_SLASH)
                        row2Buttons[13].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_CAPS_LOCK)
                        row3Buttons[0].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_SEMICOLON)
                        row3Buttons[10].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_QUOTE)
                        row3Buttons[11].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_COMMA)
                        row4Buttons[8].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_PERIOD)
                        row4Buttons[9].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_SLASH)
                        row4Buttons[10].setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_SPACE)
                        spacebar.setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_UP)
                        arrowUp.setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_LEFT)
                        arrowLeft.setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_DOWN)
                        arrowDown.setBackground(color);

                    if(event.getKeyCode() == KeyEvent.VK_RIGHT)
                        arrowRight.setBackground(color);
                }

                public JPanel getBoard()
                {
                    return keyboard;
                }
            }

listen for tab being pressed?

Print out the event passed to the listener and see what is passed to it when tab is pressed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.