controlsfx icon indicating copy to clipboard operation
controlsfx copied to clipboard

Validate SegmentedButton

Open NeoCortex97 opened this issue 1 year ago • 1 comments

I need to validate that one option of the segmented Buttons is selected, but I do not see any way how I could do this currently.

Do you have any suggestions?

NeoCortex97 avatar Jun 06 '23 23:06 NeoCortex97

You should be able to adapt the following demo to your validation scheme:

import java.util.List;
import java.util.stream.Collectors;

import org.controlsfx.control.SegmentedButton;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Demonstrates how to get which button(s) are selected in two 
 * scenarios: 
 * 
 * 1. Multiple choice (i.e., toggleGroup set to null)
 * 2. More conventional single choice.
 * 
 * @author Paul Furbacher
 *
 */
public class SegmentedButtonExploration extends Application {

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

        final VBox root = new VBox(24);
        root.setPadding(new Insets(12));
        root.getChildren().addAll(
            createSegmentedButtonPanel(true), 
            new Separator(), 
            createSegmentedButtonPanel(false));

        final Scene scene = new Scene(root);
        stage.setTitle("SegmentedButton Demo");
        stage.setScene(scene);
        stage.setResizable(false);
        
        stage.show();
    }

    public VBox createSegmentedButtonPanel(boolean isMultiSelect) {
        final Label selectedStateLabel = new Label();
        final ToggleButton tb1 = new ToggleButton("Red");
        final ToggleButton tb2 = new ToggleButton("Green");
        final ToggleButton tb3 = new ToggleButton("Blue");

        final SegmentedButton segmentedButton = new SegmentedButton();
        segmentedButton.getButtons().addAll(tb1, tb2, tb3);
        if (isMultiSelect) {
            segmentedButton.setToggleGroup(null);
        }
        if (!isMultiSelect) {
            segmentedButton.getToggleGroup().selectedToggleProperty()
                .addListener((obs, oldVal, newVal) -> {
                    selectedStateLabel.setText(
                        newVal != null
                            ? ((ToggleButton) newVal).getText()
                            : "none selected");
                });
        } else {
            final StringBinding binding = Bindings.createStringBinding(() -> {
                String result = List.of(tb1, tb2, tb3).stream()
                    .filter(tb -> tb.isSelected())
                    .map(tb -> tb.getText())
                    .collect(Collectors.joining(", "));
                return result.isEmpty() ? "no buttons selected" : result;
            }, tb1.selectedProperty(), tb2.selectedProperty(), tb3.selectedProperty());

            selectedStateLabel.textProperty().bind(binding);
        }
        
        final HBox hbox = new HBox(18);
        hbox.setAlignment(Pos.CENTER_LEFT);
        final Region spacer = new Region();
        hbox.getChildren().addAll(
            new Label(isMultiSelect ? "no toggle group: " : "with default toggle group: "),
            spacer, 
            segmentedButton);
        HBox.setHgrow(spacer, Priority.ALWAYS);

        final VBox vbox = new VBox(12);
        vbox.getChildren().addAll(hbox, selectedStateLabel);
        return vbox;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        launch(args);
    }

}

pfurbacher avatar Jun 12 '23 19:06 pfurbacher