WalidY
WalidY

Reputation: 21

setText on JLabel doesn't work inside KeyPressed method

Whenever I try to use setText inside the KeyPressed method it doesn't work, altough when I use it in a different method (initComponents) inside the same class it works there.

Feel free to ask any other code if it's necessary!

This is the KeyPressed method where it doesn't work:

    @Override
public void keyTyped(KeyEvent e) {
    char typed = e.getKeyChar();

    if (Character.isLetter(typed) && r.getHuidigeKolom() < r.getAantalLetters()) {
        typed = Character.toUpperCase(typed);
        r.getLetters()[r.positie(r.getHuidigeRij(), r.getHuidigeKolom())].setText(typed + "");
        r.getLetters()[r.positie(r.getHuidigeRij(), r.getHuidigeKolom())].setBackground(Color.blue);

        if (r.getHuidigeKolom() == 0) {
            for (int i = 1; i < r.getAantalLetters(); i++) {
                r.getLetters()[r.positie(r.getHuidigeRij(), i)].setText(".");
                r.getLetters()[r.positie(r.getHuidigeRij(), i)].setBackground(Color.blue);
            }

            r.volgendeKolom(true);

            if (r.getHuidigeKolom() < r.getAantalLetters()) {
                r.getLetters()[r.positie(r.getHuidigeRij(), r.getHuidigeKolom())].setBackground(hoverKleur);
            }

            if (typed == 10 && r.getHuidigeKolom() >= r.getAantalLetters()) {   //typed 10 is ENTER
                this.controle();
            }

            if (typed == 8 && r.getHuidigeKolom() > 0) {    //typed 8 is BACKSPACE
                this.eentjeTerug();
            }
        }
    }
}

The setText method does work in this method:

    private void initComponents(String woord) {
    this.setLayout(new GridLayout(r.getAantalPogingen(), r.getAantalLetters(), 2, 2));
    for (int i = 0; i < r.getAantalPogingen() * r.getAantalLetters(); i++) {
        r.getLetters()[i] = new Label();
        r.getLetters()[i].setBackground(Color.white);
        r.getLetters()[i].setForeground(Color.black);
        r.getLetters()[i].setAlignment(Label.CENTER);
        r.getLetters()[i].setFont(new Font("Groot", Font.BOLD, 48));
        this.add(r.getLetters()[i]);
    }

    for (int i = 0; i < 5; i++) {
        r.getLetters()[i].setText(woord.charAt(i) + "");
        r.getLetters()[i].setBackground(Color.blue);
    }

    r.setHuidigeKolom(0);
    r.setHuidigeRij(0);
}

I really appreciate any help you can provide.

Upvotes: 0

Views: 122

Answers (1)

Nick Rippe
Nick Rippe

Reputation: 6465

Without a MCTRE it's going to be a little difficult to pinpoint the exact cause of your problem, but I'm guessing that the root of your problem is that you're using Key Listeners instead of Key Bindings.

A KeyListener is very picky about what component is focused, which is likely the problem you're running into. It won't fire unless the component it was added to has the application's focus (so it's not ideal to use with containers). Here's a quick example of how to use a Key Binding:

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

public class KeyBindings extends Box{
    public KeyBindings(){
        super(BoxLayout.Y_AXIS);
        final JTextPane textArea = new JTextPane();
        textArea.insertComponent(new JLabel("Text"));
        add(textArea);

        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("New Text");
            }};
         String keyStrokeAndKey = "control SPACE";
         KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
         textArea.getInputMap().put(keyStroke, keyStrokeAndKey);
         textArea.getActionMap().put(keyStrokeAndKey, action);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new KeyBindings());
        frame.pack();
        frame.setVisible(true);
    }
}

Upvotes: 1

Related Questions