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>
add an FXML tag here:
otherwise the button is not initialized.