Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Tutorial Using FXML with Runemate

Joined
Nov 26, 2014
Messages
616
Create your FXML file using SceneBuilder, do not set a controller in the fxml file.

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

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="378.0" prefWidth="433.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">

    <children>
        <Label layoutX="14.0" layoutY="14.0" text="Blockades" />
        <ListView fx:id="listBlockadeAvailable" layoutX="14.0" layoutY="31.0" prefHeight="141.0" prefWidth="96.0" />
        <ListView fx:id="listBlockadeSelected" layoutX="154.0" layoutY="31.0" prefHeight="141.0" prefWidth="96.0" />
        <Button fx:id="btnAddBlockade" layoutX="119.0" layoutY="44.0" mnemonicParsing="false" text="&gt;" />
        <Button fx:id="btnRemoveBlockade" layoutX="119.0" layoutY="122.0" mnemonicParsing="false" text="&lt;" />
        <Label layoutX="269.0" layoutY="14.0" text="Rune" />
        <ComboBox fx:id="selectRune" layoutX="269.0" layoutY="32.0" prefWidth="150.0" />
        <Label layoutX="269.0" layoutY="69.0" text="Teleport Method:" />
        <ComboBox fx:id="selectTeleport" layoutX="269.0" layoutY="89.0" prefWidth="150.0" />
        <Button fx:id="btnStart" layoutX="378.0" layoutY="339.0" mnemonicParsing="false" text="Start" />
      <ListView fx:id="listPouchesAvailable" layoutX="14.0" layoutY="223.0" prefHeight="141.0" prefWidth="96.0" />
      <ListView fx:id="listPouchesSelected" layoutX="154.0" layoutY="223.0" prefHeight="141.0" prefWidth="96.0" />
      <Button fx:id="btnRemovePouch" layoutX="119.0" layoutY="320.0" mnemonicParsing="false" text="&lt;" />
      <Button fx:id="btnAddPouch" layoutX="119.0" layoutY="242.0" mnemonicParsing="false" text="&gt;" />
      <CheckBox fx:id="checkUsePouches" layoutX="14.0" layoutY="196.0" mnemonicParsing="false" text="Use Pouches" />
    </children>
</AnchorPane>

Create a class that extends Stage which is responsible for loading the FXML file and displaying the GUI.

Code:
public class AbyssGui extends Stage {

    public AbyssGui(SupremeAbyss script) {
        initModality(Modality.WINDOW_MODAL);
        initStage(this, script);
        show();
    }

    public void initStage(Stage stage, SupremeAbyss script) {
        try {
            FXMLLoader loader = new FXMLLoader();
           //Set your controller through the loader, not through the fxml file
            loader.setController(new AbyssController(script, stage));
           //Notice how I loaded it from the root of the project
            final Parent root = loader.load(SupremeAbyss.class.getResourceAsStream("/org/wadiya/runemate/scripts/abyss/Abyss.fxml"));
            final Scene scene = new Scene(root, 461, 378);
            stage.setTitle("Supreme Abyss");
            stage.setScene(scene);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Instantiate the Stage in you onStart method in your main script class:

Code:
    Platform.runLater(() -> gui = new AbyssGui(this));
        while (gui == null || gui.isShowing()) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
 
Top