Pranesh
Pranesh

Reputation: 43

calling java fxml from swing

i am using javafxml and swing in a single program. when i click a button on swing i need a text to be displayed in javafx panel. how to obtain fxml controller's object so that i can use it to update the changes in FX panel? Can any one help to solve this please ? Thanks in advance !

We have tried to access swing from FX where we succeeded. But the other part is not possible.

sample.java :

public void actionPerformed(java.awt.event.ActionEvent e) 
{
     FXMLLoader loader = new FXMLLoader(
                getClass().getResource(
                  "FXMLDocument.fxml"
                )
              );
     FXMLDocumentController controller = 
                loader.<FXMLDocumentController>getController();
              controller.updatePage("hello boss");
}

FXMLDocument.fxml:

< GridPane fx:controller="com.comp.sweta.FXMLDocumentController" xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"> < /HBox>

< Text fx:id="actiontarget" GridPane.columnIndex="1" GridPane.rowIndex="6"/>

< /GridPane>

FXMLDocumentController class:

public class FXMLDocumentController 
{
@FXML public Text actiontarget;

public void updatePage(String data){
    System.out.println("Testing phase");
    this.actiontarget.setText(data);
}

}

we are getting NullPointerException. not able to set text in fxpanel this way.

Upvotes: 0

Views: 308

Answers (1)

Jurgen
Jurgen

Reputation: 2154

I think you are missing a line between your first 2 statements in the actionPerformed method:

public void actionPerformed(java.awt.event.ActionEvent e) 
{
     FXMLLoader loader = ...;

     Parent  gridpane = (Parent) loader.load();

     FXMLDocumentController controller = ....;
     controller.updatePage("hello boss");
}

Upvotes: 0

Related Questions