【发布时间】:2020-07-02 09:58:57
【问题描述】:
所以我有一个 JavaFX 代码,它使用 TitledPanes 创建一个 Accordion,每个 TitledPane 都有一个复选框:
所以我的问题是有什么方法可以在单击按钮后获取这些复选框的值:即:我选择一个特定的复选框,当我单击一个按钮时,它会提示我所有选中的框值
这里是代码:
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);
}
}
【问题讨论】:
-
一种方法是简单地遍历所有复选框并在每个复选框上调用
isSelected()。可能更好的方法是使用任何类的Set来表示每个TitledPane中的数据,并在创建复选框时向selectedProperty()注册一个侦听器,该侦听器将相关实例添加或删除到或从那套。但是这里确实没有足够的信息来回答这个问题:如果你想要更完整的答案,我建议你创建一个minimal reproducible example。 -
@James_D 所以我有一个 TitledPane ,其中包含一个 BordredPane 包含一个复选框!我试过这段代码:
for( String tst : firstTitledPane.bPane.chk.getText())},但它不接受 bPane(bPane 无法解析或不是字段)知道我将所有变量都标识为类变量 -
作为@James_D 引用的帮助页面...
-
你的模型应该跟上复选框的值。
-
正如@Sedrick 所说,您需要从这里开始使用适当的数据模型。现在所有东西都保存在某种结构中,据我所知,它由一个
Map<String,Path>组成,其中每个Path包含另一个映射,即Map<Path, Operation>,您正在为(我认为)创建一个复选框每个(Path, Operation)对(这是每个TitledPane所代表的内容)。所以我首先创建一个类来表示每个TitledPane中的数据。在执行任何 UI 之前,将您的地图展平为这些对象的List。然后尝试我的第一条评论。
标签: java javafx checkbox accordion buttonclick