如果我有一个具有对象arrayList的对象:
class Event{
private ArrayList<Room> rooms;
//..
public void setRooms(ArrayList<Room> rooms) {
this.rooms = rooms;
}
public ArrayList<Room> getRooms() {
return rooms;
}
}
// ---------------------
class Room{
private String roomId;
private String roomName;
public Room(String roomId, String roomName) {
this.roomId = roomId;
this.roomName = roomName;
}
public String getRoomId() {
return roomId;
}
public String getRoomName() {
return roomName;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
}
如何从房间对象数组中创建表格中的组合框?
我所拥有的仅显示某种对象标识符。
TableColumn<Event, ArrayList> roomsColumn = new TableColumn<>("Room Select");
roomsColumn.setMinWidth(200);
roomsColumn.setCellValueFactory(new PropertyValueFactory<>("rooms"));
//Create an observable list to populate the table with.
ObservableList<Event> eventList = FXCollections.observableArrayList();
//loop the json to populate the observable list
for (Event event : events.getEventList() ){
eventList.add(event);
}
//populate the table
eventTable.setItems(eventList);
eventTable.getColumns().addAll(eventColumn, bDateColumn, eDateColumn, roomsColumn);
**构建了所有列,但是rooms列显示了逗号分隔的房间对象列表:
com.***.Room@345, com.***.Room@653, com.***.Room@889
答案 0 :(得分:1)
您需要一个自定义单元工厂来返回带有ComboBox的TableCell。
roomsColumn.setCellFactory(call -> {
// create a new cell for array lists
return new TableCell<Event, ArrayList<String>>() {
@Override
protected void updateItem(ArrayList<String> item, boolean empty) {
super.updateItem(item, empty);
// if there is no item, return an empty cell
if (empty || item == null) {
setGraphic(null);
}
else {
ComboBox<String> box = new ComboBox<>();
// set combo box items
box.setItems(FXCollections.observableArrayList(item));
// listen for changes
box.valueProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("new room "+newValue);
});
// set cell contents
setGraphic(box);
}
}
};
});