Tutorial Using FXML with Runemate

Discussion in 'Tutorials & Resources' started by Baddest Man on Earth, Jan 4, 2015.

  1. Create your FXML file using SceneBuilder, do not set a controller in the fxml file.

    Code (Text):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2.  
    3. <?import java.lang.*?>
    4. <?import javafx.scene.layout.*?>
    5. <?import javafx.scene.control.*?>
    6. <?import javafx.scene.layout.AnchorPane?>
    7.  
    8. <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">
    9.  
    10.     <children>
    11.         <Label layoutX="14.0" layoutY="14.0" text="Blockades" />
    12.         <ListView fx:id="listBlockadeAvailable" layoutX="14.0" layoutY="31.0" prefHeight="141.0" prefWidth="96.0" />
    13.         <ListView fx:id="listBlockadeSelected" layoutX="154.0" layoutY="31.0" prefHeight="141.0" prefWidth="96.0" />
    14.         <Button fx:id="btnAddBlockade" layoutX="119.0" layoutY="44.0" mnemonicParsing="false" text="&gt;" />
    15.         <Button fx:id="btnRemoveBlockade" layoutX="119.0" layoutY="122.0" mnemonicParsing="false" text="&lt;" />
    16.         <Label layoutX="269.0" layoutY="14.0" text="Rune" />
    17.         <ComboBox fx:id="selectRune" layoutX="269.0" layoutY="32.0" prefWidth="150.0" />
    18.         <Label layoutX="269.0" layoutY="69.0" text="Teleport Method:" />
    19.         <ComboBox fx:id="selectTeleport" layoutX="269.0" layoutY="89.0" prefWidth="150.0" />
    20.         <Button fx:id="btnStart" layoutX="378.0" layoutY="339.0" mnemonicParsing="false" text="Start" />
    21.       <ListView fx:id="listPouchesAvailable" layoutX="14.0" layoutY="223.0" prefHeight="141.0" prefWidth="96.0" />
    22.       <ListView fx:id="listPouchesSelected" layoutX="154.0" layoutY="223.0" prefHeight="141.0" prefWidth="96.0" />
    23.       <Button fx:id="btnRemovePouch" layoutX="119.0" layoutY="320.0" mnemonicParsing="false" text="&lt;" />
    24.       <Button fx:id="btnAddPouch" layoutX="119.0" layoutY="242.0" mnemonicParsing="false" text="&gt;" />
    25.       <CheckBox fx:id="checkUsePouches" layoutX="14.0" layoutY="196.0" mnemonicParsing="false" text="Use Pouches" />
    26.     </children>
    27. </AnchorPane>
    28.  
    Create a class that extends Stage which is responsible for loading the FXML file and displaying the GUI.

    Code (Text):
    1. public class AbyssGui extends Stage {
    2.  
    3.     public AbyssGui(SupremeAbyss script) {
    4.         initModality(Modality.WINDOW_MODAL);
    5.         initStage(this, script);
    6.         show();
    7.     }
    8.  
    9.     public void initStage(Stage stage, SupremeAbyss script) {
    10.         try {
    11.             FXMLLoader loader = new FXMLLoader();
    12.            //Set your controller through the loader, not through the fxml file
    13.             loader.setController(new AbyssController(script, stage));
    14.            //Notice how I loaded it from the root of the project
    15.             final Parent root = loader.load(SupremeAbyss.class.getResourceAsStream("/org/wadiya/runemate/scripts/abyss/Abyss.fxml"));
    16.             final Scene scene = new Scene(root, 461, 378);
    17.             stage.setTitle("Supreme Abyss");
    18.             stage.setScene(scene);
    19.         } catch (IOException e) {
    20.             e.printStackTrace();
    21.         }
    22.     }
    23. }
    Instantiate the Stage in you onStart method in your main script bot class:

    Code (Text):
    1.     Platform.runLater(() -> gui = new AbyssGui(this));
    2.         while (gui == null || gui.isShowing()) {
    3.             try {
    4.                 Thread.sleep(500);
    5.             } catch (InterruptedException e) {
    6.                 e.printStackTrace();
    7.             }
    8.         }
     
    Arbiter and Ozzy like this.
  2. Thanks a bunch :)
     

Share This Page

Loading...