1
votes

I'm trying to make GUI for my application written in Java.

I made fxml document with Scene Builder, set fx:id properly and now I'm trying to make simple changes in form.

My DocumentController:

public class FXMLDocumentController implements Initializable {
@FXML    
Label LabelDatum;



@Override
public void initialize(URL url, ResourceBundle rb) {
     LabelDatum.setText((new Date()).toString());
}    
}

My FX main file:

public class FXMain extends Application {

@Override
public void start(Stage primaryStage) throws IOException {
   Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
   Scene scene = new Scene(root);

   primaryStage.setScene(scene);
   primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}    
}

Everything I want to now, it's to set LabelDatum to actual timestamp, but when i run FX main file, nothing happens. Is there anybody, who is able to help me?

Thank you. Paul

UPDATE:

My whole FXMLDocument:

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

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

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="478.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <AnchorPane prefHeight="116.0" prefWidth="600.0" BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="LabelNadpis" layoutX="14.0" layoutY="16.0" text="Jídelní lístek Dne">
               <font>
                  <Font name="System Bold" size="24.0" />
               </font>
            </Label>
            <Label fx:id="LabelDatum" layoutX="237.0" layoutY="18.0" prefHeight="32.0" prefWidth="127.0" text="datum">
               <font>
                  <Font size="22.0" />
               </font>
            </Label>
            <Label fx:id="LabelNazevRestauraceNadpis" layoutX="14.0" layoutY="55.0" prefHeight="17.0" prefWidth="99.0" text="Název restaurace" />
            <Label fx:id="LabelSestavilNadpis" layoutX="14.0" layoutY="72.0" text="Sestavil" />
            <Label fx:id="LabelNazevRestauraceHodnota" layoutX="237.0" layoutY="55.0" text="nazev_restaurace" />
            <Label fx:id="LabelSestavilHodnota" layoutX="237.0" layoutY="72.0" text="sestavil" />
         </children>
      </AnchorPane>
   </top>
   <center>
      <ListView fx:id="ListViewPokrmy" prefHeight="231.0" prefWidth="600.0" BorderPane.alignment="CENTER" />
   </center>
   <bottom>
      <AnchorPane prefHeight="46.0" prefWidth="696.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="ButtonNactiTxt" layoutX="26.0" layoutY="12.0" mnemonicParsing="false" text="Načti txt" />
            <Button fx:id="ButtonVlozNovy" layoutX="97.0" layoutY="12.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="83.0" text="Vlož nový" />
            <Button fx:id="ButtonOdeber" layoutX="188.0" layoutY="12.0" mnemonicParsing="false" text="Odeber" />
            <Button fx:id="ButtonEditace" layoutX="250.0" layoutY="12.0" mnemonicParsing="false" text="Editace" />
            <ChoiceBox layoutX="316.0" layoutY="12.0" prefWidth="150.0" />
            <Button fx:id="ButtonZrusListek" layoutX="472.0" layoutY="11.0" mnemonicParsing="false" text="Zruš lístek" />
            <Button fx:id="ButtonUloz" layoutX="550.0" layoutY="11.0" mnemonicParsing="false" text="Ulož" />
            <Button fx:id="ButtonObnov" layoutX="597.0" layoutY="11.0" mnemonicParsing="false" text="Obnov" />
         </children>
      </AnchorPane>
   </bottom>
</BorderPane>

Everything I want: LabelDatum with actual timestamp. Happens: the scene shows up, but the initial label text doesn't change to the date

2

2 Answers

1
votes

Your FXML file does not specify a controller. You need to add a fx:controller attribute into the root element:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" 
  minHeight="-Infinity" minWidth="-Infinity" 
  prefHeight="478.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/8" 
  xmlns:fx="http://javafx.com/fxml/1"
  fx:controller="my.package.FXMLDocumentController">

(replace my.package with the actual package your controller class is defined in).

1
votes

You need to set the controller class in SceneBuilder (which will set it in the FXML file).

gfycat

(Click on the image for larger resolution, faster loading and better quality)