我正在寻求建议,因为我无法制作我的表格和底层 TableView
在场景构建器中设计,显示可观察列表中的数据。我的模型正在返回 ObservableList<ObservableList>
,我想将其分配给表单 Controller 中的列(例如 id
)。这是我的代码:
@FXML
private TableColumn id;
Database database = new Database();
TableView tableView = new TableView();
tableView.setItems(database.returnData()); // --> this is the method returning Observablelist
id = tableView.getColumns().get(0); // --> line with error
我收到错误
Error:(35, 44) java: incompatible types: java.lang.Object cannot be converted to javafx.scene.control.TableColumn你能帮我让它工作吗? 非常感谢!
编辑: 如果我从数据库中拉出 TableView 而不是可观察列表,在 Controller 中启动它,然后将 @FXML 列分配给拉出的 TableView 中的列,会更容易吗?如果是,知道该怎么做吗?感谢您可能分享的任何帮助或文字。彼得
最佳答案
您需要将表和列链接到 FXML 定义,还需要创建一个放入表中的 ObservableList
。
@FXML private TableView<SimpleFileProperty> table;
@FXML private TableColumn<SimpleFileProperty, String> sizeCol;
final private ObservableList<SimpleFileProperty> tableData =
FXCollections.observableArrayList();
// ...
table.setItems(tableData);
关于Java将Observable列表中的列分配给@fxml列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104273/