I am creating a login, it consists of entering a username and password, then when I click on validate, the Login closes and a new window opens (where I will include functions that I have not yet implemented)... but this new window has a logout button, how can I make clicking that button return me to Login?
I've already been looking for solutions, but everything seems too confusing, I've seen that I can load everything in the same Stage, just changing the Scene, but I see it as complex, I don't understand how it does it..., this is my code.
Main class
package mainPack;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class mainC extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/GUIs/Login.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
LoginController
package GUIs;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class LoginController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML PasswordField txtPass;
@FXML TextField txtUser;
@FXML Label lblError;
@FXML Label lblStatus;
@FXML
public void btnHandle(ActionEvent e) throws Exception{
if(txtUser.getText().equals("Sistemas") && txtPass.getText().equals("2019")){
lblError.setOpacity(0);
lblStatus.setText("Conectado.");
Stage stage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/GUIs/Principal.fxml"));
Scene escena = new Scene(root);
stage.setScene(escena);
stage.show();
//en esta linea, esconde el Stage del Login y carga el nuevo stage
( (Node) (e.getSource() ) ).getScene().getWindow().hide();
}else{
lblStatus.setText("Desconectado.");
lblError.setOpacity(1);
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
WindowPrincipalController (after login)
package GUIs;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
/**
* FXML Controller class
*
* @author VAPESIN
*/
public class PrincipalController implements Initializable {
/**
* Initializes the controller class.
*/
@FXML
public void cerrarSesHandle(ActionEvent e){
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="493.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUIs.LoginController">
<stylesheets>
<URL value="@login.css" />
</stylesheets>
<children>
<Label layoutX="49.0" layoutY="71.0" prefHeight="46.0" prefWidth="52.0" text="Usuario" />
<Label layoutX="49.0" layoutY="148.0" prefHeight="46.0" prefWidth="66.0" text="Contraseña" />
<Button layoutX="145.0" layoutY="252.0" mnemonicParsing="false" onAction="#btnHandle" text="Validar" />
<TextField fx:id="txtUser" layoutX="145.0" layoutY="82.0" />
<PasswordField fx:id="txtPass" layoutX="145.0" layoutY="159.0" />
<Label fx:id="lblError" layoutX="74.0" layoutY="301.0" opacity="0.0" prefHeight="46.0" prefWidth="345.0" text="Usuario o Contraseña incorrecta." textFill="RED">
<font>
<Font size="17.0" />
</font>
</Label>
<Label fx:id="lblStatus" layoutX="101.0" layoutY="25.0" prefHeight="46.0" prefWidth="330.0" text="Desconectado" />
</children>
</AnchorPane>
MainWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/10.0.1" fx:controller="GUIs.PrincipalController">
<stylesheets>
<URL value="@principal.css" />
</stylesheets>
<children>
<Label layoutX="167.0" layoutY="77.0" prefHeight="103.0" prefWidth="271.0" text="Bienvenido">
<font>
<Font size="53.0" />
</font>
</Label>
<Button layoutX="438.0" layoutY="345.0" mnemonicParsing="false" onAction="#cerrarSesHandle" prefHeight="25.0" prefWidth="142.0" text="Cerrar sesión" />
</children>
</AnchorPane>
When clicking on close, I would like it to return me to the Login.
hello look you can check my code on github on a system of my school which implements a login with its logout and return that of logout and that I remember that I had problems because I was amazed with the previous data if you want check my https code ://github.com/WihoverEsal/ProyectoAsesorias is in java FX, look at my code that I have in this part on github
I have this code that I made in my button to close session and return you to the login that here is as you can see first I create its setOnAction from there I add some styles to the letters and that this part is for the button to close session and when I want it to it returns to the window that I want in the method that is in the last part we put our constructor of the window where we want it to return and well that's all I hope and it works for you