Home > OS >  JavaFX: Button event still executes without having to press the button for a second time
JavaFX: Button event still executes without having to press the button for a second time

Time:09-18

my problem is the following:

I have a JavaFX application with a Button called "bindB";

Button bindB = new Button("None");
bindB.setOnAction(event -> {
    bindB.setText("...");
        BindKey.bindKey(scene, bindB);
});

with the text "None". Is this button pressed, his text first changes to "..." and by then calling the method "BindKey.bindKey();", the text will change to the name of the key, the user is pressing on his keyboard. This is the code of the method "BindKey.bindKey();":

public static void bindKey(Scene scene, Button bindB){
    scene.setOnKeyPressed(event -> {
            bindB.setText(String.valueOf(event.getCode()));
        });
    }

As you can see, in the args of the method we give the button "bindB", so that the method knows what button to change the name of, aswell as the current scene.

This code does work, but the problem is, that even after the button was pressed, and its text has already be changed, the text still changes to the name of different keys if you press them afterwards WITHOUT having to press the button a second time.

I thought that you had to end the "setOnAction" event by calling

event.consume();

but that didnt work... So how do I make the buttons text only change, if the button has actually been pressed a second or third time?

Otherwise, the task which the button performs is toggled by EVERY key because technically every key is the toggle key as the task reads the name of the button to know what key is for toggling.

Full code example:

Main class:

public class Main {

    // Initialize GUI
    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.run();
    }
}

GUI class:

public class GUI extends Application {

    public void run() {
        launch();
    }

    @Override
    public void start(Stage window) throws Exception {

        // When closing Window
        window.setOnCloseRequest(event -> {
            exitApplication(window);
        });

        // GridPane
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10,10));
        grid.setVgap(15);
        grid.setHgap(30);

        // Scene
        Scene scene = new Scene(grid, 200, 200);
        window.setScene(scene);

        // Bind Button
        Button bindB = new Button("None");
        GridPane.setConstraints(bindB, 1, 1);
        bindB.setOnAction(event -> {
            bindB.setText("...");
            BindKey.bindKey(scene, bindB);
        });

        // Add to Grid
        grid.getChildren().addAll(bindB);

        // Show Window
        window.show();
    }

    // Provide a clean exit
    private void exitApplication(Stage window){
        window.close();
        Platform.exit();
        System.exit(0);
    }
}

BindKey class:

public class BindKey {

    // Changes Buttons text
    public static void bindKey(Scene scene, Button bindB){
        scene.setOnKeyPressed(event -> {
            bindB.setText(String.valueOf(event.getCode()));
        });
    }
}

CodePudding user response:

I am not to 100% sure if this is the problem, but I think the only thing you have to do is the following:

scene.setOnKeyPressed(event -> {
        bindB.setText(String.valueOf(event.getCode()));
        scene.setOnKeyPressed(null);
    });
}

You just have to remove the Key-Listener from the scene after you set the text. Because otherwise it will listen for keys the entire time.

  • Related