javaFX 2.2-无法从控制器填充表 [英] javaFX 2.2 - Not able to populate table from Controller
本文介绍了javaFX 2.2-无法从控制器填充表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码.当我尝试运行时,表未在GUI上显示记录.你能建议吗?
I have below code. when i try to run, table does not display record on GUI. Can you please suggest?
fxml_tableview.fxml-描述表结构的fxml.
fxml_tableview.fxml - fxml which describe table structure.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.collections.*?>
<?import javafx.geometry.Insets?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import fxmltableview.*?>
<Scene width="400" height="550" fx:controller="fxmltableview.FXMLTableViewController"
xmlns:fx="http://javafx.com/fxml">
<GridPane alignment="center" hgap="10" vgap="10">
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
<columns>
<TableColumn fx:id="tradeId" text="Trade ID" prefWidth="200"/>
</columns>
</TableView>
</GridPane>
</Scene>
FXMLTableViewController.java//要在其中填充表列的控制器.
FXMLTableViewController.java //controller where I am trying to populate table columns.
package fxmltableview;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
public class FXMLTableViewController implements Initializable{
@FXML private TableView<Trade> tableView;
ObservableList<Trade> data = FXCollections.observableArrayList(new Trade("1"));
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
tableView.setItems (data);
}
}
FXMLTableView.java//Java主类
FXMLTableView.java //Java main class
package fxmltableview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLTableView extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("FXML TableView Example");
primaryStage.setScene((Scene)FXMLLoader.load(getClass().getResource("fxml_tableview.fxml")));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Trade.java//表的数据模型
Trade.java //Data model for table
package fxmltableview;
import javafx.beans.property.SimpleStringProperty;
public class Trade {
private SimpleStringProperty tradeId = new SimpleStringProperty("");
public Trade(String tradeId) {
setTradeId(tradeId);
}
public String getTradeId() {
return tradeId.get();
}
public void setTradeId(String a) {
tradeId.set(a);
}
}
推荐答案
快速猜测是您忘记了设置表列的单元格值工厂.
Quick guess is that you forgot to set the cell value factory of the tablecolumn.
@FXML private TableColumn<Trade, String> tradeId;
然后在初始化中:
tradeId.setCellValueFactory(new PropertyValueFactory<Trade, String>("tradeId"));
这篇关于javaFX 2.2-无法从控制器填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文