一个清单:
ListView list = (ListView) pane.lookup("#list");
ObservableList<String> countries = FXCollections.observableArrayList(
"England", "Germany", "France", "Israel");
list.setItems(countries);
请告诉我怎么做这个?
ListView list = (ListView) root.lookup("#list");
ObservableList<String> countries = FXCollections.observableArrayList(
"England", "Germany", "France", "Israel");
ObservableList<String> capitals = FXCollections.observableArrayList(
"London", "Berlin", "Paris", "Ierusalim");
答案 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);
}
}
};
}
});
}
}
你只需要将它改编为你的程序,但它会向你展示好的setCellFactory方法