titledPaneのJavaFXのチェックボックスのチェックを値を取得します。

maryem neyli:

私はTitledPanes、各TitledPaneでアコーディオンを作成するJavaFXのコードを持っているので、チェックボックスを持っています: TitledPanesとチェックボックスとアコーディオン

すなわち:私の質問があるので、ボタンのクリック後に、これらのチェックボックスの値を取得する方法がある私は、特定のチェックボックスを選択し、私はボタンをクリックしたときには、すべての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);
    }
} 
James_D:

私は強く、このようなアプリケーションに適したオブジェクトモデルの構築をお勧めします。あなたのそれぞれは、TitledPane(中のキーとして使用される文字列に依存してpaths、マップ)PathHttpMethodOperationだから私はそれらのデータをカプセル化するクラスで始まると思います。

私はこれを呼ばれてきましたRequestが、それは最も適切な名前ではないかもしれません。

import java.util.Objects;

import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;

public class Request {

    private final String name ;
    private final Path path ;
    private final HttpMethod method ;
    private final Operation operation ;

    public Request(String name, Path path, HttpMethod method, Operation operation) {
        super();
        this.name = name;
        this.path = path;
        this.method = method;
        this.operation = operation;
    }

    public String getName() {
        return name;
    }

    public Path getPath() {
        return path;
    }

    public HttpMethod getMethod() {
        return method;
    }

    public Operation getOperation() {
        return operation;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, path, method, operation);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Request other = (Request) obj;
        return 
                Objects.equals(name, other.name) &&
                Objects.equals(path, other.path) &&
                Objects.equals(method, other.method) &&
                Objects.equals(operation, other.operation) ;
    }

}

あなたはこれらのプロパティがUIで編集可能にする場合は、JavaFXのプロパティの代わりに、プレーンな値とそれらに相当するであろう。

今、あなたは闊歩するAPIによって返されたデータ構造を反復処理とのプレーンなリストを作成することができますRequest秒:

Swagger swagger = new SwaggerParser().read("https://petstore.swagger.io/v2/swagger.json");
Map<String, Path> paths = swagger.getPaths();

List<Request> requests = new ArrayList<>() ;

for (Map.Entry<String, Path> entry : paths.entrySet()) {
    Path path = entry.getValue();
    String pathName = entry.getKey() ;
    for (Map.Entry<HttpMethod, Operation> methodOp : path.getOperationMap().entrySet()) {
        HttpMethod method = methodOp.getKey() ;
        Operation operation = methodOp.getValue() ;
        requests.add(new Request(pathName, path, method, operation));
    }
}

項目はチェックボックスで選択されているのを追跡するために、作成Set選択されたものを保持します:

Set<Request> selectedRequests = new HashSet<>();

その後、チェックボックスを作成するたびに、そのにリスナーを追加しselectedProperty()、対応するを追加または削除するにはRequest、そのセットから:

for (Request req : requests) {
    CheckBox chk = new CheckBox();
    chk.setText(req.getMethod() + " : " + req.getName() + " : " + operation.getSummary());

    chk.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
        if (isNowSelected) {
            selectedRequests.add(req);
        } else {
            selectedRequests.remove(req);
        }
    });

}

あなたが独立して、ユーザのチェックボックスの状態を操作することができるようにしたい場合は、使用することができObservableSet、かつ他の方向にあるチェックボックスの状態を更新するリスナーを追加します。

ObservableSet<Request> selectedRequests = FXCollections.observableSet();

そして

for (Request req : requests) {
    CheckBox chk = new CheckBox();
    chk.setText(req.getMethod() + " : " + req.getName() + " : " + operation.getSummary());

    chk.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
        if (isNowSelected) {
            selectedRequests.add(req);
        } else {
            selectedRequests.remove(req);
        }
    });

    selectedRequests.addListener((Change<? extends Request> c) ->
        chk.setSelected(selectedRequests.contains(req)));

}

この設定で、あなただけの操作で、チェックボックスの状態を変更することができselectedRequests、たとえば、セットを:

Request req = ... ;
// checks the corresponding check box: 
selectedRequests.add(req);
// unchecks the check box:
selectedRequests.remove(req);
// checks all check boxes:
selectedRequests.addAll(requests);
// unchecks all check boxes:
selectedRequests.clear();

これはUI内の他のコントロールのために有用であり得ます。

今すぐあなたのボタンのハンドラであなただけの選択要求のセットを反復処理し、必要なものは何でも行うことができます。

terminer.setOnAction(event -> {
    selectedRequests.forEach(req -> {
        // Do whatever you need with the Request object here
        System.out.println(req.getMethod() + " : " + req.getName() + " : " + req.getOperation().getSummary());
    });
});

すべて一緒にそれを置く、それは次のようになります。

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
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.collections.FXCollections;
import javafx.collections.ObservableSet;
import javafx.collections.SetChangeListener.Change;
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 {

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

        Swagger swagger = new SwaggerParser().read("https://petstore.swagger.io/v2/swagger.json");
        Map<String, Path> paths = swagger.getPaths();

        List<Request> requests = new ArrayList<>();

        for (Map.Entry<String, Path> entry : paths.entrySet()) {
            Path path = entry.getValue();
            String pathName = entry.getKey();
            for (Map.Entry<HttpMethod, Operation> methodOp : path.getOperationMap().entrySet()) {
                HttpMethod method = methodOp.getKey();
                Operation operation = methodOp.getValue();
                requests.add(new Request(pathName, path, method, operation));
            }
        }

        ObservableSet<Request> selectedRequests = FXCollections.observableSet();

        // Create Root Pane.
        VBox root = new VBox();
        root.setPadding(new Insets(20, 10, 10, 10));

        for (Request req : requests) {

            Operation operation = req.getOperation();
            CheckBox chk = new CheckBox();

            chk.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
                if (isNowSelected) {
                    selectedRequests.add(req);
                } else {
                    selectedRequests.remove(req);
                }
            });

            selectedRequests.addListener((Change<? extends Request> c) ->
                chk.setSelected(selectedRequests.contains(req)));

            chk.setText(req.getMethod() + " : " + req.getName() + " : " + operation.getSummary());
            TitledPane firstTitledPane = new TitledPane();
            BorderPane bPane = new BorderPane();
            bPane.setRight(chk);
            firstTitledPane.setGraphic(bPane);
            VBox content1 = new VBox();
            content1.getChildren().add(new Label("Summary:" + operation.getSummary()));
            content1.getChildren().add(new Label("Parameters number: " + operation.getParameters().size()));
            for (Parameter parameter : operation.getParameters()) {
                content1.getChildren().add(new Label(" - " + parameter.getName()));
            }
            content1.getChildren().add(new Label("Responses:"));
            for (Map.Entry<String, Response> r : operation.getResponses().entrySet()) {
                content1.getChildren().add(new Label(" - " + r.getKey() + ": " + r.getValue().getDescription()));
            }
            firstTitledPane.setContent(content1);
            firstTitledPane.setExpanded(false);
            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 -> {
            selectedRequests.forEach(req -> {
                // Do whatever you need with the Request object here
                System.out.println(req.getMethod() + " : " + req.getName() + " : " + req.getOperation().getSummary());
            });
            // this will clear all the checkboxes:
            selectedRequests.clear();
        });

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=341401&siteId=1