Listview有多个列表

时间:2013-11-24 19:55:35

标签: javafx-2

一个清单:

ListView list = (ListView) pane.lookup("#list");
ObservableList<String> countries = FXCollections.observableArrayList(
   "England", "Germany", "France", "Israel");
list.setItems(countries);

enter image description here


请告诉我怎么做这个? enter image description here

 ListView list = (ListView) root.lookup("#list");
    ObservableList<String> countries = FXCollections.observableArrayList(
            "England", "Germany", "France", "Israel");

    ObservableList<String> capitals = FXCollections.observableArrayList(
            "London", "Berlin", "Paris", "Ierusalim");

1 个答案:

答案 0 :(得分:1)

有一个例子给你。只需制作一个有国家和资本领域的豆子。你将拥有一个YourBean的ListView。像那样:

豆子

public class MyBean {

    private String country;
    private String capital;


    public MyBean(String country, String capital) {
        this.country = country;
        this.capital = capital;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCapital() {
        return capital;
    }

    public void setCapital(String capital) {
        this.capital = capital;
    }
}

和ListView

 public class Example extends ListView<MyBean> {


    public Example() {
        this.getItems().add(new MyBean("France", "Paris"));
        this.getItems().add(new MyBean("England", "London"));
        this.setCellFactory(new Callback<ListView<MyBean>, ListCell<MyBean>>() {
            @Override
            public ListCell<MyBean> call(ListView<MyBean> myBeanListView) {
                return new ListCell<MyBean>() {
                    @Override
                    protected void updateItem(MyBean myBean, boolean b) {
                        super.updateItem(myBean, b);
                        if (!b) {
                            HBox box = new HBox();
                            box.setSpacing(50);
                            box.getChildren().add(new Label(myBean.getCountry()));
                            box.getChildren().add(new Label(myBean.getCapital()));
                            setGraphic(box);
                        } else {
                            setGraphic(null);
                        }
                    }
                };
            }
        });
    }
}

enter image description here

你只需要将它改编为你的程序,但它会向你展示好的setCellFactory方法