0

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.

7
  • What is <code>r<\code>? Mar 6, 2014 at 14:42
  • 3
    please whats goal, nothing cleaver without your SSCCE or MCVE or MCTRE, short, runnable, compilable with hardcoded value in local variables
    – mKorbel
    Mar 6, 2014 at 14:42
  • Do [r.positie(r.getHuidigeRij(), i)] in above code and [i] in the below code next to .setText() have the same value?
    – dARKpRINCE
    Mar 6, 2014 at 14:55
  • "Feel free to ask any other code if it's necessary!" - provide a runnable example. See How to create a Minimal, Complete, Tested and Readable example Mar 6, 2014 at 15:05
  • This is a word guessing game called Lingo where you guess a word and you only have the first letter shown like this here: puu.sh/7l4w5.jpg Now whenever I type it should fill up the board like this: puu.sh/7l4Aw.png r.getLetters()[numberHere] is the place of a box where a letter is shown, in this case it can be filled from 0-24 because its a 5x5 board.
    – WalidY
    Mar 6, 2014 at 15:22

1 Answer 1

1

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);
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.