Giter VIP home page Giter VIP logo

Comments (1)

pfurbacher avatar pfurbacher commented on June 12, 2024

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);
    }

}

from controlsfx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.