Reputation: 846
I have an ArrayList :
List<Column> listOfColumns = new ArrayList();
How can I bind this list to a listView? It should always show the columns inside this list.
Also, I use
final ObservableList<Columns> data = FXCollections.observableArrayList();
right now to make the ListView only contain Columns, but a Column is an object which contains a name(string) and an array list, so right now my ListView shows both the name of the column and the content of the array list in the Column. Is there any way to make the ListView only show the name of the column?
The reason I do it like this, is because then I can use listView.getSelectionModel().getSelectedItem(), and it returns a column.
Upvotes: 1
Views: 3006
Reputation: 45456
Given that Column
is a POJO with one String
field and one ArrayList
, by default what you see in the ListView
is given by the toString()
method.
You just need to customize this method to display what you want:
private class Column {
private String name;
private ArrayList<String> list;
public Column(String name, ArrayList list) {
this.name = name;
this.list = list;
}
// getters and setters
@Override
public String toString() {
//return name + list;
return name;
}
}
Anyway, the listener will return a Column
object if a row is selected. What you print there depends if you're using toString()
or just accesing the string field:
list.getSelectionModel().selectedItemProperty().addListener(
(observable,oldValue,newValue)->{
System.out.println("Selected item: " + newValue);
System.out.println("Selected item: " + newValue.getName());
});
Upvotes: 2