Reputation: 186
I'm trying to switch a scene using a button which created by Scene Builder.
That is my Controller class
public class Controller implements Initializable {
@FXML Button LoginButton;
@FXML private void handleButtonAction(ActionEvent event) throws IOException {
Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
Scene tableViewScene = new Scene(tableViewParent);
Stage window = (Stage) ((Node)event.getSource()).getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
And there is FXML
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
<MenuBar VBox.vgrow="NEVER" />
<AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
<children>
<AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
<children>
<TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
<effect>
<InnerShadow />
</effect></TextField>
<PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
<effect>
<InnerShadow blurType="TWO_PASS_BOX" />
</effect></PasswordField>
<Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
<AnchorPane />
</children>
</VBox>
For some reason IntelliJ shows error Cannot resolve 'handleButtonAction'
onAction="#handleButtonAction"
Upvotes: 1
Views: 3157
Reputation: 690
You can retrieve the Scene (and the Stage) with any FXML element belonging to it. You only have to do anyElement.getScene().getWindow()
public class Controller {
@FXML
Button loginButton;
@FXML
private void handleButtonAction() {
Parent tableViewParent = FXMLLoader.load(getClass().getResource("../FXML/MainMenu.fxml"));
Scene tableViewScene = new Scene(tableViewParent);
// Instead of retrieving the stage by the event's source, you can do it by one of your FXML component.
Stage window = (Stage) loginButton.getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
@FXML
public void initialize() {
//initialize the field you want here.
}
}
And the FXML :
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.Controller">
<!-- Fabian was right, you forgot this children. -->
<children>
<MenuBar VBox.vgrow="NEVER" />
<AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #ccf2ff #ccf2ff;" VBox.vgrow="ALWAYS">
<children>
<AnchorPane id="login2" layoutX="320.0" prefHeight="371.0" prefWidth="316.0">
<children>
<TextField alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
<effect>
<InnerShadow />
</effect>
</TextField>
<PasswordField alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
<effect>
<InnerShadow blurType="TWO_PASS_BOX" />
</effect>
</PasswordField>
<Button fx:id="loginButton" layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
<AnchorPane />
</children>
</VBox>
Some explanation/remarks on your code.
initialize
method with FXML annotation is now sufficient (look at the note in interface definition).
fx:id
in your FXML. Did you forget it ? I added it in Button
element in the FXML. (I do not know if it was effectively the loginButton...)
Upvotes: 2
Reputation: 341
Write below code in your controller
@FXML
protected void handleButtonAction() {
System.out.println("In handleButtonAction");
// your logic
}
and FXML file
<Button layoutX="129.0" layoutY="220.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
I hope it will work... I used scenebuilder for FXML, if not works send me error screenshot
Upvotes: 0