Search code examples
javaif-statementjavafxtextfieldgridpane

Username and password are always incorrect with custom javafx login dialog


Really don't know what do to do here? Any help on where my next move could be so that I can get this up and running would be greatly appreciated! I am looking to roll this log in application into my next class project by using a combo box with this login screen. IF anyone has any suggestions on what im doing wrong or if my code could be written better it would be fantastic.

import javafx.application.Application;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.util.Pair;
import javafx.geometry.Insets;
import java.util.Optional;


public class LoginPassword extends Application {
    @Override
    public void start(Stage primaryStage) {

        Dialog<Pair<String, String>> dialog = new Dialog<>();
        dialog.setTitle("Login");
        dialog.setHeaderText("Enter your username and password");



        ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);


        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        TextField username = new TextField();
        username.setPromptText("Username");
        PasswordField password = new PasswordField();
        password.setPromptText("Password");

        grid.add(new Label("Username:"), 0, 0);
        grid.add(username, 1, 0);
        grid.add(new Label("Password:"), 0, 1);
        grid.add(password, 1, 1);


        dialog.getDialogPane().setContent(grid);

        Optional<Pair<String, String>> input = dialog.showAndWait();
        System.out.println(input.get());


        if (username.equals("dom123")) {

            if (password.equals("d123")) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("You have been logged in");
                alert.setContentText("Welcome" + username);

                return;

            } else {
                int passwordattempts = 1;
                while (passwordattempts < 3) {
                    dialog.showAndWait();
                    passwordattempts++;
                }
            }
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText("Contact Admin");
            alert.showAndWait();

        } else {
            int usernameattempts = 1;
            while (usernameattempts < 3) {
                dialog.showAndWait();
                usernameattempts++;
            }
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText("Invalid Username");
            alert.showAndWait();
        }
    }


    public static void main (String [] args) {
        launch(args);
    }
  }





**strong text**

Solution

  • Change

    if (username.equals("dom123")) {
    
        if (password.equals("d123")) {
    

    to

    if(String.valueOf(username.getText()).equals("dom123")){
        if(String.valueOf(password.getText()).equals("d123")){
    

    Because your username and password variables are just references to the username textfield and password textfield, not the actual value stored in them, so you need to get the text that is stored in them using getText() method.