所以我试图创建一个显示几个数据表的简单JavaFX程序。我有我的模型视图控制器,从我可以告诉一切看起来很清楚。运行应用程序显示没有问题,除了表中没有数据显示。 4个表被初始化,我可以点击它们,这样就可以清楚地设置它们,只是没有数据被放入其中。这是我的代码。
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 {
private Stage primaryStage;
//must match the the type of pane in .fxml file
private AnchorPane rootLayout;
//set up a list
private ObservableList<ReleaseData> releaseData = FXCollections.observableArrayList();
//Constructor
public MainApp()
{
releaseData.add(new ReleaseData("Borderlands", "2K", "11/24/2010", "4/5"));
releaseData.add(new ReleaseData("Half-Life 2", "Valve", "9/9/2002", "5/5"));
releaseData.add(new ReleaseData("Far Cry 3", "Activision", "12/1/2012", "5/5"));
releaseData.add(new ReleaseData("Goat Simulator", "Coffe-Stain", "8/1/2014", "3/5"));
}
/**
@return
*/
public ObservableList<ReleaseData> getReleaseData()
{
return releaseData;
}
@Override
public void start(Stage primaryStage)
{
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Game Data");
initRootLayout();
}
public void initRootLayout()
{
try {
//Load fxml layout
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/releaseDataOverview.fxml"));
rootLayout = (AnchorPane) loader.load();
//Give the controller access
ShowReleaseDataController controller = loader.getController();
controller.setMainApp(this);
//Show scene containing the root layout
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
}catch (IOException e) {e.printStackTrace(); }
}
public Stage getPrimaryStage()
{
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
package releaseData.model;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class ReleaseData {
private final StringProperty name;
private final StringProperty publisher;
private final StringProperty date;
private final StringProperty rating;
//Constructors
public ReleaseData() { this(null, null, null, null); }
public ReleaseData(String name, String publisher, String date, String rating){
this.name = new SimpleStringProperty(name);
this.publisher = new SimpleStringProperty(publisher);
this.date = new SimpleStringProperty(date);
this.rating = new SimpleStringProperty(rating);
}
//Sets, gets, properties
public String getName() { return name.get(); }
public void setName(String name) {this.name.set(name); }
public StringProperty nameProperty() { return name; }
public String getPublisher() {return publisher.get(); }
public void setPublisher(String publisher) {this.publisher.set(publisher); }
public StringProperty publisherProperty() {return publisher; }
public String getDate() { return date.get(); }
public void setDate(String date) {this.date.set(date); }
public StringProperty dateProperty() { return date; }
public String getRating() { return rating.get(); }
public void setRating(String rating) { this.rating.set(rating); }
public StringProperty ratingProperty() {return rating; }
}
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 {
@FXML
private TableView<ReleaseData> releaseDataTable;
@FXML
private TableColumn<ReleaseData, String> nameColumn;
@FXML
private TableColumn<ReleaseData, String> publisherColumn;
@FXML
private TableColumn<ReleaseData, String> dateColumn;
@FXML
private TableColumn<ReleaseData, String> ratingColumn;
private MainApp mainApp;
public ShowReleaseDataController() {}
@FXML
private void initializer()
{
nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
publisherColumn.setCellValueFactory(cellData -> cellData.getValue().publisherProperty());
dateColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
ratingColumn.setCellValueFactory(cellData -> cellData.getValue().ratingProperty());
}
public void setMainApp(MainApp mainApp)
{
this.mainApp=mainApp;
releaseDataTable.setItems(mainApp.getReleaseData());
}
}
我已经完成了我能想到的一切以找到问题。如果有人能指出我正确的方向,我将非常感激!
答案 0 :(得分:0)
初始化方法应为
public void initialize() {...}
不是initializer()