JavaFX文本字段数据到不同阶段的TableView
我正在尝试将用户数据从新窗口的TextField输入主窗口的TableView。但它并没有填充表,我也没有得到任何异常。点击按钮后,新窗口将关闭。我正在创建一个非常简单的应用程序,只是为了弄清楚如何在控制器之间正确地通信JavaFX文本字段数据到不同阶段的TableView,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我正在尝试将用户数据从新窗口的TextField输入主窗口的TableView。但它并没有填充表,我也没有得到任何异常。点击按钮后,新窗口将关闭。我正在创建一个非常简单的应用程序,只是为了弄清楚如何在控制器之间正确地通信 package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scen
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
static Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
this.primaryStage= primaryStage;
this.primaryStage.setTitle("Hello World");
this.primaryStage.setScene(new Scene(root));
this.primaryStage.show();
}
public void closeStage(){
primaryStage.close();
}
public static void main(String[] args) {
launch(args);
}
}
主窗口的控制器类,其中有用于打开新阶段的表和按钮:
radnici工人模型等级:
package sample;
public class radnici {
private String ime;
public radnici(String ime) {
this.ime = ime;
}
public String getIme() {
return ime;
}
public void setIme(String ime) {
this.ime = ime;
}
}
请有人帮助我,这样我才能最终正确地完成这项工作。注意:请使用。我已经更改了一些类的名称,以便它们遵循标准约定
您看不到对表的更改,因为您正在从FXML文件加载一个完整的新UI结构,包括一个新的TableView,然后在新的TableView中设置项目
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("sample.fxml"));
borderPane = new BorderPane();
// load a new UI from the FXML file:
borderPane = loader.load();
// get the controller for the new UI:
Controller controller = loader.getController();
ObservableList<radnici> lista = FXCollections.observableArrayList();
lista.add(new radnici(upišiRadnika.getText()));
// set the table items in the new UI:
controller.tabela.setItems(lista);
upišiRadnika.clear();
controller.CloseStage();
然后在加载FMXL文件时将表格列表传递给新控制器:
//Opening new Stage on button clicked
public void dodajRadnikaDugemKlik() throws IOException {
FXMLLoader loader = new FXMLLoader();
borderPane = new BorderPane();
loader.setLocation(getClass().getResource("dodajRadnika.fxml"));
borderPane = loader.load();
DodajRadnikaKontroler controller = loader.getController();
controller.setItems(tabela.getItems());
scene = new Scene(borderPane);
stage.setTitle("Dodaj Radnika");
stage.setScene(scene);
stage.showAndWait();
}
现在在dodajRadnikaKlik方法中,您只需更新列表:
@FXML public void dodajRadnikaKlik() throws IOException {
// if you want to add an item:
items.add(new radnici(upišiRadnika.getText()));
// if you want to replace all items with the new one:
// items.setAll(new radnici(upišiRadnika.getText()));
upišiRadnika.clear();
controller.CloseStage();
}
非常感谢,这很有效。几天来我一直在想这个问题。但我有一些不同的逻辑——那就是我没有完全理解它。当您将空表中的空列表传递给setItems方法中的参数,然后使用用户输入更新DodajRadnikaklik中的列表项时,它应该覆盖该列表。为什么它会填充表的列表,即表,而不仅仅是项目列表(未设置为表的列表)?我们使用tabela.getItems只是为了计算列表项?此列表项是如何连接到表的列表的。@Kvark900 tabela.getItems提供了对该表用作其数据源的列表的引用。因此,如果您向该列表中添加了一些内容,则会向表中显示的数据中添加一些内容。
public class DodajRadnikaKontroler {
// Existing code omitted:
private ObservableList<Radnici> items ;
public void setItems(ObservableList<Radnici> items) {
this.itmes = items ;
}
}
//Opening new Stage on button clicked
public void dodajRadnikaDugemKlik() throws IOException {
FXMLLoader loader = new FXMLLoader();
borderPane = new BorderPane();
loader.setLocation(getClass().getResource("dodajRadnika.fxml"));
borderPane = loader.load();
DodajRadnikaKontroler controller = loader.getController();
controller.setItems(tabela.getItems());
scene = new Scene(borderPane);
stage.setTitle("Dodaj Radnika");
stage.setScene(scene);
stage.showAndWait();
}
@FXML public void dodajRadnikaKlik() throws IOException {
// if you want to add an item:
items.add(new radnici(upišiRadnika.getText()));
// if you want to replace all items with the new one:
// items.setAll(new radnici(upišiRadnika.getText()));
upišiRadnika.clear();
controller.CloseStage();
}