Javafx 和 Java

Javafx and Java

我有一个问题,非常感谢任何形式的帮助和回答。

这是我的问题:

我正在与 edmunds api 一起工作。我已经创建了 classes,我可以在我的 classes 对象中解析我的 json 数据。现在,我想将这些对象显示为滚动窗格。为此,我创建了一个 "for" 循环来为每个对象创建一个 HBOX,标签包含我的对象属性的名称。

到现在为止,在 HBOX 和滚动窗格中显示数据是成功的,但它们只是 HBox,我无法select 任何 HBOX 使用。

在我的滚动窗格中,我显示汽车的品牌(如宝马、奥迪等),当我 select 例如宝马的 HBOx 时,我只想显示该品牌的所有车型。

有什么不明白的地方请告诉我

这是我的 class "make":

@JsonIgnoreProperties(ignoreUnknown = true)
public class Make {

    @JsonProperty("id")
    private int mk_id ;
    @JsonProperty("name")
    private String mk_name;
    @JsonProperty("niceName")
    private String mk_nicename;
    @JsonProperty("models")
    private List<Model> list_modele; }

我的class型号:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Model {

    @JsonProperty("id")
    private String md_id ;
    @JsonProperty("name")
    private String md_name;
    @JsonProperty("niceName")
    private String md_nicename;
    @JsonProperty("years")
    private List<Years> list_years;}

和我的循环:

@FXML public void btn_clicked (javafx.event.ActionEvent e)
    {


        All_makes t = (All_makes)newparse_object <All_makes>(All_makes.class).ParseUri("https://api.edmunds.com/api/vehicle/v2/makes?state=new&fmt=json&api_key=wdxg7wh338vac3359m34qjj6");
        HBox o = new HBox();
        for( int i =0 ; i< t.get_list_makes().size();i++)
        {
        VBox b = new VBox(10);
        Label label = new Label(t.get_list_makes().get(i).get_mk_name());
        Label label2 = newLabel(t.get_list_makes().get(i).get_mk_nicename());
        b.getChildren().addAll(label, label2);
        b.setMaxSize(100, 100);
        o.getChildren().add(b);
        }
        o.maxWidth(5);
        scroll_pane.setContent(o);

}

A ListView 提供选择功能,以及开箱即用的滚动功能。这是一个简单的例子:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ListViewExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        ListView<Make> listView = new ListView<>();
        listView.setOrientation(Orientation.HORIZONTAL);

        listView.setMaxHeight(100);

        listView.setCellFactory(lv -> new ListCell<Make>() {
            private Label nameLabel = new Label();
            private Label niceNameLabel = new Label();
            private VBox vbox = new VBox(nameLabel, niceNameLabel);

            {
                vbox.setMaxSize(100, 100);
            }

            @Override
            protected void updateItem(Make make, boolean empty) {
                super.updateItem(make, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    nameLabel.setText(make.getMk_name());
                    niceNameLabel.setText(make.getMk_nicename());
                    setGraphic(vbox);
                }
            }
        });

        listView.getSelectionModel().selectedItemProperty().addListener((obs, oldMake, newMake) -> {
            System.out.println(newMake.getMk_name() + " selected");
        });

        for (int i = 1 ; i <= 10 ; i++) {
            Make make = new Make();
            make.setMk_name("Make "+i);
            make.setMk_nicename("Description of make "+i);
            listView.getItems().add(new Make("Make "+i, "Description of make "+i));
        }

        Scene scene = new Scene(new StackPane(listView));
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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