【发布时间】:2019-05-19 14:07:02
【问题描述】:
我对 JavaFX 还很陌生。我一直在绝望地试图让它工作这么长时间,但不知道为什么它不起作用。这些项目未显示在 tableview 上。我使用场景生成器创建了 UI。我查看了许多类似的问题,但似乎没有任何帮助。
简化为最少的代码:
主要:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
package sample;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
ObservableList<Person> people = FXCollections.observableArrayList();
@FXML private TableView<Person> table;
@FXML private TableColumn<Person, SimpleStringProperty> c1;
@FXML private TableColumn<Person, SimpleStringProperty> c2;
@Override
public void initialize(URL location, ResourceBundle resources) {
c1.setCellValueFactory(new PropertyValueFactory<>("age"));
c2.setCellValueFactory(new PropertyValueFactory<>("name"));
people.add(new Person("2", "Sam"));
people.add(new Person("1", "Met"));
table.setItems(people);
}
}
人物类:
package sample;
import javafx.beans.property.SimpleStringProperty;
public class Person {
private SimpleStringProperty age;
private SimpleStringProperty name;
public Person(String age, String name) {
this.age = new SimpleStringProperty(age);
this.name = new SimpleStringProperty(name);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
xmlns:fx="http://javafx.com/fxml/1">
<TableView fx:id="table" layoutX="51.0" layoutY="31.0" prefHeight="338.0" prefWidth="498.0">
<columns>
<TableColumn fx:id="c1" prefWidth="75.0" text="C1">
<cellValueFactory>
<PropertyValueFactory property="c1" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="c2" prefWidth="75.0" text="C1">
<cellValueFactory>
<PropertyValueFactory property="c2" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</AnchorPane>
谢谢。
【问题讨论】:
-
new PropertyValueFactory<>("age")要求表项类 (Person) 具有公共getAge()方法。更改 Person 以具有适当的 JavaFX 属性,就像大多数标准 JavaFX 类一样。 -
您需要在
initialize方法中将 TableColumn 对象放入 TableView 对象:table.getColumns().addAll(new TableColumn[]{c1, c2}); -
@Embid123 没有必要这样做(你不需要手动创建数组,因为它是一个可变参数方法;
table.getColumns().addAll(c1, c2);会产生相同的结果):fxml 结果的结构在TableColumn对象中已经是columns列表的一部分。 -
我在 fxml 中没有看到,抱歉。