在javafx中从另一个窗口设置TextField的值
我在Action(按enter键)上设置了一个TextField以打开另一个fxml窗口,该窗口显示一个选项表(数百个选项)。基本上,我需要第二个窗口来设置第一个窗口上文本字段的文本在javafx中从另一个窗口设置TextField的值,java,mysql,javafx,fxml,scenebuilder,Java,Mysql,Javafx,Fxml,Scenebuilder,我在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 { Par
@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。根据您的代码,“父级”
阶段
,即包含文本字段
,是显示三个按钮的阶段的所有者。因此,您只需在子阶段
中调用方法即可访问父阶段
。一旦对父级阶段
进行了引用,就可以访问其节点并对其进行操作
我只更改了你代码中的两个文件
Parent.fxml
-我将id属性添加到TextField
在第二个阶段执行操作时,不需要使用系统剪贴板来更新其中一个文本字段的内容。为了向你展示如何做到这一点,如果你发布一个。包括java代码和FXML文件的内容。@Abra我希望不会太晚。我做了一个赤裸裸的版本,我想做的事。
<?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>