0

I am trying to create a command list for ProcessBuilder running a program depending on what is selected on GUI.

I have 10 buttons and i need each button to return and add a String to List when toggled and remove it while not toggled.

I was trying to add value to the list when .isSelected and remove it when disabled. But i think this is not a good approach.

Does anyone have any ideas ?

EDIT: Is adding all buttons to ToggleGroup and use Switch is a valid solution ?

1
  • If you add all the buttons to a toggle group, only one button can be selected at a time, which is probably not what you want... Commented Mar 7, 2016 at 19:14

3 Answers 3

2

A simple implementation is:

public class MyApplication extends Application {

    private final List<String> commands = new ArrayList<>();

    @Override
    public void start(Stage primaryStage) {
        VBox commandToggles = new VBox();
        commandToggles.getChildren().add(createCommandToggle("Command 1", "exec1"));
        commandToggles.getChildren().add(createCommandToggle("Command 2", "exec2"));
        commandToggles.getChildren().add(createCommandToggle("Command 3", "exec3"));
        // ...

        Button runButton = new Button("Run");
        runButton.setOnAction(e -> {
            ProcessBuilder pb = new ProcessBuilder(commands);
            // ...
        });

        // ...

    }

    private ToggleButton createCommandToggle(String text, String executable) {
        ToggleButton button = new ToggleButton(text);
        button.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
            if (isSelected) { 
                commands.add(executable);
            } else {
                commands.remove(executable);
            }
        }
        return button ;
    }
}

As @Valette_Renoux suggests, you can refine this by encapsulating the text for the button and the executable command in an enum, and replace the list with an EnumSet. This makes building the toggle buttons a little less repetitive (though you might need just a little more work in the runButton handler to extract the commands).

Sign up to request clarification or add additional context in comments.

Comments

0

I would use an EnumSet. Define one enum constant for each Button (with a name property to display), then have the togglebutton add or remove the enum from the set. This way the button class only needs to have as arguments to the constructor which enum constant it is for, and the EnumSet.

It is then easy to add arguments to the command line by iterating over the EnumSet.

The advantage of this solution is that the Button class can be small and generic (adding one more command line option will be just adding a new CommandOptionButton(Options.VERBOSE, optionsSet), and the set is much better algorithmically than adding and removing Strings from a list.

Comments

0

Thanks guys I went for something like this:

public ToggleButton getBuildBtn() {

        final ToggleButton button = new ToggleButton("Build");

        button.setStyle("-fx-font: 15 verdana; -fx-base: " + buttonsColor + ";");
        button.setOnAction(event -> selectedhandler(button, "build"));
        return button;
}

private void selectedhandler(final ToggleButton button, String command) {
    if (button.isSelected()) {
        button.setStyle("-fx-base: #00ff0f;");
        commands.add(command);
    } else {
        button.setStyle("-fx-base: " + buttonsColor + ";");
        commands.remove(command);
    }
}

I will think about enumSet while refactoring. Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.