2013-11-24 93 views
1

一个列表:列表视图与多个列表

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

有你的一个例子。只要在国家和资本领域打一个豆。你将有一个你的豆的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方法