I would like to write simple application where I'm going to make a few views:
- Login
- Register
- Main app
I think that my method is not the best way to do this. Can someone give me a tip?
Main.java
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Controller implements Initializable{
@FXML
Button openRegister;
@FXML
Button openLogin;
private Stage stage;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
private void openRegister() throws IOException{
stage = (Stage) openRegister.getScene().getWindow();
BorderPane root;
root = (BorderPane) FXMLLoader.load(getClass().getResource("Register.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setScene(scene);
System.out.println("Register.fxml opened");
}
@FXML
private void openLogin() throws IOException{
stage = (Stage) openLogin.getScene().getWindow();
BorderPane root;
root = (BorderPane) FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setScene(scene);
System.out.println("Login.fxml opened");
}
}
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<center>
<Button fx:id="openRegister" mnemonicParsing="false" onMouseClicked="#openRegister" text="Button" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Register.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<center>
<Button fx:id="openLogin" mnemonicParsing="false" onMouseClicked="#openLogin" text="Button" BorderPane.alignment="CENTER" />
</center>
</BorderPane>