For example, I created a button labeled "1". Whenever this button is pressed, 1 is appended to a textField. However, I can add 1 to a textField simply by typing 1 on my keyboard. When doing so, I'd like by button to get view as if it was pressed instead if a key. I've been thinking thant may be it's possible to manage this issue in this way:
rootNode.setOnKeyTyped(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
textField.appendText(event.getCharacter());
if(event.getCharacter().equals("1")){
// here button should be pressed
}
}
});
Is there some method, that can change appearance of the button? Thanks is advance.
@James_D , You program works correctly, but I connt apply your solution to my program. Maybe it's because I customized my buttons. Have a look at a part of my code:
HashMap<String, Button> buttons = new HashMap<>();
int btnVal = 1;
for(int j = 5 ; j >= 3; j --){
for(int i = 1; i <= 3; i ++){
Button btn = createNumberButton(Integer.toString(btnVal++), inputField, i, j);
rootNode.getChildren().add(btn);
buttons.put(Integer.toString(btnVal), btn);
}
}
rootNode.setOnKeyPressed(event -> {
Button btn = buttons.get(event.getText());
if (btn != null) {
System.out.println(event.getText());
btn.arm();
inputField.appendText(event.getText());
}
});
rootNode.setOnKeyReleased(event -> {
Button btn = buttons.get(event.getText());
if (btn != null) {
btn.disarm();
}
});