Set value of TextField from another window in javafx

xyzit

I have a TextField set on Action(press enter) to open another fxml window that shows a table of choices(hundreds of choices). Basically I need that 2nd window to set the text of the textfield on the first window.

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

I have a table on the 2nd window with a button labeled: "use selected". I want to make it so that the moment click "use selected", the window closes and at the same time set the text field from the selection.

Edit: I got the clipboard to work by adding

Clipboard.getSystemClipboard().setContent(content);

Now, I just need to paste the value directly after the window closes; as if CRTL+V was pressed.

Abra

According to your code, the "parent" Stage, i.e. the one containing the TextField, is the owner of the Stage displaying the three buttons. Hence you can simply call method getOwner() in the child Stage in order to access the parent Stage. Once you have a reference to the parent Stage, you can access its nodes and manipulate them.

I only changed two files in your code.

  1. Parent.fxml - I added the id attribute to 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 - I added the method 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();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaFX Set an Input filter by Pattern on a TextField

How to add value to set from another set?

How to set a values for combobox based on a clicked value from another combobox using javafx

Set KeyPressed event for a TextField in JavaFX

JavaFX Resizing TextField with Window

Set value from another dataframe

Java: How to set a value from javascript to a wicket component textfield

JavaFx set Label text from another controller

Flutter set value to a textfield

Can I set the value of a TextField in one scene from another scene, while both scenes are showing?

How do you set the value of a TextField from Dropdown Selection?

How can I acess value from textfield in another module in reactJS

How to transfer a container from one window to another in JavaFX

Getting the value of TextField from one class to another with button

Java Swing - How to get the value of text in a TextField from another class

Return List value from another window

Java - adding value from TextField to object constructor of another class

How can I get an image to show from another window in javafx?

Can't set the value of a static TextField in javafx

How to set a window always on top of another specific window in javafx

Set value of textfield with javascript

Javafx label not updating after input from TextField in another scene

set caretPosition to the right in a textField (javaFX)

How can I access controls of a JavaFX window from another JavaFX window?

How to set value to second page textfield from Main page textfield

How to get TextField value from another class or method

How to get the value of a textfield into another Textfield?

In JavaFX, Is it possible to enable a CheckBox when the TextField's value is not set to Zero

react set value to a textfield