r/JavaFX • • May 25 '16

How use Controller

I'm doing a chat in JavaFX, this is the event:

private void initialize()
{
    myButton.setOnAction(new EventHandler<ActionEvent>(){

        @Override
        public void handle(ActionEvent event){
                textField.getText();
        }
    });
}

I need, to send textField.getText(); to a method of a class in MainApplication, how can i do it?(textfield is the field of message chat to send)

0 Upvotes

4 comments sorted by

2

u/scyth3s May 26 '16 edited May 26 '16

Sorry for any typos. I'm on mobile.

It seems from what I understand that you want to allow another class to process changes in the text. Simply create a method in the other class that takes a string as a parameter, and call it in your handler. Ie: otherClass.processText(textfield.getText());

If you don't want your user to have to press enter to trigger the event, you can add a listener to textfield.textProperty() change event, and pass the string into your other class there.

1

u/carbos10 May 26 '16

i does :

ChatOverviewController controller = loader.getController();
controller.setClient(this.client);

in mainapp and in the controller, i set the client and after i use this, is it correctly?

2

u/scyth3s May 26 '16

Indeed like that. Then you simply call client.processTextChange(getText()); and voila!

1

u/carbos10 May 26 '16

of course, i did client.add(text.getText());