JavaFX switch scenes

75 Views
Published
JavaFX switch scenes with using SceneBuilder tutorial example explained

#javafx #switch #scenes

//--------------------------------Main.java--------------------------------------
package application;

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

public class Main extends Application {

@Override
public void start(Stage stage) {
try {

Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

} catch(Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
launch(args);
}
}
//---------------------------------SceneController.java---------------------------------------
package application;

import java.io.IOException;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class SceneController {

private Stage stage;
private Scene scene;
private Parent root;

public void switchToScene1(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}

public void switchToScene2(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
//------------------------------------------------------------------------------------------------

Bro Code merch store
Category
Bro Code
Tags
javafx switch scenes, switch scenes javafx, javafx switch scenes fxml
Be the first to comment