0

I'm trying to change the visibility for some TextFields and to initialize a new Tooltip in a method that is called from another controller.

So in a parent window I have a TextField setted to not visibile by default and a button to open a new child window (setted as showAndWait). In the child window I have a TableView and when a value is selected from it the TextField should appear in the parent window with the value selected setted in the tooltip.

So from the code below I only get the printed message from the method. I'm most likely wrong but my best guess is I should call parent's initialize method when selecting the value from child.

// parent fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxapplication4.JavaFXApplication4">
    <children>
      <TextField fx:id="textField" layoutX="208.0" layoutY="107.0" prefHeight="25.0" prefWidth="195.0" visible="false" />
      <Button fx:id="toChild" layoutX="262.0" layoutY="200.0" onAction="#toChild" mnemonicParsing="false" text="Child window" />
   </children>
</AnchorPane>

// parent controller

package javafxapplication4;

import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


public class JavaFXApplication4 extends Application {

    @FXML
    public TextField textField;

    @Override
    public void start(Stage primaryStage) throws IOException {
      Parent root = FXMLLoader.load(getClass().getResource("Parent.fxml"));
        Scene scene = new Scene(root);
       primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }

// called onmouseclick by child's tableview
    public void execute()
    {

        System.out.println("Execute called... He wants his money back!");
     textField.setTooltip(new Tooltip("Value selected: "));
     textField.setVisible(true);

    }


// button for child window 

    public void toChild() throws IOException{
     FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getClassLoader().getResource("javafxapplication4/Child.fxml"));
        Parent parent = loader.load();
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setScene(new Scene(parent));
        stage.showAndWait();
    }
}

// child fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxapplication4.ChildController">
   <children>
      <TableView fx:id="table" layoutX="101.0" layoutY="100.0" onMouseClicked="#select" prefHeight="200.0" prefWidth="442.0">
        <columns>
          <TableColumn prefWidth="75.0" text="C1" />
          <TableColumn prefWidth="75.0" text="C2" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

//child controller

package javafxapplication4;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.TableView;


public class ChildController implements Initializable {

  @FXML
  public TableView table;

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

    }    

// onmouseclick method for tableview
    public void select() throws IOException
    { 
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getClassLoader().getResource("javafxapplication4/Parent.fxml"));
        Parent parent = loader.load();
        JavaFXApplication4 controller = loader.getController();
        Class value = (Class) table.getSelectionModel().getSelectedItem();

        table.setOnMouseClicked(event -> {

        controller.execute();
      ((Node)(event.getSource())).getScene().getWindow().hide();

      }   );}}

Edit: So basically I've tried to set a new tooltip for the Textfield in the parent window through a method in the parent controller which I called from the child controller. I also tried by using controller.textField.setTooltip(new Tooltip("...")) in the child controller but no success. I guess I have to reload the parent window after the modifications but I've got no idea how. (the problem with setting the visibility I think is from the same issue)

Thanks.

Vlad
  • 27
  • 1
  • 6
  • Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – Slaw May 27 '19 at 21:46
  • Well kind of but not really. I managed the data transfer part but I don't know how to set the properties for the input elements. -> I can set data from one controller to a textField located in another controller. -> I can't set new properties to that textField from another controller. Something like this. Seems simple but it just doesn't work. Thanks anyway. The answer to that question is really close. – Vlad May 30 '19 at 17:26

0 Answers0