Add a new scene after the login. Now I do not have any action that executes from a button in the new scene. This is the main class.
package controllers;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import com.sun.glass.ui.Window.Level;
import controllers.index.IndexController;
import controllers.login.LoginController;
import javafx.application.Application;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
public class Main extends Application {
private Stage stage;
private BorderPane rootLayout;
javafx.geometry.Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
@Override
public void start(Stage stage) {
this.stage = stage;
stage.initStyle(StageStyle.UNDECORATED);//SE BLOQUEAN LAS OPCIONES DE VENTANAS minimizar,maximizar,cerrar.
stage.setTitle("Sistema Idiomas RUGE");
initRootLayout();//se inicializa el layout principal rootLayout
showLoginView();//se muestra la vista del login
}
public static void main(String[] args) {
launch(args);
}
/**
* Funcion para inicializar el archivo RootLayout el cual sera el layout padre de todos los escenarios.
*/
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("rootLayout/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Funcion para mostrar la vista login.
*/
public void showLoginView() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("login/login.fxml"));
AnchorPane loginView = (AnchorPane) loader.load();
rootLayout.setCenter(loginView);
LoginController controller = loader.getController();
controller.setMainApp(this);
setViewCenter();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Funcion para ingresar al sistema.
* Esta funcion se usa en LoginController en caso de que los datos del usuario hayan sido correctos.
*/
public void gotoProfile() {
try {
replaceSceneContent("index/index.fxml");
} catch (Exception ex) {
System.out.println("error al iniciar"+ex);
}
}
/**
* Funcion para remplazar un escenario por otro.
* Se enviara de parametro la ruta del archivo fxml que se mostrara.
*/
private AnchorPane replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
AnchorPane page = (AnchorPane) loader.load();
Scene scene = stage.getScene();
scene = new Scene(page,1000,600);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
setViewCenter();
IndexController controller = loader.getController();
controller.setMainApp(this);
return page;
}
/**
* Funcion para setear la ventana en el centro de la pantalla
*/
private void setViewCenter(){
stage.setX((screenBounds.getWidth() - stage.getWidth()) / 2);
stage.setY((screenBounds.getHeight() - stage.getHeight()) / 2);
}
}
Which when entering from the login runs the function gotoProfile () which will replace the scene with the new one where the index fxml is.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import com.jfoenix.controls.*?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchorPane" maxHeight="600.0" maxWidth="10000.0" minHeight="600.0" minWidth="1000.0" prefHeight="600.0" prefWidth="1000.0" scaleZ="0.0" style="-fx-background-color: #FFF;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.index.IndexController">
<rotationAxis>
<Point3D />
</rotationAxis>
<effect>
<DropShadow />
</effect>
<children>
<JFXHamburger fx:id="hamburger" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="12.0" />
<JFXDrawer fx:id="drawer" prefHeight="598.0" prefWidth="200.0" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="1.0" />
<Button mnemonicParsing="false" onAction="#prueba" text="Button" AnchorPane.leftAnchor="300.0" AnchorPane.topAnchor="300.0" />
</children>
</AnchorPane>
It's here when I try to execute the test method from the button that is in index.fxml and it does not work for me. Although probe put the test function in initialize of indexController and if it works.
package controllers.index;
import java.net.URL;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXDrawer;
import com.jfoenix.controls.JFXHamburger;
import com.jfoenix.transitions.hamburger.HamburgerBackArrowBasicTransition;
import controllers.Main;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class IndexController implements Initializable{
private Main main;
@FXML
private AnchorPane anchorPane;
@FXML
private Button button;
public void setMainApp(Main main) {
this.main = main;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
@FXML
public void prueba(){
System.out.println("Sirve");
}
}