0

Acabo de empezar a utilizar JavaFx y aun no estoy muy familiarizado con el. Estoy desarollando una aplicacion, la cual cuenta con un login.

El problema que tengo es que no se como hacerle para que cuando presione el boton entrar de la ventana login este me dirija a la ventana de inicio de la aplicacion y a su vez cierre la ventana del login, que este no quede detras de la ventana de inicio.

Este es el codigo que tengo de las dos ventanas en las que me quiero mover:

package com.its.tapicentro;

import java.io.IOException;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {    

    @Override
    public void start(Stage stage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("/interfaces/Login.fxml"));

        Scene scene = new Scene(root);
        scene.getStylesheets().add("/styles/Styles.css");

        stage.setTitle("Tapicentro");
        stage.setScene(scene);
        stage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);

    }

}

Login.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="511.0" prefWidth="413.0" style="-fx-background-color: #ffffff;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.LoginController">
   <children>
      <JFXTextField fx:id="usuario" labelFloat="true" layoutX="115.0" layoutY="142.0" prefHeight="25.0" prefWidth="200.0" promptText="Usuario" />
      <JFXPasswordField fx:id="password" labelFloat="true" layoutX="115.0" layoutY="230.0" prefHeight="25.0" prefWidth="200.0" promptText="Password" />
      <MaterialDesignIconView glyphName="ACCOUNT" layoutX="58.0" layoutY="168.0" size="35" />
      <MaterialDesignIconView glyphName="ACCOUNT_KEY" layoutX="58.0" layoutY="255.0" size="35" />
      <JFXButton fx:id="bentrar" layoutX="161.0" layoutY="323.0" onAction="#entrar" prefHeight="41.0" prefWidth="90.0" ripplerFill="#4059a9" style="-fx-background-color: #e7e7e7;" text="Entrar">
         <font>
            <Font name="System Bold Italic" size="12.0" />
         </font></JFXButton>
      <Pane prefHeight="94.0" prefWidth="413.0" style="-fx-background-color: #2D75E8;">
         <children>
            <FontAwesomeIconView fill="WHITE" glyphName="LOCK" layoutX="184.0" layoutY="71.0" size="70" />
         </children>
      </Pane>
   </children>
</AnchorPane>

LoginController.java:

package controllers;

import com.its.tapicentro.MainApp;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class LoginController {

    public LoginController() {

    }

    @FXML
    private JFXTextField usuario;

    @FXML
    private JFXPasswordField password;

    @FXML
    private JFXButton bentrar;

    @FXML
    public void entrar() throws IOException {

    }

}

Inicio.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="585.0" prefWidth="761.0" style="-fx-background-color: #ffffff;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.InicioController">
   <children>
      <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="133.0" prefWidth="761.0" style="-fx-background-color: #2D75E8;">
         <children>
            <Label layoutX="156.0" layoutY="32.0" text="Tapicentro" textFill="WHITE">
               <font>
                  <Font size="48.0" />
               </font>
            </Label>
            <FontAwesomeIconView fill="WHITE" glyphName="HOUZZ" layoutX="90.0" layoutY="98.0" size="90" />
         </children></Pane>
      <Pane layoutX="66.0" layoutY="162.0" prefHeight="402.0" prefWidth="650.0" AnchorPane.bottomAnchor="21.0" AnchorPane.leftAnchor="66.0" AnchorPane.rightAnchor="45.0" AnchorPane.topAnchor="162.0">
         <children>
            <GridPane alignment="CENTER" hgap="10.0" prefHeight="370.0" prefWidth="660.0" vgap="10.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <JFXButton fx:id="gestU" contentDisplay="TOP" onAction="#gestionarUsuarios" prefHeight="307.0" prefWidth="220.0" style="-fx-background-color: #ffffff;" styleClass="jfxbutton" stylesheets="@../styles/Styles.css" text="Gestionar Usuarios" GridPane.columnIndex="2">
                     <graphic>
                        <FontAwesomeIconView fill="#2d75e8" glyphName="GROUP" size="50" />
                     </graphic>
                     <font>
                        <Font size="14.0" />
                     </font>
                  </JFXButton>
                  <JFXButton contentDisplay="TOP" prefHeight="307.0" prefWidth="220.0" style="-fx-background-color: #ffffff;" styleClass="jfxbutton" stylesheets="@../styles/Styles.css" text="Gestionar Proveedores" GridPane.columnIndex="1">
                     <graphic>
                        <FontAwesomeIconView fill="#2d75e8" glyphName="BOOK" size="50" />
                     </graphic>
                     <font>
                        <Font size="14.0" />
                     </font>
                  </JFXButton>
                  <JFXButton contentDisplay="TOP" prefHeight="307.0" prefWidth="220.0" style="-fx-background-color: ffffff;" styleClass="jfxbutton" stylesheets="@../styles/Styles.css" text="Gestionar Mercancia">
                     <graphic>
                        <FontAwesomeIconView fill="#2d75e8" glyphName="PENCIL" size="50" />
                     </graphic>
                     <font>
                        <Font size="14.0" />
                     </font>
                  </JFXButton>
                  <JFXButton contentDisplay="TOP" prefHeight="307.0" prefWidth="220.0" style="-fx-background-color: #ffffff;" styleClass="jfxbutton" stylesheets="@../styles/Styles.css" text="Agregar o quitar productos" GridPane.columnIndex="1" GridPane.rowIndex="1">
                     <graphic>
                        <FontAwesomeIconView fill="#2d75e8" glyphName="SHOPPING_BASKET" size="50" />
                     </graphic>
                     <font>
                        <Font size="14.0" />
                     </font>
                  </JFXButton>
               </children>
            </GridPane>
         </children>
      </Pane>
   </children>
</AnchorPane>

InicioController:

package controllers;

import com.jfoenix.controls.JFXButton;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class InicioController {

    @FXML
    private JFXButton gestU;

    @FXML
    void gestionarUsuarios(ActionEvent event) throws IOException {
        Stage stage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/interfaces/formularioUsuario.fxml"));

        Scene scene = new Scene(root);
        scene.getStylesheets().add("/styles/Styles.css");

        stage.setTitle("Tapicentro");
        stage.setScene(scene);
        stage.show();
    }

}

1 respuesta 1

0

Mira, para realizar esa funcionalidad se utiliza un metodo llamado MouseClicked. Desde la aplicación de JavaFX del lado derecho, y en la pestaña Code, lo vas a encontrar. Le pones un nombre a dicho metodo como por ejemplo: btnIngresar_MouseClicked. Después de esto guardas el archivo y te dirigis al FXML de la ventana Login y haces click derecho y presionas Make Controles. Hecho esto, Java te crea automáticamente la estructura de los métodos a los que les hallas puesto un nombre en el Controller de tu ventana.

Dentro de ese método Mouse Clicked (que se lanzara apenas hagas click en el) se escribe el código que abrirá la ventana que desees. El mismo es:

Ventana de Login:

@FXML
private void btnIngresar_MouseClicked(MouseEvent event) throws IOException {
    //Para crear una ventana necesitas un nuevo Stage (Escenario)
    Stage stage = new Stage();
    //Cargas el FXML que queres que abra en un Parent
    Parent root = FXMLLoader.load(getClass().getResource("ventanaDos.fxml"));
    //Se declara una Scene y se le asigna el FXML (Una Scene es la ventana)
    Scene scene = new Scene(root);
    //Establecemos la scena en el Stage
    stage.setScene(scene);
    //titulo para la ventana
    stage.setTitle("TITULO");
    //El formulario se maximiza y se muestra
    stage.setMaximized(true);
    stage.show();

    //Cerramos la ventana anterior de Login. La obtenemos a partir de un control (Button)
    Stage old = (Stage) btnIngresar.getScene().getWindow();
    old.close();
}
1
  • 1
    Muchas gracias Gonzalo! me ha sido muy util el 13 oct. 2018 a las 0:43

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.