Search code examples
javaswingjbuttonbackground-colorjtextarea

How can i fix the Java Typing Tutor program's KeyBoard button's background Color in Class KeyBoard given below?


KeyBoard class:

package keyBoardTypingTutor;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class KeyBoard {
    private final JFrame f = new JFrame("Keyboard");
    private final JPanel keyboard = new JPanel();
    private Color buttonBackground;
    private static JButton[][] b = new JButton[5][40];

    private static final String[][] key = { 
        {"~","1","2","3","4","5","6","7","8","9","0","-","=","Backspace"},
        {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"},
        {"Caps","A","S","D","F","G","H","J","K","L",";","'","Enter"},
        {"Shift","Z","X","C","V","B","N","M",",",".","/","\u2191"},
        {" ","\u2190","\u2193","\u2192"}
        };

    public KeyBoard() {
        // TODO Auto-generated constructor stub
        keyboard.setLayout(new GridBagLayout());
        Insets zeroInsets = new Insets(0, 0, 0, 0);
        Font monospaced = new Font("Monospaced",Font.PLAIN, 12);

        JPanel pRow;

        GridBagConstraints cRow = new GridBagConstraints();
        GridBagConstraints cButton = new GridBagConstraints();
        cRow.anchor = GridBagConstraints.WEST;
        cButton.ipady = 21;

        for(int row=0; row<key.length; row++){
            pRow = new JPanel(new GridBagLayout());
            cRow.gridy = row;

            for(int col=0; col<key[row].length; col++){
                switch(key[row][col]){
                case "Backspace":
                    cButton.ipadx = 0;
                    break;
                case "Tab":
                    cButton.ipadx = 17;
                    break;
                case "Caps":
                    cButton.ipadx = 14;
                    break;
                case "Enter":
                    cButton.ipadx = 27;
                    break;
                case "Shift":
                    cButton.ipadx = 27;
                    break;
                case "/":
                    cButton.insets = new Insets(0, 0, 0, 29);
                    break;
                case " ":
                    cButton.ipadx = 247;
                    cButton.insets = new Insets(0, 192, 0, 73);
                    break;
                default:
                    cButton.ipadx = 7;
                    cButton.insets = zeroInsets;
                    break;
                }

                b[row][col] = new JButton(key[row][col]);
                b[row][col].setFont(monospaced);
                b[row][col].setFocusable(false);
                pRow.add(b[row][col], cButton);
//              b[row][col].setForeground(Color.WHITE);
//              b[row][col].setBackground(Color.DARK_GRAY);
                buttonBackground = b[row][col].getBackground();

            }
            keyboard.add(pRow, cRow);
        }
        f.add(keyboard);
    }



    public void pressedButton(int row, int col){
        b[row][col].setBackground(Color.BLUE);
        b[row][col].setForeground(Color.WHITE);
        System.out.print("here");
    }
    public void releasedButton(int row, int col){
        b[row][col].setBackground(buttonBackground);
        b[row][col].setForeground(Color.BLACK);
    }

    public void launch(){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.pack();
        f.setResizable(false);
        f.setLocationRelativeTo(null);
    }
    public JPanel getKeyboard() {
        return keyboard;
    }


    public JButton[][] getB() {
        return b;
    }
}

TextAreaFrame class-->>

package keyBoardTypingTutor;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

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

public class TextAreaFrame {
    private JTextArea textArea;
    private JPanel areaPanel;
    private JFrame frame;
    private KeyBoard keyBoard;
    private JButton[][] b;

    public TextAreaFrame() {
        // TODO Auto-generated constructor stub
        areaPanel = new JPanel();
        keyBoard = new KeyBoard();

        textArea = new JTextArea(7, 50);
        textArea.setLineWrap(true);
        textArea.setFont(new Font("SansSerif", Font.PLAIN, 18));

        areaPanel.add(textArea);
        KeyHandler handler = new KeyHandler();
        textArea.addKeyListener(handler);
    }

    private class KeyHandler implements KeyListener {
        public void keyPressed(KeyEvent event) {
            // System.out.print(event.getKeyCode());
            switch (event.getKeyCode()) {
            case KeyEvent.VK_0:
                System.out.print(event.getKeyCode());
                keyBoard.pressedButton(0, 10);
                break;
            case KeyEvent.VK_1:
                keyBoard.pressedButton(0, 1);
                break;
            case KeyEvent.VK_2:
                keyBoard.pressedButton(0, 2);
                break;
            case KeyEvent.VK_3:
                keyBoard.pressedButton(0, 3);
                break;
            case KeyEvent.VK_4:
                keyBoard.pressedButton(0, 4);
                break;
            case KeyEvent.VK_5:
                keyBoard.pressedButton(0, 5);
                break;
            case KeyEvent.VK_6:
                keyBoard.pressedButton(0, 6);
                break;
            case KeyEvent.VK_7:
                keyBoard.pressedButton(0, 7);
                break;
            case KeyEvent.VK_8:
                keyBoard.pressedButton(0, 8);
                break;
            case KeyEvent.VK_9:
                keyBoard.pressedButton(0, 9);
                break;
            }

        }

        public void keyReleased(KeyEvent event) {
            // System.out.print(event.getKeyCode());
            switch (event.getKeyCode()) {
            case KeyEvent.VK_0:
                keyBoard.releasedButton(0, 10);
                break;
            case KeyEvent.VK_1:
                keyBoard.releasedButton(0, 1);
                break;
            case KeyEvent.VK_2:
                keyBoard.releasedButton(0, 2);
                break;
            case KeyEvent.VK_3:
                keyBoard.releasedButton(0, 3);
                break;
            case KeyEvent.VK_4:
                keyBoard.releasedButton(0, 4);
                break;
            case KeyEvent.VK_5:
                keyBoard.releasedButton(0, 5);
                break;
            case KeyEvent.VK_6:
                keyBoard.releasedButton(0, 6);
                break;
            case KeyEvent.VK_7:
                keyBoard.releasedButton(0, 7);
                break;
            case KeyEvent.VK_8:
                keyBoard.releasedButton(0, 8);
                break;
            case KeyEvent.VK_9:
                keyBoard.releasedButton(0, 9);
                break;
            }
        }

        public void keyTyped(KeyEvent event) {
            // System.out.print(event.getKeyCode());

        }
    }

    public JPanel getAreaPanel() {
        return areaPanel;
    }

    // public void launch(){
    // frame = new JFrame("Area");
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // frame.setVisible(true);
    // frame.setSize(800, 300);
    // frame.add(areaPanel);
    // }
    // public static void main(String[] args){
    // TextAreaFrame f = new TextAreaFrame();
    // f.launch();
    // }
}

LabelFrame class-->>

package keyBoardTypingTutor;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class LabelFrame {
    private JLabel label;
    private JPanel panel;

    public LabelFrame() {
        // TODO Auto-generated constructor stub
        String text = "A quick Brown Fox Jumps over the Lazy Dog. Programmers are mentalzz.";
        panel = new JPanel();

        label = new JLabel(text);
        label.setFont(new Font("SansSerif", Font.PLAIN, 18));
        label.setHorizontalTextPosition(SwingConstants.LEFT);

        panel.add(label);
    }

    public JPanel getLabelPanel() {
        return panel;
    }

    public JLabel getLabel() {
        return label;
    }
}

KeyBoardTest class-->>

package keyBoardTypingTutor;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class KeyBoardTest extends JFrame {
    private KeyBoard keyBoard;
    private TextAreaFrame textAreaFrame;
    private LabelFrame labelFrame;

    public KeyBoardTest() {
        // TODO Auto-generated constructor stub
        keyBoard = new KeyBoard();
        textAreaFrame = new TextAreaFrame();
        labelFrame = new LabelFrame();

        add(labelFrame.getLabelPanel(), BorderLayout.PAGE_START);
        add(textAreaFrame.getAreaPanel(), BorderLayout.CENTER);
        add(keyBoard.getKeyboard(), BorderLayout.PAGE_END);

        // keyBoard.getKeyboard().setBackground(Color.GRAY);
        // labelFrame.getLabelPanel().setBackground(Color.GRAY);
        // labelFrame.getLabel().setForeground(Color.WHITE);
        // textAreaFrame.getAreaPanel().setBackground(Color.GRAY);
    }

    public static void main(String[] args) {
        KeyBoardTest test = new KeyBoardTest();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setVisible(true);
        test.pack();
        test.setResizable(false);
        test.setLocationRelativeTo(null);
        // try {
        // UIManager
        // .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        // SwingUtilities.updateComponentTreeUI(test);
        // } catch (Exception e) {
        // // TODO: handle exception
        // e.printStackTrace();
        // }

    }
}

In this program, When i press a button in the Physical keyboard, the TextAreaFrame class's KeyEventListener's method keyPressed() and keyReleased() invokes KeyBoard class's pressedButton() and releasedButton() method where i want to change the Button's background color that is pressed currently. But in this program the color is not changing. I have waste a lot time to solve this problem but unable to fix the problem.


Solution

  • You're creating two instances of Keyboard, one in KeyBoardTest which is been displayed on the screen and one in TextAreaFrame which is is not.

    public class KeyBoardTest extends JFrame {
        private KeyBoard keyBoard;
        private TextAreaFrame textAreaFrame;
        private LabelFrame labelFrame;
    
        public KeyBoardTest() {
            // TODO Auto-generated constructor stub
            // This is the keyboard that's on the screen
            keyBoard = new KeyBoard();
            textAreaFrame = new TextAreaFrame();
            labelFrame = new LabelFrame();
    
            add(labelFrame.getLabelPanel(), BorderLayout.PAGE_START);
            add(textAreaFrame.getAreaPanel(), BorderLayout.CENTER);
            add(keyBoard.getKeyboard(), BorderLayout.PAGE_END);
    
            // keyBoard.getKeyboard().setBackground(Color.GRAY);
            // labelFrame.getLabelPanel().setBackground(Color.GRAY);
            // labelFrame.getLabel().setForeground(Color.WHITE);
            // textAreaFrame.getAreaPanel().setBackground(Color.GRAY);
        }
    
     //...
    
    public class TextAreaFrame {
    private JTextArea textArea;
    private JPanel areaPanel;
    private JFrame frame;
    private KeyBoard keyBoard;
    private JButton[][] b;
    
    public TextAreaFrame() {
        // TODO Auto-generated constructor stub
        areaPanel = new JPanel();
        // This one is not, but is the instance you are trying to
        // change...
        keyBoard = new KeyBoard();
    

    Instead, pass the instance of KeyBoard which on the screen TextAreaFrame

    public class KeyBoardTest extends JFrame {
    
        private KeyBoard keyBoard;
        private TextAreaFrame textAreaFrame;
        private LabelFrame labelFrame;
    
        public KeyBoardTest() {
            // TODO Auto-generated constructor stub
            keyBoard = new KeyBoard();
            textAreaFrame = new TextAreaFrame(keyBoard);
            labelFrame = new LabelFrame();
    
    //...
    
    public class TextAreaFrame {
    
        private JTextArea textArea;
        private JPanel areaPanel;
        private JFrame frame;
        private KeyBoard keyBoard;
        private JButton[][] b;
    
        public TextAreaFrame(KeyBoard keyBoard) {
            // TODO Auto-generated constructor stub
            areaPanel = new JPanel();
            this.keyBoard = keyBoard;
    

    Personally, I think you're driving yourself into a deep hole with KeyListener. My personal approach might be to create a key binding for each key stroke your keyboard supports and simply inject that value into the JTextArea and notify the KeyBoard, but that's just me