0

I am developing a small, fake, Mail Client in JavaFXML. It offers a Listview with messages written on a txt file, a TextArea which prints selected message and some buttons. Here's an Image of the Main View: https://ibb.co/iKN2rm I already took care of "New Message" Button, which is launching a new FXML View and works well.

Here's an Image of the "New_Message" View: https://ibb.co/hQf5Bm

Now I'm trying to implement the "Reply" button, which should launch the same View as before (New Message) but set on all three TextFields strings taken from the Main View, such as Message's text, recipient and Message Argument.

Below the New Message Button Handler:

private void handle_new(ActionEvent event) {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Client/Resources/new_utente1.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setTitle("New Message");
        stage.setScene(new Scene(root1));
        stage.show();
    } catch (Exception ecc) {
        System.out.println("ERROR: " + ecc.getMessage());
    }
}

I tried to implement the handle_reply method, but I'm not able to add parameters because FXML file won't find the method if I do so.

Below a small part of the FXML file:

<TextArea fx:id = "testo" editable="false" layoutX="283.0" layoutY="53.0" prefHeight="450.0" prefWidth="490.0" promptText="Select a message and it will be displayed here..." />
      <Button id="nuovo" layoutX="46.0" layoutY="521.0" onAction = "#handle_new" mnemonicParsing="false" prefHeight="25.0" prefWidth="173.0" text="New Message" />
      <Button id="reply" layoutX="283.0" layoutY="521.0" onAction = "#handle_reply" mnemonicParsing="false" prefHeight="25.0" prefWidth="132.0" text="Reply" />

My question is: How do I implement the "handle_reply" method as described before? Thank you

Sergio
  • 354
  • 1
  • 12
  • 2
    You can get those values from the text fields in the controller and [pass them to the new fxml's controller](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). – James_D Jan 09 '18 at 11:41

2 Answers2

0

Unfortunately it's not possible to use handlers with parameters. You should use different handler in each case.

Had this problem some time ago too. As a solution you could perform small refactoring to minimize code duplicating.

And if you know javascript you can play with this instead of using java handlers: https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#scripting

Edit: also you can check this answer: https://stackoverflow.com/a/37902780/5572007 (pretty the same)

amseager
  • 5,795
  • 4
  • 24
  • 47
0

Create a Controller class

then connect this class to your view

private void handle_new(ActionEvent event) {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/Client/Resources/new_utente1.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        CController ctr = fxmlLoader.getController();
        ctr.setLabelText("asdsad");
        Stage stage = new Stage();
        stage.setTitle("New Message");
        stage.setScene(new Scene(root1));
        stage.show();
    } catch (Exception ecc) {
        System.out.println("ERROR: " + ecc.getMessage());
    }
}

and in your Controllers you'll do

public class CController implements Initializable {

    @FXML TextArea testo;

    @Override
    public void initialize(URL location, ResourceBundle resources) {}

    public void setLabelText(String text){testo.setText(text);}
}
fabian
  • 80,457
  • 12
  • 86
  • 114
mBogaz
  • 136
  • 12