如何使用List从javafx中的CellValueFactory填充TableColumns单元格?我有一个特定整数的列表,我希望在我的列中显示,我不知道如何正确使用setCellValueFactory来显示我已存储在此列表中的整数。
package com.editor.controller;
import java.util.ArrayList;
import java.util.List;
import com.editor.drops.DropManager;
import com.editor.drops.DropTable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class EditorController {
@FXML
private TableView<Integer> idTable;
@FXML
private TableColumn ids; //What goes here exactly? <P, S> ?
private static final ObservableList<Integer> NPCS = FXCollections.observableArrayList();
public static final List<DropTable> TABLES = new ArrayList<>();
@FXML
private void initialize() {
DropManager.loadDrops();
TABLES.forEach(table -> {
table.getIds().forEach(id -> NPCS.add(id));
});
}
}
答案 0 :(得分:1)
你需要创建一个属性为id(整数)的类,它应该是这样的:
public class SomeClass {
private int id;
public int getId() {
return pid;
}
public void setId(int id) {
this.id = id;
}
}
public class EditorController {
@FXML
private TableView<SomeClass> idTable;
@FXML
private TableColumn<SomeClass, Integer> ids;
private static final ObservableList<Integer> NPCS = FXCollections.observableArrayList();
public static final List<DropTable> TABLES = new ArrayList<>();
@FXML
private void initialize() {
DropManager.loadDrops();
TABLES.forEach(table -> {
table.getIds().forEach(id -> NPCS.add(id));
});
ids.setCellValueFactory(new PropertyValueFactory<SomeClass, Integer>("id"));
}
}