Search code examples
javaactionlistenerkeyevent

ActionListener from JTextArea Java


I'm new to Java, and trying to figure out one last thing for my program.

This is the program that I have coded and with the layout it is perfectly fine no problem at all.

enter image description here

Now my program suppose highlight the buttons whenever to press it on the keyboard (NOT BY PRESSING THE BUTTON ON THE SCREEN)

I'm not sure what I have to use since the action that it needs to take is when they type it in the JTextArea. I'm trying to use KeyEvent with KeyPressed but not sure if that is the right thing to do since it doesn't really work.

I can't post my code at the moment here since this is an assignment and I don't want some of my classmate to google and use it if they found it here. (LOL)

As required here is my codes :)

import javax.swing.*; // import all javax.swing
import java.awt.*; // import all java.awt
import java.awt.event.*;

public class Sample extends JFrame implements KeyListener { // start of the
                                                            // class

    private JTextArea field = new JTextArea(10,15); // create an instance of
                                                        // JTextField
    private JPanel mainPanel = new JPanel(); // create an instance of JPanel
    private JPanel TopFieldPan = new JPanel();
    private JPanel MainBtnsPan = new JPanel();
    private JPanel FifthRowPan = new JPanel();

    JPanel fifthBtn = new JPanel();

    private static JButton Space = new JButton("");

    public Sample() { // start of the weather constructor

        Space.setPreferredSize(new Dimension(280, 45));
        fifthBtn.add(Space);


        TopFieldPan.add(field);

        FifthRowPan.setLayout(new BoxLayout(FifthRowPan, BoxLayout.X_AXIS));
        FifthRowPan.add(fifthBtn);
        MainBtnsPan.setLayout(new GridLayout(5, 5, 0, 0));

        MainBtnsPan.add(FifthRowPan);

        mainPanel.add(TopFieldPan);
        mainPanel.add(MainBtnsPan);

        this.add(mainPanel);

        Space.setSelected(true);
        field.addKeyListener(this); // important !!!

        setTitle("Typing Tutor"); // set the title to the frame
        setSize(300, 300); // set the fixed size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLocationRelativeTo(null); 
        setVisible(true); // make it visible

    } // ends of the constructor

    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
            Space.setBackground(Color.green);

        }


    }

    public void keyReleased(KeyEvent evt) {
        Space.setBackground(null);
    }

    public void keyTyped(KeyEvent evt) {
        // TODO Auto-generated method stub

        if(evt.getKeyChar() == 'a' || evt.getKeyChar() ==  'A')
        {
            Space.setBackground(Color.green);
        }
        else if(evt.getKeyChar() == 'b' || evt.getKeyChar() == 'B')
        {
            Space.setBackground(Color.red);
        }       
    }

    public static void main(String[] args) { // start of the main method

        new Sample();

    } // ends of main method

} // ends of class

I tried to simplified the code as much as I could and here is the final one.

So I'm trying to make it when I press a or A it should highlight that space JButton.


Solution

  • Create a map of your buttons and the keys they map to, like this:

    Map<String, JButton> buttonMap = new HashMap<String, Button>();
    

    Then, when you are adding the buttons, add them to the map, like this:

    buttonMap.put(FirstRow[i].toLowerCase(), btn);
    

    Then, add something like this into your KeyTyped:

    public void keyTyped(KeyEvent evt) {
        String keyPressed = new String(""+evt.getKeyChar()).toLowerCase();
        JButton tmp = buttonMap.get(keyPressed);
        if(null != tmp){
            tmp.doClick();
        }
    }
    

    I did this quickly to your code for row 1 and 2. You'll have to play around with it to get it to work for the special keys, but it should get you where you're trying to go.

    I pasted it here, to keep the answer small. http://pastebin.com/t1v8d6Hi