我正在一个项目中,我需要在表视图中添加一些数据,但是当我尝试调用addToTable时,它没有显示任何数据。我已经尝试过刷新表,并且当我尝试在initialize方法中对其进行硬编码时,在启动应用程序时它确实会显示。 编辑:我在创建表视图的位置添加了Fxml文件。
package nl.delphinity.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import nl.delphinity.model.QuestionTable;
public class TeacherController implements Initializable {
@FXML
public TableView<QuestionTable> tableView;
@FXML
public TableColumn<QuestionTable, String> nameColumn;
@FXML
public TableColumn<QuestionTable, String> subjectColumn;
@FXML
public TableColumn<QuestionTable, String> timeColumn;
public Button finishQuestion;
@Override
public void initialize(URL location, ResourceBundle resources) {
nameColumn.setCellValueFactory(new PropertyValueFactory<QuestionTable, String>("name"));
subjectColumn.setCellValueFactory(new PropertyValueFactory<QuestionTable, String>("subject"));
timeColumn.setCellValueFactory(new PropertyValueFactory<QuestionTable, String>("time"));
}
@FXML
private void answerQuestion() {
QuestionTable selectedItem = tableView.getSelectionModel().getSelectedItem();
tableView.getItems().remove(selectedItem);
}
@FXML
public void addToTable(String name, String subject, String time) {
QuestionTable test = new QuestionTable(name, subject, time);
tableView.getItems().add(test);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.paint.LinearGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="551.0" prefWidth="340.0" style="-fx-background-color: #fff; -fx-border-width: 200%; -fx-border-color: afabab;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="nl.delphinity.controller.TeacherController">
<Label alignment="CENTER" layoutX="33.0" layoutY="69.0" prefHeight="107.0" prefWidth="275.0" style="-fx-font-family: calibri;" text="Vos by Tesla">
<font>
<Font size="40.0" />
</font>
<textFill>
<LinearGradient endX="1.0" endY="1.0" startX="0.18702290895330997" startY="1.0">
<stops>
<Stop color="#8e2de2" />
<Stop color="#5100ff" offset="1.0" />
</stops>
</LinearGradient>
</textFill>
</Label>
<TableView fx:id="tableView" layoutX="45.0" layoutY="176.0" prefHeight="285.0" prefWidth="250.0" style="-fx-background-color: #dad8d4;">
<columns>
<TableColumn fx:id="nameColumn" prefWidth="84.00000762939453" text="Naam" />
<TableColumn fx:id="subjectColumn" prefWidth="81.5999755859375" text="Vak" />
<TableColumn fx:id="timeColumn" prefWidth="84.79998779296875" text="Tijd" />
</columns>
</TableView>
<Button fx:id="finishQuestion" layoutX="95.0" layoutY="488.0" mnemonicParsing="false" onMouseClicked="#answerQuestion" prefHeight="26.0" prefWidth="150.0" text="Vraag afhandelen" textFill="#636060" />
</AnchorPane>
public void QuestionListener(String subject) {
App.db.collection(subject)
.whereEqualTo("subject", subject)
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirestoreException e) {
if (e != null) {
System.err.println("Listen failed: " + e);
return;
}
for (DocumentChange dc : snapshots.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
System.out.println("New: " + dc.getDocument().getData());
TeacherController teacherController = new TeacherController();
teacherController.addToTable(dc.getDocument().getString("name"), dc.getDocument().getString("subject"), dc.getDocument().getString("time"));
break;
case MODIFIED:
System.out.println("Modified: " + dc.getDocument().getData());
break;
case REMOVED:
System.out.println("Removed: " + dc.getDocument().getData());
break;
default:
break;
}
}
}
});
}
```
答案 0 :(得分:-1)
您需要将observablList附加到表视图中,并且代码应如下所示。
public class TeacherController implements Initializable {
@FXML
public TableView<QuestionTable> tableView;
@FXML
public TableColumn<QuestionTable, String> nameColumn;
@FXML
public TableColumn<QuestionTable, String> subjectColumn;
@FXML
public TableColumn<QuestionTable, String> timeColumn;
public Button finishQuestion;
private Observablelist<QuestionTable> data = FXCollection.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
nameColumn.setCellValueFactory(new PropertyValueFactory<QuestionTable, String>("name"));
subjectColumn.setCellValueFactory(new PropertyValueFactory<QuestionTable, String>("subject"));
timeColumn.setCellValueFactory(new PropertyValueFactory<QuestionTable, String>("time"));
tableview.setItem(data);//Add Observable list object here....
}
@FXML
private void answerQuestion() {
QuestionTable selectedItem = tableView.getSelectionModel().getSelectedItem();
tableView.getItems().remove(selectedItem);
}
@FXML
public void addToTable(String name, String subject, String time) {
QuestionTable test = new QuestionTable(name, subject, time);
tableView.getItems().add(test);
}
}
如果已创建,则可以在表中添加或删除数据
data.remove(tableView.getSelectionModel().getSelectedIndex());
data.add(QuestionTable q);