我试图将我从RethinkDB获取的数据推送到JavaFx TableView,但是,这些更改没有出现在tableview中,我无法弄清楚原因。< / p>
我对JavaFx很陌生,所以我希望你能帮助我。
以下是我的课程:(我没有包含我从数据库保存数据的内存类)
RethinkDBConnect类
public class RethinkDBConnect {
public static final RethinkDB r = RethinkDB.r;
GsonConverter con = new GsonConverter();
JiraTicketBody body = new JiraTicketBody();
ViewController viewcon = new ViewController();
TicketDataProperty tickprop = new TicketDataProperty(null, null, null, null, null, null);
public void Connection(){
viewcon.list.add(newTicketDataProperty
("test","test","test","test","test","test"));
}
}
TicketDataProperty类
public class TicketDataProperty {
private final SimpleStringProperty key;
private final SimpleStringProperty prioritaet;
private final SimpleStringProperty erstellt;
private final SimpleStringProperty status;
private final SimpleStringProperty zustand;
private final SimpleStringProperty beschreibung;
public TicketDataProperty(String key, String prioritaet, String erstellt,
String status, String zustand, String beschreibung)
{
this.key = new SimpleStringProperty(key);
this.prioritaet = new SimpleStringProperty(prioritaet);
this.erstellt = new SimpleStringProperty(erstellt);
this.status = new SimpleStringProperty(status);
this.zustand = new SimpleStringProperty(zustand);
this.beschreibung = new SimpleStringProperty(beschreibung);
}
public String getKey() {
return key.get();
}
public void setKey(String value) {
key.set(value);
}
public String getPrioritaet() {
return prioritaet.get();
}
public void setPrioritaet(String value) {
prioritaet.set(value);
}
public String getErstellt() {
return erstellt.get();
}
public void setErstellt(String value) {
erstellt.set(value);
}
public String getStatus() {
return status.get();
}
public void setStatus(String value) {
status.set(value);
}
public String getZustand() {
return zustand.get();
}
public void setZustand(String value) {
zustand.set(value);
}
public String getBeschreibung() {
return beschreibung.get();
}
public void setBeschreibung(String value) {
beschreibung.set(value);
}
}
ViewController类
public class ViewController implements Initializable {
TicketDataProperty tickdat = new TicketDataProperty(null, null, null, null, null, null);
@FXML private TableView <TicketDataProperty> table;
@FXML private TableColumn <TicketDataProperty,String> key;
@FXML private TableColumn <TicketDataProperty,String> prioritaet;
@FXML private TableColumn <TicketDataProperty,String> erstellt;
@FXML private TableColumn <TicketDataProperty,String> status;
@FXML private TableColumn <TicketDataProperty,String> zustand;
@FXML private TableColumn <TicketDataProperty,String> beschreibung;
public ObservableList<TicketDataProperty> list = FXCollections.observableArrayList(
new TicketDataProperty("example","example","example","example","example","example")
);
@Override
public void initialize(URL location, ResourceBundle resources) {
key.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("key"));
prioritaet.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("prioritaet"));
erstellt.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("erstellt"));
status.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("status"));
zustand.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("zustand"));
beschreibung.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("beschreibung"));
table.setItems(list);
}
}
GsonConverter类
public class GsonConverter {
public JiraTicketBody gson(String json)
{
Gson gson = new Gson();
JiraTicketBody BodyObj = gson.fromJson(json,JiraTicketBody.class);
return BodyObj;
}
}
主要课程
public class Main extends Application
{
//ViewXML
@Override
public void start(Stage primaryStage) throws IOException
{
Parent root = FXMLLoader.load(getClass().getResource("/view/ViewXML.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Ticket System Application");
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args)
{
try {
//ViewXML
launch(args);
RethinkDBConnect obj = new RethinkDBConnect();
obj.Connection();
} catch(Exception e) {
}
}
}
答案 0 :(得分:2)
您发布的代码存在两个问题。
如documentation中所述,Application.launch()
阻止,直到应用程序退出。因此,在应用程序处于关闭过程之前,您甚至不会创建RethinkDBConnection
类。您应该将start()
方法视为应用程序的入口点,并且launch()
方法中除了main(...)
之外不应该有任何代码。您在JavaFX应用程序中执行的任何操作都应该使用start(...)
或init(...)
方法,或从那里调用的方法等完成。
在这种情况下,由于您似乎不需要来自控制器外部的RethinkDBConnection
,我认为没有理由不从控制器本身创建RethinkDBConnect
。
您需要更新属于控制器的list
。相反,您正在创建一个恰好与控制器相同的类的新对象,并更新属于该对象的列表。显然,该列表与表格没有任何关系。您需要将对用作表的支持列表的实际列表的引用传递给RethinkDBController
实例。
所以你的代码应该是这样的:
public class RethinkDBConnection {
// public static final RethinkDB r = RethinkDB.r;
// GsonConverter con = new GsonConverter();
// JiraTicketBody body = new JiraTicketBody();
private final ObservableList<TicketDataProperty> dataList ;
public RethinkDBConnection(ObservableList<TicketDataProperty> dataList) {
this.dataList = dataList ;
}
public void connect(){
dataList.add(new TicketDataProperty
("test","test","test","test","test","test"));
}
}
然后在控制器中你可以这样做:
public class ViewController implements Initializable {
@FXML private TableView <TicketDataProperty> table;
@FXML private TableColumn <TicketDataProperty,String> key;
@FXML private TableColumn <TicketDataProperty,String> prioritaet;
@FXML private TableColumn <TicketDataProperty,String> erstellt;
@FXML private TableColumn <TicketDataProperty,String> status;
@FXML private TableColumn <TicketDataProperty,String> zustand;
@FXML private TableColumn <TicketDataProperty,String> beschreibung;
private ObservableList<TicketDataProperty> list = FXCollections.observableArrayList(
new TicketDataProperty("example","example","example","example","example","example")
);
@Override
public void initialize(URL location, ResourceBundle resources) {
key.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("key"));
prioritaet.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("prioritaet"));
erstellt.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("erstellt"));
status.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("status"));
zustand.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("zustand"));
beschreibung.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("beschreibung"));
table.setItems(list);
RethinkDBConnection connection = new RethinkDBConnection(list);
connection.connect();
}
}
您的Main
课程应该是:
public class Main extends Application
{
//ViewXML
@Override
public void start(Stage primaryStage) throws IOException
{
Parent root = FXMLLoader.load(getClass().getResource("/view/ViewXML.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Ticket System Application");
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
如果您确实需要访问控制器外的RethinkDBConnection
实例,请按如下方式修改控制器:
public class ViewController implements Initializable {
@FXML private TableView <TicketDataProperty> table;
@FXML private TableColumn <TicketDataProperty,String> key;
@FXML private TableColumn <TicketDataProperty,String> prioritaet;
@FXML private TableColumn <TicketDataProperty,String> erstellt;
@FXML private TableColumn <TicketDataProperty,String> status;
@FXML private TableColumn <TicketDataProperty,String> zustand;
@FXML private TableColumn <TicketDataProperty,String> beschreibung;
private ObservableList<TicketDataProperty> list = FXCollections.observableArrayList(
new TicketDataProperty("example","example","example","example","example","example")
);
@Override
public void initialize(URL location, ResourceBundle resources) {
key.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("key"));
prioritaet.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("prioritaet"));
erstellt.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("erstellt"));
status.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("status"));
zustand.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("zustand"));
beschreibung.setCellValueFactory(new PropertyValueFactory<TicketDataProperty,String>("beschreibung"));
table.setItems(list);
}
public ObservableList<TicketDataProperty> getDataList() {
return list ;
}
}
并使用此版本的Main
:
public class Main extends Application
{
//ViewXML
@Override
public void start(Stage primaryStage) throws IOException
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/ViewXML.fxml"));
Parent root = loader.load();
ViewController controller = loader.getController();
RethinkDBConnection connection = new RethinkDBConnection(controller.getDataList());
connection.connect();
Scene scene = new Scene(root);
primaryStage.setTitle("Ticket System Application");
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
请注意,我重命名了一些类和方法以遵守标准naming conventions。