I'm writing this post because I came across one problem - switching scenes in JavaFx. I've been learning how to do it for a while but I still don't understand it. Can you help me with this issue?
In the controller class I want to switch scenes after typing the correct login and password - LoginButtonClicked
method, tu.fxml
is my new scene constructed in Scene Builder.
I will be also very grateful if you could tell me if I'm making any mistakes with this code =D. For example a badly constructed controller etc. Thanks in advance!
Main class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Endomondo By Michael");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller class:
package sample;
public class LoginController {
@FXML
public TextField login,password;
Stage MainStage;
Scene scene;
public void LoginButtonClicked(ActionEvent event) throws IOException {
if(login.getText().equals("yes")&&password.getText().equals("yes"))
{
MainStage = FXMLLoader.load(getClass().getResource("tu.fxml"));
}
else
{
System.out.print("no");
}
}
}