0

I have 3 classes with which I'm currently working: ButtonMove.java, MainController.java, and Main.fxml. I've been experimenting with SceneBuilder to create user interfaces and wanted to add motion to an object. I've tried getting this button to move, but to no avail. The stage launches and I see the button, but it remains motionless.

package TestClasses;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ButtonMove extends Application
{

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);

    }


}

The above class, ButtonMove.java, just loads the fxml file, Main.fxml, and launches the stage. Below is the Main.fxml controller file, which for some reason keeps telling me that javafx.fxml.FXML is never used.

package TestClasses;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.animation.TranslateTransition;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.util.Duration;

public class MainController implements Initializable
{

    private Button button;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        TranslateTransition transition = new TranslateTransition();
        transition.setDuration(Duration.seconds(4));
        transition.setNode(button);
        transition.setToY(-200);
        transition.play();
    }

}

And this is just the fxml file generated by SceneBuilder.

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button id="button" layoutX="274.0" layoutY="310.0" mnemonicParsing="false" text="CLICK" />
         </children>
      </AnchorPane>
   </children>
</StackPane>

2 Answers 2

1

which for some reason keeps telling me that javafx.fxml.FXML is never used.

It's because you don't use the @FXML annotation anywhere in the code.

To connect the fxml to the controller, you need to

  1. Make the field that should be injected visible to the FXMLLoader by annotating it:

    @FXML
    private Button button;
    
  2. Specify the controller class to be used with the fxml by adding the fx:controller attribute to the root element:

    <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TestClasses.MainController">
    

    or by specifying a controller instance before calling FXMLLoader.load

    FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
    loader.setController(new MainController());
    Parent root = loader.load();
    
  3. Specify the fx:id attribute for objects you want to inject to the controller:

    <Button fx:id="button" layoutX="274.0" layoutY="310.0" mnemonicParsing="false" text="CLICK" />
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for this. I got it working. I'm very new to javaFX and I'm still in the process of learning the syntax. Your explanations were clear, concise, and well organized.
0

add an FXML tag here:

 @FXML
 private Button button;

otherwise the button is not initialized.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.