0
我想添加一个TableView控件到预先存在的应用程序。我试图复制下面的示例代码(它完美运行):通过fxml创建TableView不填充
public class FxTableViewExample1 extends Application {
private TableView<TransitionRow> outputTable;
private ObservableList<TransitionRow> data;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Table View Example 1");
// Table view, data, columns and properties
outputTable = new TableView<TransitionRow>();
data = getInitialTableData();
outputTable.setItems(data);
TableColumn col1 = new TableColumn("Harland");
col1.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("scaleY"));
TableColumn col2 = new TableColumn("Gradstein");
col2.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("gradsteinAge"));
TableColumn col3 = new TableColumn("Label");
col3.setCellValueFactory(new PropertyValueFactory<TransitionRow, String>("oldName"));
outputTable.getColumns().setAll(col1, col2, col3);
outputTable.setPrefWidth(450);
outputTable.setPrefHeight(300);
// Vbox
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(25, 25, 25, 25));;
vbox.getChildren().addAll(outputTable);
// Scene
Scene scene = new Scene(vbox, 500, 475); // w x h
primaryStage.setScene(scene);
primaryStage.show();
// Select the first row
outputTable.getSelectionModel().select(0);
TransitionRow tr = outputTable.getSelectionModel().getSelectedItem();
System.out.println(tr);
}
private ObservableList<TransitionRow> getInitialTableData() {
List<TransitionRow> list = new ArrayList<>();
TransitionRow tr1 = new TransitionRow();
tr1.setScaleY((Double) 124.567d);
tr1.setGradsteinAge((Double) 130.001d);
tr1.setOldName("Stuff");
TransitionRow tr2 = new TransitionRow();
tr2.setScaleY((Double) 456.546d);
tr2.setGradsteinAge((Double) 123.768d);
tr2.setOldName("Other stuff");
list.add(tr1);
list.add(tr2);
ObservableList<TransitionRow> data = FXCollections.observableList(list);
return data;
}
}
我的应用程序是通过SceneBuilder与单独的控制器类。当我试图在上面的例子中的表格,没有填充所以整合我创建了下面的小例子来证明我的问题:
[Test1Run.java]
public class Test1Run extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Test1.fxml"));
stage.setTitle("Test1");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
[Test1.fxml]
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="experimental.tableview.Test1Controller">
<TableView fx:id="outputTable" layoutX="14.0" layoutY="14.0" prefHeight="371.0" prefWidth="569.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="17.0">
<columns>
<TableColumn fx:id="col1" prefWidth="75.0" text="C1" />
<TableColumn fx:id="col2" prefWidth="75.0" text="C2" />
<TableColumn fx:id="col3" prefWidth="75.0" text="C3" />
</columns>
</TableView>
</AnchorPane>
[Test1Controller.java]
public class Test1Controller implements Initializable {
@FXML private TableView<TransitionRow> outputTable;
@FXML private TableColumn<TransitionRow, Double> col1;
@FXML private TableColumn<TransitionRow, Double> col2;
@FXML private TableColumn<TransitionRow, String> col3;
private ObservableList<TransitionRow> data;
@Override
public void initialize(URL url, ResourceBundle rb) {
outputTable = new TableView<TransitionRow>();
col1 = new TableColumn<TransitionRow,Double>("Harland");
col1.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("scaleY"));
col2 = new TableColumn<TransitionRow,Double>("Gradstein");
col2.setCellValueFactory(new PropertyValueFactory<TransitionRow, Double>("gradsteinAge"));
col3 = new TableColumn<TransitionRow,String>("Label");
col3.setCellValueFactory(new PropertyValueFactory<TransitionRow, String>("oldName"));
// This line should cause the column names on the GUI to change. They don't.
outputTable.getColumns().addAll(col1, col2, col3);
data = getInitialTableData();
// This line should cause rows of data to appear on the TableView. It doesn't.
outputTable.setItems(data);
}
private ObservableList<TransitionRow> getInitialTableData() {
List<TransitionRow> list = new ArrayList<>();
TransitionRow tr1 = new TransitionRow();
tr1.setScaleY((Double) 124.567d);
tr1.setGradsteinAge((Double) 130.001d);
tr1.setOldName("Stuff");
TransitionRow tr2 = new TransitionRow();
tr2.setScaleY((Double) 456.546d);
tr2.setGradsteinAge((Double) 123.768d);
tr2.setOldName("Other stuff");
list.add(tr1);
list.add(tr2);
ObservableList<TransitionRow> results = FXCollections.observableList(list);
return results;
}
}
我按照你的建议编辑了“初始化”,它没有区别? 另外我不明白你的意思是“添加到现场”。当然tableview是通过SceneBuilder/fxml创建的,我只想配置一个数据给它? – Ralph
另外我假设TransitionRow有正确的方法,因为它可以在第一个代码片段中与PropertyValueFactory完美地工作:FxTableViewExample1 – Ralph
@Ralph我只是用我的修改测试了代码,它的工作原理......一定有什么错误,你没有发布任何信息关于......通过*“添加到场景”*向父项添加代码,该代码是/成为场景根的后代或该根本身,当您覆盖该字段的值时,您不会执行此操作。 – fabian