【问题标题】:Set value of TextField from another window in javafx从 javafx 中的另一个窗口设置 TextField 的值
【发布时间】:2019-10-05 12:31:11
【问题描述】:

我在 Action(按 Enter)上设置了一个 TextField,以打开另一个 fxml 窗口,该窗口显示一个选项表(数百个选项)。基本上我需要第二个窗口在第一个窗口上设置文本字段的文本。

@FXML //this pops out a 2nd window where i can choose a person. Set from Scene Builder
private void pickperson(ActionEvent event) throws IOException {
    Parent parent = FXMLLoader.load(getClass().getResource("/fxml/personpicker.fxml"));
    Scene scene = new Scene(parent);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.centerOnScreen();
    stage.show();
} 

@FXML //when i click "use selected" this gets executed
private void use(ActionEvent event) {
    Person person0 = table.getSelectionModel().getSelectedItem();
    int id = person0.getId();
    String name = person0.getNAME();
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    final ClipboardContent content = new ClipboardContent();
    content.putString(Integer.toString(id)); //i tried clipboard but when i paste, nothing is pasted
    Stage stage = (Stage) useselected.getScene().getWindow();//closes the window
    stage.close();
}

我在第二个窗口有一个表格,上面有一个标有“使用选定”的按钮。我想让它在单击“使用选定的”时关闭窗口,同时从选择中设置文本字段。

编辑: 我通过添加

让剪贴板工作
Clipboard.getSystemClipboard().setContent(content);

现在,我只需要在窗口关闭后直接粘贴值;就像按下了 CRTL+V 一样。

【问题讨论】:

  • 当您在第二个Stage 中执行操作时,您不需要使用系统剪贴板来更新一个Stage 中的文本字段的内容。为了向您展示如何做到这一点,如果您发布 minimal reproducible example 会有所帮助。包括 Java 代码和 FXML 文件的内容。
  • @Abra 我希望为时不晚。我制作了我正在尝试做的事情的准系统版本。 github.com/alexislyndon/testcodes

标签: java mysql javafx fxml scenebuilder


【解决方案1】:

根据您的代码,“父”Stage,即包含TextField 的那个是显示三个按钮的Stage 的所有者。因此,您可以简单地在子 Stage 中调用方法 getOwner() 以访问父 Stage。获得对父 Stage 的引用后,您就可以访问其节点并对其进行操作。

我只更改了您代码中的两个文件。

  1. Parent.fxml - 我将 id 属性添加到 TextField
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="266.0" prefWidth="394.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.ParentController">
   <children>
      <TextField id="txtFld" layoutX="123.0" layoutY="121.0" onAction="#picker" />
      <Label layoutX="140.0" layoutY="86.0" text="Press enter to choose" />
   </children>
</AnchorPane>
  1. ChildController.java - 我添加了方法handleEvent(int)
package test;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class ChildController implements Initializable {
    @FXML
    AnchorPane ap;
    @FXML
    private Button btnone;
    @FXML
    private Button btntwo;
    @FXML
    private Button btnthree;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    @FXML
    private void one(ActionEvent event) {
        handleEvent(1);
    }

    @FXML
    private void two(ActionEvent event) {
        handleEvent(2);
    }

    @FXML
    private void three(ActionEvent event) {
        handleEvent(3);
    }

    private void handleEvent(int chosenNumber) {
        Stage stage = (Stage) ap.getScene().getWindow();
        Stage owner = (Stage) stage.getOwner();
        Scene scene = owner.getScene();
        Parent root = scene.getRoot();
        TextField txtFld = (TextField) root.lookup("#txtFld");
        txtFld.setText(String.valueOf(chosenNumber));
        stage.close();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2015-01-25
    • 2018-12-11
    相关资源
    最近更新 更多