2

Get letter pressed from JButton when pressed

  public class ButtonDisabler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btnGetText = (JButton) e.getSource();
        char charLetterPressed;            
        charLetterPressed=(btnGetText.getText().charAt(1));

        btnGetText.setEnabled(false);
    }
}

Then use that letter and compare it to a string, then display the letter only if found into a JLabel

 char charChkWord;
     StringBuffer word = new StringBuffer();
    for (int i = 0; i < strRandomWord.length(); i++) {
        charChkWord = strRandomWord.charAt(i);
        if (charLetterPressed == String.valueOf(charChkWord)) {
            lblWord.setText(word.append(charChkWord).toString());
        }
    }

I'm not sure how to get that letter and compare it to the string.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1645427
  • 43
  • 2
  • 7

4 Answers4

1

I can't recomend KeyListener. Instead, let each button know it's name, as shown below and here.

for (int i = 0; i < 26; i++) {
    String letter = String.valueOf((char) (i + 'A'));
    buttons[i] = new JButton(letter);
    north.add(buttons[i]);
}

Then you can use the contains() method from the String class.

lblWord.getText().contains(button.getText());
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I'm with trashgod, I'd avoid KeyListeners if you can.

I'd also set the text of the button to the character you want to use and/or set the name of the button as well

JButton btnA = new JButton("A");
btnA.setName("A");

This would allow you to make a choice over how you want to display the text on the button while providing you a means for providing additional information that might be more useful to you...

JButton button = (JButton) evt.getSource();
String text = button.getText();
// If you wanted to use the name of the button instead...
String name = button.getName();

// You would use this if you need part of the text...
char charPressed = Character.toLowerCase(text.charAt(0)); 
// You could to this to convert the character to a String for easier
// comparison...
String strCharPressed = Character.toString(text.charAt(0)).toLowerCase(); 

// A sample
String word = "This is a test";

// Finds the first occurrence of the character in the String...
// Comparison is case sensitive...
// If indexOf > -1 then the word contains the character
int indexOf = word.toLowerCase().indexOf(charPressed);
// Or, you just check to see if it is contained in the word...
boolean contains = word.toLowerCase().contains(Character.toString(charPressed));

System.out.println("indexOf = " + indexOf);
System.out.println("contains = " + contains);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can make char charLetterPressed; out of method scope.

You don't need String.valueOf(charChkWord) just use charLetterPressed == charChkWord.

In the if scope use break; to exit for loop after setting a label text.

Roman C
  • 49,761
  • 33
  • 66
  • 176
0

Use the following code to get the KeyPressed

class MyKeyListener extends KeyAdapter {
  public void keyPressed(KeyEvent evt) {

     Char chr = evt.getKeyChar();

    }
  }

Now once you have the key pressed, to convert it into String, do the following....

String s = new String(chr);

if(s.equals(charChkWord)){

   // Do something...

}else{

   // Do something...

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75