JavaFX - 传递值

时间:2016-11-29 21:35:35

标签: java javafx

我知道这个问题很常见,但我的选项已经用完了。试过每一个解决方案,没有什么对我有用。

我尝试做的只是将一些值从1个JFX窗口传递到另一个窗口。

这是login

中的loginController方法
public void login(ActionEvent event){
    try{
        if(loginModel.isLogin(txtUsername.getText(), txtPassword.getText())){
            try {
                ((Node)event.getSource()).getScene().getWindow().hide();
                Stage primaryStage = new Stage();
                FXMLLoader loader = new FXMLLoader();
                mainViewController sendHello = loader.<mainViewController>getController();
                sendHello.getUser(txtUsername.getText());
                Pane root = loader.load(getClass().getResource("mainView.fxml").openStream());
                primaryStage.setTitle("Kcalculator");
                primaryStage.setScene(new Scene(root));
                primaryStage.setResizable(false);
                primaryStage.show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else{
            Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
            alert1.setTitle("Login failed");
            alert1.setHeaderText(null);
            alert1.setContentText("The username or password you have entered is invalid.");
            alert1.showAndWait();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

这是我的mainViewController班级

public class mainViewController implements Initializable{

    @FXML
    private Label userLbl = new Label();

    public void getUser(String user){
        userLbl.setText("Welcome " + user);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }
}

在尝试了几个解决方案之后,在&#34;成功&#34;之后我仍然得到NullPointerException。登录。

2 个答案:

答案 0 :(得分:0)

您需要在loader.getController之前加载fxml才能返回null以外的任何内容:

FXMLLoader loader = new FXMLLoader();

// load fxml
Pane root = loader.load(getClass().getResource("mainView.fxml").openStream());

// get controller created using fx:controller attribute in the fxml
mainViewController sendHello = loader.<mainViewController>getController();

// use controller
sendHello.getUser(txtUsername.getText());

请注意,fx:controller属性需要为此分配正确的值才能正常工作。

答案 1 :(得分:0)

我最近不得不为应用程序开发登录。此应用程序打开用户帐户类型的正确视图。它使用sqlite来存储和检索有关用户的信息,以及存储和检索加密密码。您可能会觉得这很有帮助。

  

控制器

import java.io.IOException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class FXMLDocumentController implements Initializable {

    @FXML private TextField tfUsername;
    @FXML private PasswordField pfPassword;
    @FXML private Button btnSignIn;

    @FXML
    void handlebtnSignIn(ActionEvent event){

        int userID = 0;
        String username = "", accountType = "";
        byte[] encryptedPassword = null, salt = null;
        if(tfUsername.getText().length() > 0 && pfPassword.getText().length() > 0)
        {
           try(Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); 
                PreparedStatement  stmt = createPreparedStatement(conn, tfUsername.getText());
                ResultSet rs = stmt.executeQuery())
            {            
                while(rs.next())
                {
                    userID = rs.getInt("user_id");
                    username = rs.getString("username");
                    encryptedPassword = rs.getBytes("password");
                    salt = rs.getBytes("salt");
                    accountType = rs.getString("account_type");              
                }

                if(tfUsername.getText().equals(username) && authenticate(pfPassword.getText(), encryptedPassword, salt))
                {
                    Stage stage = new Stage();

                    stage.setWidth(1080);
                    stage.setHeight(720);
                    Parent fxmlLoader = null;

                    switch(accountType)
                    {
                        case "administrator":  
                            stage.setTitle("Tech Services Console");
                            System.out.println("I just signed in as an administrator");
                            fxmlLoader = FXMLLoader.load(getClass().getResource("AdminView.fxml"));
                            break;
                        case "collection":
                            stage.setTitle("Collection Console");
                            System.out.println("I just signed in as a collection member");
                            fxmlLoader = FXMLLoader.load(getClass().getResource("CollectionDevView.fxml"));
                            break;
                        case "selector":
                            stage.setTitle("Selector Console");
                            System.out.println("I just signed in as a selector member");
                            fxmlLoader = FXMLLoader.load(getClass().getResource("Selector.fxml"));
                            break;
                    }


                    stage.setScene(new Scene(fxmlLoader));

                    stage.show();
                    ((Node)(event.getSource())).getScene().getWindow().hide();
                    tfUsername.setText("");
                    pfPassword.setText("");
                }  
                else
                {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setHeaderText(null);
                    alert.setContentText("Incorrect username or password!");
                    alert.showAndWait();
                }
            } 
            catch (SQLException ex) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText(null);
                alert.setContentText("Incorrect username or password!");
                alert.showAndWait();
                System.out.println(ex.toString());
            } catch (NoSuchAlgorithmException | InvalidKeySpecException | IOException ex) {
                Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }        
    }

    @FXML
    void handleEnterButtonPress(KeyEvent event)
    {
        if(event.getCode().equals(KeyCode.ENTER))
        {
            btnSignIn.fire();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }   

    private PreparedStatement createPreparedStatement(Connection con, String username) throws SQLException {
        String sql = "SELECT * FROM User WHERE username = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, username);
        return ps;
    }

    boolean authenticate(String attemptedPassword, byte[] encryptedPassword, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException
    {
        byte[] encryptedAttmeptedPassword = getEncryptedPassword(attemptedPassword, salt);
        return Arrays.equals(encryptedPassword, encryptedAttmeptedPassword);
    }

    public byte[] getEncryptedPassword(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException
    {
        String algorithm = "PBKDF2WithHmacSHA1";
        int derivedKeyLength = 160;
        int iterations = 2000;
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);
        SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);

        return f.generateSecret(spec).getEncoded();
    }

    public byte[] generateSalt() throws NoSuchAlgorithmException 
    {    
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] salt = new byte[8];
        random.nextBytes(salt);

        return salt;
    }
}
  

FXML

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

<?import javafx.geometry.Insets?>
<?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.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane id="apMain" onKeyReleased="#handleEnterButtonPress" prefHeight="350.0" prefWidth="425.0" stylesheets="@../css/log_in_stylesheet.css" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="planningchart3.FXMLDocumentController">
   <children>
      <GridPane alignment="CENTER" hgap="10.0" layoutX="45.0" layoutY="69.0" prefHeight="284.0" prefWidth="448.0" vgap="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.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 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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <padding>
            <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
         </padding>
         <children>
            <TextField fx:id="tfUsername" promptText="username" text="sedj601" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="3" />
            <PasswordField fx:id="pfPassword" prefHeight="25.0" prefWidth="107.0" promptText="password" text="hello" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="4" />
            <Button fx:id="btnSignIn" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#handlebtnSignIn" prefHeight="30.0" prefWidth="92.0" text="Sign in" wrapText="true" GridPane.columnIndex="3" GridPane.rowIndex="5" />
            <Text id="welcomeText" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome" GridPane.columnIndex="1" GridPane.rowIndex="2">
               <font>
                  <Font name="Tahoma" size="20.0" />
               </font>
            </Text>
            <Label styleClass="outline" text="User name: " textFill="#03244d" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="3">
               <font>
                  <Font name="Cambria Bold" size="14.0" />
               </font></Label>
            <Label styleClass="outline" text="Password: " textFill="#03244d" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="4">
               <font>
                  <Font name="Cambria Bold" size="14.0" />
               </font></Label>
         </children>
      </GridPane>
   </children>
</AnchorPane>

我将用户名设置为sedj601,将密码设置为hello。你可能想要摆脱这一部分。

  

这是有用的here

     

这部分可能会对您的问题更有帮助。

try
{
    if(tfUsername.getText().equals(username) && authenticate(pfPassword.getText(), encryptedPassword, salt))
    {
        Stage stage = new Stage();
        stage.setWidth(1080);
        stage.setHeight(720);
        Parent fxmlLoader = null;

        switch(accountType)
        {
            case "administrator":  
                stage.setTitle("Tech Services Console");
                System.out.println("I just signed in as an administrator");
                fxmlLoader = FXMLLoader.load(getClass().getResource("AdminView.fxml"));
                break;
            case "collection":
                stage.setTitle("Collection Console");
                System.out.println("I just signed in as a collection member");
                fxmlLoader = FXMLLoader.load(getClass().getResource("CollectionDevView.fxml"));
                break;
            case "selector":
                stage.setTitle("Selector Console");
                System.out.println("I just signed in as a selector member");
                fxmlLoader = FXMLLoader.load(getClass().getResource("Selector.fxml"));
                break;
        }

        stage.setScene(new Scene(fxmlLoader));
        stage.show();
        ((Node)(event.getSource())).getScene().getWindow().hide();
        tfUsername.setText("");
        pfPassword.setText("");
    }  
    else
    {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText(null);
        alert.setContentText("Incorrect username or password!");
        alert.showAndWait();
    }
} 
catch (SQLException ex) {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setHeaderText(null);
    alert.setContentText("Incorrect username or password!");
    alert.showAndWait();
    System.out.println(ex.toString());
} 
catch (NoSuchAlgorithmException | InvalidKeySpecException | IOException ex)          
{
    Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}