0

I am writing a program for a school project using JavaFX. I am designing the UI in SceneBuilder 8, and it is generating FXML for me. My question is, how do I connect my buttons and sliders and whatnot to my java code? Is it anything like coding in buttons in XCode?

Here is my FXML source if it helps:

<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<SplitPane dividerPositions="0.3996655518394649" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="470.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="145.0">
         <children>
            <TableView layoutY="33.0" prefHeight="323.0" prefWidth="180.0" AnchorPane.bottomAnchor="42.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="33.0">
              <columns>
                <TableColumn prefWidth="77.0" text="Event" />
                <TableColumn prefWidth="101.0" text="Suggested time" />
              </columns>
               <columnResizePolicy>
                  <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
               </columnResizePolicy>
               <effect>
                  <Blend />
               </effect>
            </TableView>
            <Label layoutY="6.0" prefHeight="27.0" prefWidth="90.0" text="Event view">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <Button layoutY="368.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="34.0" text="+">
               <font>
                  <Font name="System Bold" size="14.0" />
               </font>
            </Button>
         </children></AnchorPane>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="267.0">
         <children>
            <Label layoutY="7.0" prefHeight="27.0" prefWidth="90.0" text="Detail view">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <GridPane layoutX="14.0" layoutY="34.0" prefHeight="120.0" prefWidth="258.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="124.0" minWidth="10.0" prefWidth="87.0" />
                <ColumnConstraints hgrow="SOMETIMES" maxWidth="176.0" minWidth="10.0" prefWidth="171.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="30.0" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="75.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="60.0" minHeight="10.0" prefHeight="31.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="25.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label text="Name" />
                  <Label text="Details" GridPane.rowIndex="1" />
                  <Label text="Estimated time" GridPane.rowIndex="2" />
                  <TextField promptText="Enter name" GridPane.columnIndex="1" />
                  <TextField promptText="Enter estimated time" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                  <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Enter details" GridPane.columnIndex="1" GridPane.rowIndex="1" />
               </children>
            </GridPane>
            <Button layoutX="219.0" layoutY="372.0" mnemonicParsing="false" text="Done" />
            <Button layoutX="152.0" layoutY="372.0" mnemonicParsing="false" text="Cancel" />
         </children></AnchorPane>
  </items>
</SplitPane>

Thank you for any help you can give!

EDIT: To clarify, I have an FXML file, a controller class, and a constructor class. I need to connect to my controller class.

1

1 Answer 1

2

Once you have your controller class defined, you can link your FXML file to it. In SceneBuilder, expand the "Controller" titled pane in the bottom left, and type in the fully-qualified name of the controller class.

To specify a handler method for a button, select the button, and then expand the "Code" titled pane (bottom pane of the right panel). You can, for example, add the method name for an action handler in the "On Action" field.

enter image description here

If you are doing this without SceneBuilder, you add the attribute fx:controller="..." to the root element of the FXML, specifying the fully-qualified class name. For the button, you can add onAction="#methodName" to the <Button ... > element, specifying the name of the method to execute when the button is pressed. The method should either take a single parameter of type ActionEvent, or no parameters.

2
  • In the controller pane, would i write ControllerClassName or ControllerClassName.java? Apr 20, 2015 at 1:38
  • The class name, ControllerClassName, not the filename. (The source file will not even be available at runtime.)
    – James_D
    Apr 20, 2015 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.