EDIT: Thanks to jns for the answer, simple mistake. Have to you .equals() to check a string.
so I made a simple login GUI with javafx/scenebuilder and have an issue with reading the user input at the username field and password field. What I am trying to have happen is for the handleLogin method to try to account for three different actions, one being the correct credentials entered, user="test" and pass="test1". The next is if the fields are kept blank, a warning alert should appear. The last is for anything else entered to issue an error alert. Currently no matter what is in the fields, upon pressing login button, only the error alert shows. There are three files total called MainApp, LoginOverview, and LoginOverviewController. Here is a screenshot for what is currently shown
MainApp.java
package main.launch;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import main.launch.view.LoginOverviewController;
public class MainApp extends Application {
public Stage primaryStage;
/**
* Constructor
*/
public MainApp() {
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("TroubleTicketSystem");
showLoginOverview();
}
/**
* Shows the person overview inside the root layout.
*/
public void showLoginOverview() {
try {
// Load login overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/LoginOverview.fxml"));
AnchorPane loginOverview = (AnchorPane) loader.load();
Scene scene = new Scene(loginOverview);
primaryStage.setScene(scene);
primaryStage.show();
// Give the controller access to the main app.
LoginOverviewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
LoginOverviewController.java
package main.launch.view;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import main.launch.MainApp;
public class LoginOverviewController{
@FXML
private TextField usernameField;
@FXML
private PasswordField passwordField;
// Reference to the main application.
private MainApp mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public LoginOverviewController() {
}
/**
* Called when the user clicks the login button.
*
*/
@FXML
private void handleLogin(){
if (usernameField.getText() == "test" && passwordField.getText() == "test1")
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("Login");
alert.setHeaderText("Successfull Login");
alert.setContentText("You are now logged in, testuser!");
alert.showAndWait();
}
else if (usernameField.getText() == "" || passwordField.getText() == ""){
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No information");
alert.setHeaderText("Missing credentials");
alert.setContentText("Please enter in your username and password.");
alert.showAndWait();
}
else {
Alert alert = new Alert(AlertType.ERROR);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("Error");
alert.setHeaderText("Wrong information");
alert.setContentText("Please try again.");
alert.showAndWait();
}
}
/**
* Is called by the main application to give a reference back to itself.
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
}
LoginOverview.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?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 prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.launch.view.LoginOverviewController">
<children>
<Button layoutX="188.0" layoutY="301.0" mnemonicParsing="false" onAction="#handleLogin" prefHeight="50.0" prefWidth="125.0" text="Login" />
<Label layoutX="126.0" layoutY="153.0" prefHeight="32.0" prefWidth="70.0" text="Username" />
<Label layoutX="126.0" layoutY="191.0" prefHeight="32.0" prefWidth="62.0" text="Password" />
<TextField fx:id="usernameField" layoutX="197.0" layoutY="158.0" />
<Label layoutX="87.0" layoutY="77.0" text="Trouble Ticket System" textFill="#0d00ff">
<font>
<Font size="36.0" />
</font>
</Label>
<PasswordField fx:id="passwordField" layoutX="197.0" layoutY="196.0" />
</children>
</AnchorPane>
Any help is appreciated, thanks!