我正在为Uni项目创建一个Cinema系统。我已经在JavaFXML中创建了一个Tableview,但是无法用数据填充它。我不确定为什么。
Main.java(CinemaSystem.java)-这是项目的主类。
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("home.fxml"));
primaryStage.setScene(new Scene(root));
//Set Stage as Borderless
primaryStage.initStyle(StageStyle.UNDECORATED);
//Make window draggable
root.setOnMousePressed(event -> {
x = event.getSceneX();
y = event.getSceneY();
});
root.setOnMouseDragged(event -> {
primaryStage.setX(event.getScreenX() - x);
primaryStage.setY(event.getScreenY() - y);
});
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}}
CinemaController.java-这是home.fxml文件的控制器。
public class CinemaController {
public abstract class FilmController implements Initializable {
//Configure the table
@FXML
private TableView<Films> tableView;
@FXML
private TableColumn<Films, String> filmNameColumn;
@FXML
private TableColumn<Films, String> filmYearColumn;
@FXML
private TableColumn<Films, String> filmAgeColumn;
@FXML
private TableColumn<Films, String> filmTheatreColumn;
@FXML
private TableColumn<Films, LocalDate> filmDateColumn;
public void initialize(URL url, ResourceBundle rb) {
//set up columns in the table
filmNameColumn.setCellValueFactory(new PropertyValueFactory<Films, String>("FilmName"));
filmYearColumn.setCellValueFactory(new PropertyValueFactory<Films, String>("Year"));
filmAgeColumn.setCellValueFactory(new PropertyValueFactory<Films, String>("AgeRating"));
filmTheatreColumn.setCellValueFactory(new PropertyValueFactory<Films, String>("Theatre"));
filmDateColumn.setCellValueFactory(new PropertyValueFactory<Films, LocalDate>("DateTime"));
//load dummy data
tableView.setItems(getFilms());
}
//Return an ObservableList of Objects
public ObservableList<Films> getFilms() {
ObservableList<Films> Films = FXCollections.observableArrayList();
Films.add(new Films("Test", "2000", "18+", "12", LocalDate.of(2019, Month.MARCH, 25)));
return Films;
}
}}
home.fxml-在此文件中,TableView具有以下内容:
<TableView fx:id="tableView" layoutX="268.0" layoutY="115.0" prefHeight="480.0" prefWidth="635.0">
<columns>
<TableColumn fx:id="filmNameColumn" prefWidth="175.0" text="Film Name" />
<TableColumn fx:id="filmYearColumn" minWidth="0.0" prefWidth="109.0" text="Film Year" />
<TableColumn fx:id="filmAgeColumn" minWidth="3.0" prefWidth="98.0" text="Age Rating" />
<TableColumn fx:id="filmTheatreColumn" minWidth="0.0" prefWidth="111.0" text="Theatre" />
<TableColumn fx:id="filmDateColumn" prefWidth="141.0" text="Date / Time" />
</columns>
</TableView>
当我运行该程序时,它在表视图中没有显示任何数据,我不确定为什么。任何帮助将是巨大的!谢谢。