0
所以我想做一個簡單的JavaFX程序,顯示一些數據表。我有我的模型視圖控制器,並從我可以告訴一切看起來很清楚。運行應用程序顯示沒有問題,除了沒有數據顯示在表中。這4個表格已初始化,我可以點擊它們清楚地設置它們,只是沒有數據放入它們。這是我的代碼。JavaFX不填充表中的數據列
package releaseData;
import java.io.IOException;
import java.util.Observable;
import releaseData.model.ReleaseData;
import releaseData.view.ShowReleaseDataController;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainApp extends Application {
\t private Stage primaryStage;
\t //must match the the type of pane in .fxml file
\t private AnchorPane rootLayout;
\t
\t //set up a list
\t private ObservableList<ReleaseData> releaseData = FXCollections.observableArrayList();
\t //Constructor
\t public MainApp()
\t {
\t \t releaseData.add(new ReleaseData("Borderlands", "2K", "11/24/2010", "4/5"));
\t \t releaseData.add(new ReleaseData("Half-Life 2", "Valve", "9/9/2002", "5/5"));
\t \t releaseData.add(new ReleaseData("Far Cry 3", "Activision", "12/1/2012", "5/5"));
\t \t releaseData.add(new ReleaseData("Goat Simulator", "Coffe-Stain", "8/1/2014", "3/5"));
\t }
\t /**
\t @return
\t */
\t public ObservableList<ReleaseData> getReleaseData()
\t {
\t \t return releaseData;
\t }
\t
\t @Override
\t public void start(Stage primaryStage)
\t {
\t \t this.primaryStage = primaryStage;
\t \t this.primaryStage.setTitle("Game Data");
\t \t initRootLayout();
\t }
\t
\t public void initRootLayout()
\t {
\t \t try {
\t \t \t //Load fxml layout
\t \t \t FXMLLoader loader = new FXMLLoader();
\t \t \t loader.setLocation(MainApp.class.getResource("view/releaseDataOverview.fxml"));
\t \t \t rootLayout = (AnchorPane) loader.load();
\t \t \t
\t \t \t //Give the controller access
\t \t \t ShowReleaseDataController controller = loader.getController();
\t \t \t controller.setMainApp(this);
\t \t \t
\t \t \t //Show scene containing the root layout
\t \t \t Scene scene = new Scene(rootLayout);
\t \t \t primaryStage.setScene(scene);
\t \t \t primaryStage.show();
\t \t \t
\t \t }catch (IOException e) {e.printStackTrace(); }
\t }
\t
\t public Stage getPrimaryStage()
\t {
\t \t return primaryStage;
\t }
\t
\t public static void main(String[] args) {
\t \t launch(args);
\t }
}
package releaseData.model;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class ReleaseData {
\t
\t private final StringProperty name;
\t private final StringProperty publisher;
\t private final StringProperty date;
\t private final StringProperty rating;
\t
\t //Constructors
\t public ReleaseData() { this(null, null, null, null); }
\t
\t public ReleaseData(String name, String publisher, String date, String rating){
\t \t this.name = new SimpleStringProperty(name);
\t \t this.publisher = new SimpleStringProperty(publisher);
\t \t this.date = new SimpleStringProperty(date);
\t \t this.rating = new SimpleStringProperty(rating);
\t }
\t
\t //Sets, gets, properties
\t public String getName() { return name.get(); }
\t public void setName(String name) {this.name.set(name); }
\t public StringProperty nameProperty() { return name; }
\t
\t public String getPublisher() {return publisher.get(); }
\t public void setPublisher(String publisher) {this.publisher.set(publisher); }
\t public StringProperty publisherProperty() {return publisher; }
\t
\t public String getDate() { return date.get(); }
\t public void setDate(String date) {this.date.set(date); }
\t public StringProperty dateProperty() { return date; }
\t
\t public String getRating() { return rating.get(); }
\t public void setRating(String rating) { this.rating.set(rating); }
\t public StringProperty ratingProperty() {return rating; }
\t
\t }
\t
package releaseData.view;
import releaseData.MainApp;
import releaseData.model.ReleaseData;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class ShowReleaseDataController {
\t
\t @FXML
\t private TableView<ReleaseData> releaseDataTable;
\t @FXML
\t private TableColumn<ReleaseData, String> nameColumn;
\t @FXML
\t private TableColumn<ReleaseData, String> publisherColumn;
\t @FXML
\t private TableColumn<ReleaseData, String> dateColumn;
\t @FXML
\t private TableColumn<ReleaseData, String> ratingColumn;
\t
\t private MainApp mainApp;
\t
\t public ShowReleaseDataController() {}
\t
\t
\t @FXML
\t private void initializer()
\t {
\t \t nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
\t \t publisherColumn.setCellValueFactory(cellData -> cellData.getValue().publisherProperty());
\t \t dateColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
\t \t ratingColumn.setCellValueFactory(cellData -> cellData.getValue().ratingProperty());
\t }
\t
\t public void setMainApp(MainApp mainApp)
\t {
\t \t this.mainApp=mainApp;
\t \t releaseDataTable.setItems(mainApp.getReleaseData());
\t }
}
我所做的一切我能想到的到f解決這個問題。如果有人能指引我正確的方向,我會非常感激!
謝謝謝謝謝謝!一封流浪的信讓我完全停留了兩個小時!我只是需要別人來幫我看看!這是今晚的最後一場,你救了我的培根朋友! – Athlos 2014-12-08 00:20:33