简体   繁体   中英

How to call javafx from a normal java class

I want to call/open gui/fxml file from a normal class, i'm really having difficulties in finding a way to do this, I've try to instantiate the controller class but i'm not getting start function, Is it even possible to do what i'm trying to do?

normal class

    public class ReadXMLFile {

  public static void main(String argv[]) {

    }


}

fxml document

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane id="AnchorPane" prefHeight="442.0" prefWidth="449.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="gis_map.CitizenScheduledController">
   <children>
      <TableView layoutX="1.0" layoutY="-1.0" prefHeight="384.0" prefWidth="447.0">
        <columns>
          <TableColumn prefWidth="75.0" text="CitizenId" />
          <TableColumn prefWidth="75.0" text="Name" />
            <TableColumn prefWidth="75.0" text="Address" />
            <TableColumn prefWidth="75.0" text="Arrival" />
            <TableColumn prefWidth="75.0" text="Departure" />
            <TableColumn prefWidth="75.0" text="Actions" />
        </columns>
      </TableView>
      <Button layoutX="373.0" layoutY="396.0" mnemonicParsing="false" text="Approve" />
   </children>
</AnchorPane>

controllerClass

    package gis_map;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;


public class CitizenScheduledController implements Initializable {


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

}

Is it possible to do what I'm trying to do?

No (at least, not easily, or in a way that makes sense).

The lifecycle of a JavaFX application is controlled by an instance of the Application class. Furthermore, you need to start the JavaFX toolkit and set the FX application thread running. The static Application.launch(...) method accomplishes this, as well as creating the required instance of your Application class and calling the appropriate lifecycle methods on it.

So, just follow the standard pattern and make your ReadXMLFile class into a subclass of Application , make the main(...) method just call launch() , and define the start() method to load the FXML, etc.

For the sake of completeness:

Starting with JavaFX 9 it's possible to start up a application without relying on Application.launch . You can use Platform.startup for this purpose. Note that you're only allowed to use Platform.startup and/or Application.launch once in your application.

public static void main(String[] args) {
    Platform.startup(() -> {
        // display empty scene after startup
        Scene scene = new Scene(new Pane());
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    });
}

It's usually better to use the normal application lifecycle and use a class extending Application as entry point and use the init / start methods for any initialisations you'd usually do from the main method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM