Pergunta

The docs say the button should fire for clicks, keypress, and touch. It doesn't seem to work. Some code to play with.

public class FireButtonTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextArea ta = new TextArea();

        Button btn1 = new Button();
        btn1.setText("btn1");
        btn1.setOnAction((ActionEvent event) -> {
            ta.setText(ta.getText()+"btn1 fired \n");
        });

        Button btn2 = new Button();
        btn2.setText("btn2");
        btn2.setOnAction((ActionEvent event) -> {
            ta.setText(ta.getText()+"btn2 fired \n");
        });

        btn2.setOnKeyReleased((KeyEvent ke) -> {
            if (ke.getCode() == KeyCode.ENTER) {
                ta.setText(ta.getText()+"btn2 key event -> ");
                btn2.fire();
            }
        });

        //?? can't test this
        btn2.setOnTouchReleased((TouchEvent te) -> {
            if (te.getTouchCount() == 1){
                ta.setText(ta.getText()+"btn2 touch event -> ");
                btn2.fire();
            }
        });

        VBox root = new VBox(5);
        root.getChildren().addAll(btn1, btn2, ta);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Button Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Why can't I just press enter when the button is focused?

Foi útil?

Solução

Doh! I guess you have to use the space bar. Here's the source.

<!-- language: java -->

protected static final List<KeyBinding> BUTTON_BINDINGS = new ArrayList<KeyBinding>();
     static {
             BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_PRESSED, PRESS_ACTION));
             BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_RELEASED, RELEASE_ACTION));
     }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top