gpt4 book ai didi

JavaFX Textfield数据到不同Stage中的TableView

转载 作者:行者123 更新时间:2023-12-02 13:11:09 24 4
gpt4 key购买 nike

我正在尝试将新窗口中的TextField中的用户数据输入到主窗口中的TableView中。但它没有填充表,并且我没有收到任何异常。单击按钮后,新窗口就会关闭。我正在创建非常简单的应用程序,只是为了弄清楚如何在 Controller 之间正确通信。

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);
}
}

主窗口的 Controller 类,其中有用于打开新Stage的表格和按钮:

package sample;

import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;


public class Controller implements Initializable {
@FXML TableView<radnici> tabela;
@FXML TableColumn<radnici, String> kolona;
@FXML Button dodajRadnikaDugme;
@FXML Button closeDugme;

Main main = new Main();
static Stage stage = new Stage();
Scene scene;
BorderPane borderPane;

@Override
public void initialize(URL location, ResourceBundle resources) {
kolona.setCellValueFactory(new PropertyValueFactory<radnici, String>("ime"));
}

public TableView<radnici> getTabela() {
return tabela;
}

public TableColumn<radnici, String> getKolona() {
return kolona;
}

//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();
scene = new Scene(borderPane);
stage.setTitle("Dodaj Radnika");
stage.setScene(scene);
stage.showAndWait();
}

//Closing main window
public void closeDugmeKlik(){
main.closeStage();
}

//Method for closing new window
public void CloseStage(){
stage.close();
}
}

新窗口的 Controller 类,其中只有 TextField 和 Button:

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import java.io.IOException;

public class dodajRadnikaKontroler {
@FXML TextField upišiRadnika;
@FXML Button dodajRadnika;

BorderPane borderPane;

public void initialize(){
System.out.println("učitavanje podataka...");
}

//Method for adding data on button clicked from TextField into table in main window
@FXML public void dodajRadnikaKlik() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("sample.fxml"));
borderPane = new BorderPane();
borderPane = loader.load();
Controller controller = loader.getController();
ObservableList<radnici> lista = FXCollections.observableArrayList();
lista.add(new radnici(upišiRadnika.getText()));
controller.tabela.setItems(lista);
upišiRadnika.clear();
controller.CloseStage();
}
}

worker 模型类(“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;
}
}

请有人帮助我,以便我最终能正确地做到这一点。

最佳答案

注意:请使用proper naming conventions 。我更改了一些类的名称,以便它们遵循标准约定。

您看不到表格的更改,因为您正在从 FXML 文件加载全新的 UI 结构,包括新的 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();

由于您甚至没有显示这个新 UI,因此您看不到表格中的项目。

大概您真正想要做的是更新现有表中的项目。为此,您所需要的只是对表的支持列表的引用。您需要将其传递给第二个 FXML 文件的 Controller 。向 DodajRadnikaKontroler 添加字段和方法:

public class DodajRadnikaKontroler {

// Existing code omitted:

private ObservableList<Radnici> items ;

public void setItems(ObservableList<Radnici> items) {
this.itmes = items ;
}
}

然后在加载 FMXL 文件时将表的列表传递给新 Controller :

//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();
}

关于JavaFX Textfield数据到不同Stage中的TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com