获取标题为Pane JAVAFX的复选框的检查值

问题描述 投票:-1回答:1

所以我有一个JavaFX代码,它使用TitledPanes创建一个手风琴,每个TitledPane都有一个复选框:Accordion with TitledPanes and CheckBox

所以我的问题是,单击按钮后,是否有任何方法可以获取那些checkBoxes的值:即:我选择一个特定的复选框,当我单击按钮时,它将为我显示所有checkedBoxes值

这是代码:

    import java.net.MalformedURLException;
import java.util.Map;

import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.parser.SwaggerParser;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GroupOfTitledPane extends Application {

    Stage stage;
    String ppaths[];
    String methods[];
    int i=0;

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

            //URL url= new URL(index.locationTextField.getText());
            //System.out.println(url);
            Swagger swagger = new SwaggerParser().read("https://petstore.swagger.io/v2/swagger.json");
            Map<String, Path> paths = swagger.getPaths(); 
            // Create Root Pane.
            VBox root = new VBox();
            root.setPadding(new Insets(20, 10, 10, 10));
            for (Map.Entry<String, Path> p : paths.entrySet()) {
                Path path = p.getValue();
                Map<HttpMethod, Operation> operations = path.getOperationMap();
                for (java.util.Map.Entry<HttpMethod, Operation> o : operations.entrySet()) {
                    CheckBox chk = new CheckBox();
                  chk.setText((o.getKey()).toString()+" : "+(p.getKey()).toString()+" : "+o.getValue().getSummary());
                TitledPane firstTitledPane = new TitledPane() ;
                BorderPane bPane = new BorderPane();
                 bPane.setRight(chk);
                 firstTitledPane.setGraphic(bPane);
                    VBox content1 = new VBox();
                    System.out.println("===");
                    System.out.println("PATH:" + p.getKey());
                    System.out.println("Http method:" + o.getKey());
                    System.out.println("Summary:" + o.getValue().getSummary());
                    content1.getChildren().add(new Label("Summary:" + o.getValue().getSummary()));
                    System.out.println("Parameters number: " + o.getValue().getParameters().size());
                    content1.getChildren().add(new Label("Parameters number: " + o.getValue().getParameters().size()));
                    for (Parameter parameter : o.getValue().getParameters()) {
                        System.out.println(" - " + parameter.getName());
                        content1.getChildren().add(new Label(" - " + parameter.getName()));
                    }
                    System.out.println("Responses:");
                    content1.getChildren().add(new Label("Responses:"));
                    for (Map.Entry<String, Response> r : o.getValue().getResponses().entrySet()) {
                        System.out.println(" - " + r.getKey() + ": " + r.getValue().getDescription());
                        content1.getChildren().add(new Label(" - " + r.getKey() + ": " + r.getValue().getDescription()));
                    }           
                    firstTitledPane.setContent(content1);
                    root.getChildren().addAll(firstTitledPane);

                }

            }


            ScrollPane scrollPane = new ScrollPane();
            scrollPane.setFitToHeight(true);
            scrollPane.setFitToWidth(true);
            Button terminer = new Button("Terminer");
            root.getChildren().addAll(terminer);
            root.setAlignment(Pos.BOTTOM_RIGHT);
            root.setSpacing(10);
            scrollPane.setContent(root);
            Scene scene = new Scene(scrollPane, 600, 400);
            stage.setScene(scene);
            stage.show(); 
            terminer.setOnAction(event -> {

            });

            }



    public static void main(String[] args) {
    Application.launch(args);
    }
} 
java javafx checkbox accordion buttonclick
1个回答
-1
投票

您已选择方法。在您的代码中boolean checked = chk.isSelected();或者您可以在if语句中使用它

if(chk.isSelected(); {
//do something
}
© www.soinside.com 2019 - 2024. All rights reserved.