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.
I’m with trashgod, I’d avoid
KeyListenersif 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
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…