首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javafx和Java

Javafx和Java
EN

Stack Overflow用户
提问于 2017-01-28 22:45:39
回答 1查看 64关注 0票数 0

我有一个问题,我真的很感谢大家的帮助和回答。

这就是我的问题:

我正在使用edmunds api。我已经创建了类,可以在classes对象中解析json数据了。现在,我想将这些对象显示为Scrollpane。为此,我创建了一个" for“循环,为每个对象创建一个HBOX,标签中包含我的对象属性的名称。

到目前为止,在HBOX和滚动窗格中显示数据是成功的,但它们只是HBoxs,我不能选择任何一个HBOX来使用。

在我的滚动窗格中,我显示汽车的制造商(如宝马,奥迪等),当我选择例如宝马的HBOx时,我只想显示该制造商的所有车型。

如果你有什么不明白的,请告诉我

这是我的类"make":

代码语言:javascript
复制
@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; }

我的班级模型:

代码语言:javascript
复制
@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;}

我的循环是:

代码语言:javascript
复制
@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);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-29 00:50:05

ListView提供了选择功能,以及向外滚动。下面是一个简单的例子:

代码语言:javascript
复制
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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41911067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档