Close JDialog from JavaFx Button

2019-07-27 22:49发布

问题:

I need to close my JDialog from JavaFx Button defined in my FXML file. I post code of my class called by Main Application:

ExampleWindow.java

public class ExampleWindow extends JDialog 
{
@FXML
Button closeButton;

public ExampleWindow()
{
}

public void initAndShowGUI()
{
    final JFXPanel fxPanel = new JFXPanel();
    add(fxPanel);

    Platform.runLater(new Runnable()
    {
        @Override
        public void run()
        {
            AnchorPane parent = null;
            FXMLLoader fxmlLoader = new FXMLLoader();

            fxmlLoader.setRoot(this);
            try {
                parent = fxmlLoader.load(getClass().getResource("WindowControlPanel.fxml"));
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            scene = new Scene(parent);
            fxPanel.setScene(scene);
        }
    });
}

public void onAction(ActionEvent ac)
{
this.dispose();
}

}

Method onAction is called by JavaFx Button (on FXML file)

WindowControlPanel.fxml

<AnchorPane id="AnchorPane" fx:id="windowPanel" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="1024.0" style="-fx-border-color: white, grey; -fx-border-width: 2, 1; -fx-border-insets: 0, 0 1 1 0" xmlns:fx="http://javafx.com/fxml" fx:controller="ExampleWindow">
<children>
<FlowPane alignment="CENTER_RIGHT" columnHalignment="CENTER" prefHeight="50.0" prefWidth="1024.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
  <children>
    <Button fx:id="closeButton" mnemonicParsing="false" onAction="#onAction" prefHeight="35.0" prefWidth="100.0" text="Close">
      <FlowPane.margin>
        <Insets bottom="5.0" left="20.0" right="20.0" top="5.0" />
      </FlowPane.margin>
    </Button>
  </children>
</FlowPane>
</children>
</AnchorPane>

When I pressed closeButton, method onAction is correctly called, but my JDialog doesn't close. Any ideas? Where am I wrong?