mr_nobody
mr_nobody

Reputation: 194

Listview with multiple lists

One list:

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

enter image description here


Please tell me how to do like this? 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");

Upvotes: 1

Views: 916

Answers (1)

agonist_
agonist_

Reputation: 5032

There is an example for you. Just make a bean with country and capital field. And you will have a ListView of YourBean. Like that :

The bean

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;
    }
}

and the 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

You just have to adapt it to your program but it for show you the good setCellFactory method

Upvotes: 1

Related Questions