在 javafx table 中设置项目,我做错了什么?
Setting the items in a javafx table, what am I doing wrong?
我必须初始化 javafx table 但我不知道出了什么问题。我使用了这个:https://docs.oracle.com/javafx/2/ui_controls/table-view.htm 教程,我也在使用 SceneBuilder 和 FXML 来制作 GUI。
主要class:
这将设置 window 并创建 table,以及所有列等。它还确保打开或创建将被读取和写入的文件。
public class Main extends Application {
private AnchorPane rootLayout;
public void initialize ()
{
System.out.println("Initialized");
try {
File masterFile = new File(("G:\"),"masterfile.txt"); //I just needed to create the file to be able to access it at a later date
if (!masterFile.exists()){
masterFile.createNewFile();
}
MainScreenController table = new MainScreenController();
table.initializeTable();
System.setProperty("glass.accessible.force", "false");
} catch (Exception e) {
System.out.println("INITIALIZE METHOD WENT HAYWIRE");
}
}
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader root = new FXMLLoader();
root.setLocation(Main.class.getResource("view/MainScreen.fxml"));
rootLayout = (AnchorPane) root.load();
Scene scene = new Scene(rootLayout);
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
initialize();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
table 的控制器 class:
这是我尝试用我拥有的数据填充列。
@FXML
public Button showAddNewPerson;
@FXML
public Button EditStudentFiles;
@FXML
public Button generateDebtorReport;
@FXML
public Button generateSeniorReport;
@FXML
public TableView<StudentData> dataTable = new TableView<StudentData>();
@FXML
private TableColumn<StudentData, String> firstNameCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> lastNameCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> schoolCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> stateCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> emailCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, Integer> yearJoinedCol = new TableColumn<StudentData, Integer>();
@FXML
private TableColumn<StudentData, String> activeCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, Double> debtCol = new TableColumn<StudentData, Double>();
public AnchorPane addNewPersonLayout;
@SuppressWarnings("unchecked")
public void initializeTable() {
ObservableList<StudentData> observableData = FXCollections.observableArrayList(StudentData.retrieveDataFromFile());//retrieves the array list from the file
//I think these statements pick out each instance variable of each object in the arrayList
firstNameCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("lastName"));
schoolCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("schoolName"));
stateCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("stateAbbreviaion"));
emailCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("studentEmail"));
yearJoinedCol.setCellValueFactory(new PropertyValueFactory<StudentData, Integer>("yearJoined"));
activeCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("areTheyActive"));
debtCol.setCellValueFactory(new PropertyValueFactory<StudentData, Double>("amountOwed"));
//sets observable list into the table
dataTable.setItems(observableData);
//this is what the tutorial says works
dataTable.getColumns().addAll(firstNameCol, lastNameCol, schoolCol, stateCol, emailCol, yearJoinedCol, activeCol, debtCol);
}
我真的不明白这个 table 应该如何工作,因为教程让我有点困惑。
首先,确保您已经在 FXML 中定义了控制器 class。
<YourRootElement fx:controller="MyControllerClass">
然后,我不确定您为什么要创建 "visual elements" 的新实例以及为什么将它们定义为 public,但是如果您使用的是 JavaFX 8,则需要定义他们是这样的:
@FXML
private TableView<StudentData> dataTable;
关于你的 Table,你有 3 个选择:
(最好的)初始化controller里面的Table。在调用 main 上的 "show" 方法之前,您应该在控制器上创建初始化方法以填充所有数据。该方法将由 JRE 默认调用...请确保为您的学生提供 Getters Class。
public ControllerClass{
//All your variables and Stuff
@FXML
private void initialize(){
//here is where you initialize your data
}
}
有一个小技巧可以强制 table 更新,但由于我不能 100% 确定您是否正确收集了数据,所以我不能保证它能正常工作.
在将项目添加到 ObservableList
之后放置此代码
dataTable.getColumns().get(0).setVisible(false);
dataTable.getColumns().get(0).setVisible(true);
我必须初始化 javafx table 但我不知道出了什么问题。我使用了这个:https://docs.oracle.com/javafx/2/ui_controls/table-view.htm 教程,我也在使用 SceneBuilder 和 FXML 来制作 GUI。
主要class: 这将设置 window 并创建 table,以及所有列等。它还确保打开或创建将被读取和写入的文件。
public class Main extends Application {
private AnchorPane rootLayout;
public void initialize ()
{
System.out.println("Initialized");
try {
File masterFile = new File(("G:\"),"masterfile.txt"); //I just needed to create the file to be able to access it at a later date
if (!masterFile.exists()){
masterFile.createNewFile();
}
MainScreenController table = new MainScreenController();
table.initializeTable();
System.setProperty("glass.accessible.force", "false");
} catch (Exception e) {
System.out.println("INITIALIZE METHOD WENT HAYWIRE");
}
}
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader root = new FXMLLoader();
root.setLocation(Main.class.getResource("view/MainScreen.fxml"));
rootLayout = (AnchorPane) root.load();
Scene scene = new Scene(rootLayout);
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
initialize();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
table 的控制器 class: 这是我尝试用我拥有的数据填充列。
@FXML
public Button showAddNewPerson;
@FXML
public Button EditStudentFiles;
@FXML
public Button generateDebtorReport;
@FXML
public Button generateSeniorReport;
@FXML
public TableView<StudentData> dataTable = new TableView<StudentData>();
@FXML
private TableColumn<StudentData, String> firstNameCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> lastNameCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> schoolCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> stateCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, String> emailCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, Integer> yearJoinedCol = new TableColumn<StudentData, Integer>();
@FXML
private TableColumn<StudentData, String> activeCol = new TableColumn<StudentData, String>();
@FXML
private TableColumn<StudentData, Double> debtCol = new TableColumn<StudentData, Double>();
public AnchorPane addNewPersonLayout;
@SuppressWarnings("unchecked")
public void initializeTable() {
ObservableList<StudentData> observableData = FXCollections.observableArrayList(StudentData.retrieveDataFromFile());//retrieves the array list from the file
//I think these statements pick out each instance variable of each object in the arrayList
firstNameCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("lastName"));
schoolCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("schoolName"));
stateCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("stateAbbreviaion"));
emailCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("studentEmail"));
yearJoinedCol.setCellValueFactory(new PropertyValueFactory<StudentData, Integer>("yearJoined"));
activeCol.setCellValueFactory(new PropertyValueFactory<StudentData, String>("areTheyActive"));
debtCol.setCellValueFactory(new PropertyValueFactory<StudentData, Double>("amountOwed"));
//sets observable list into the table
dataTable.setItems(observableData);
//this is what the tutorial says works
dataTable.getColumns().addAll(firstNameCol, lastNameCol, schoolCol, stateCol, emailCol, yearJoinedCol, activeCol, debtCol);
}
我真的不明白这个 table 应该如何工作,因为教程让我有点困惑。
首先,确保您已经在 FXML 中定义了控制器 class。
<YourRootElement fx:controller="MyControllerClass">
然后,我不确定您为什么要创建 "visual elements" 的新实例以及为什么将它们定义为 public,但是如果您使用的是 JavaFX 8,则需要定义他们是这样的:
@FXML
private TableView<StudentData> dataTable;
关于你的 Table,你有 3 个选择:
(最好的)初始化controller里面的Table。在调用 main 上的 "show" 方法之前,您应该在控制器上创建初始化方法以填充所有数据。该方法将由 JRE 默认调用...请确保为您的学生提供 Getters Class。
public ControllerClass{ //All your variables and Stuff @FXML private void initialize(){ //here is where you initialize your data } }
有一个小技巧可以强制 table 更新,但由于我不能 100% 确定您是否正确收集了数据,所以我不能保证它能正常工作. 在将项目添加到 ObservableList
之后放置此代码dataTable.getColumns().get(0).setVisible(false); dataTable.getColumns().get(0).setVisible(true);