JavaFX Password field not working


Question

In scene builder I have a password field with the fx:id passwordBox and in the corresponding controller class I have

@FXML private static PasswordField passwordBox = new PasswordField();

I ALSO TRIED

@FXML private static PasswordField passwordBox;

When I run the program the letters in the password field are plain text. The same thing happens when I preview the window in scene builder. The password field is a PasswordField so I didn't mistake it for a TextField.

What can I do?

Edit: FXML file

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

<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>

<AnchorPane fx:id="mainAnchor" opacity="1.0" prefHeight="200.0" prefWidth="408.0000999999975" styleClass="back" xmlns:fx="http://javafx.com/fxml" fx:controller="application.MainController">
  <!-- TODO Add Nodes -->
  <children>
    <Label fx:id="serverIPLbl" layoutX="14.0" layoutY="28.0" prefWidth="175.0" text="Server IP">
      <font>
        <Font name="Segoe UI" size="12.0" fx:id="x1" />
      </font>
    </Label>
    <TextField fx:id="serverIPBox" layoutX="14.0" layoutY="50.0" prefWidth="175.0" />
    <Label fx:id="portLbl" font="$x1" layoutX="215.0" layoutY="28.0" prefWidth="175.0" text="Port" />
    <TextField fx:id="portBox" layoutX="215.0" layoutY="50.0" prefWidth="175.0" />
    <Label fx:id="passwordLbl" font="$x1" layoutX="14.0" layoutY="81.0" prefWidth="175.0" text="Server Password" />
    <ImageView fx:id="settingsButton" fitHeight="23.0" fitWidth="23.0" layoutX="14.0" layoutY="137.0" onMouseClicked="#settingsClicked" pickOnBounds="true" preserveRatio="true">
      <image>
        <Image url="@../images/gear.png" />
      </image>
    </ImageView>
    <Button id="startServer" fx:id="connectButton" layoutX="14.0" layoutY="167.0" mnemonicParsing="false" onAction="#connectClicked" prefHeight="22.0" prefWidth="376.0" text="Connect" />
    <Label fx:id="usernameLbl" font="$x1" layoutX="215.0" layoutY="81.0" prefWidth="175.0" text="Username" />
    <TextField id="serverIPBox" fx:id="usernameBox" layoutX="215.0" layoutY="103.0" prefWidth="175.0" promptText="a-z A-Z 0-9 _ - chars allowed" />
    <ImageView id="favoriteButton" fx:id="favoritesButton" fitHeight="23.0" fitWidth="23.0" layoutX="55.0" layoutY="137.0" onMouseClicked="#favoritesClicked" pickOnBounds="true" preserveRatio="true">
      <image>
        <Image url="@../images/favorite.png" />
      </image>
    </ImageView>
    <PasswordField fx:id="passwordBox" layoutX="14.0" layoutY="103.0" prefWidth="175.0" promptText="Optional" />
  </children>
  <stylesheets>
    <URL value="@style.css" />
  </stylesheets>
</AnchorPane>
1
0
1/18/2014 6:01:20 AM

Issues with your code

@FXML is an injection annotation for controller instances, you shouldn't use it in conjunction with static members or members which you initialize using the new keyword.

Instead the definition in your controller should be:

@FXML private PasswordField passwordBox;

Additional considerations which may or may not apply in your case

  • Controller instances should always be created through an new FXMLLoader().load(...) call (not via a new keyword on the controller itself, unless you subsequently use loader.setController()).
  • The fxml file must specify an name for your password field which matches the name in your controller (in your case this name is passwordBox).

Example

GatewayApplication.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;

public class GatewayApplication extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("passport.fxml"));
        AnchorPane layout = loader.load();
        stage.setScene(new Scene(layout));
        stage.show();
    }
}

MainController.java

Generated by SceneBuilder View | Show Sample Skeleton.

package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;


public class MainController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button connectButton;

    @FXML
    private ImageView favoritesButton;

    @FXML
    private AnchorPane mainAnchor;

    @FXML
    private PasswordField passwordBox;

    @FXML
    private Label passwordLbl;

    @FXML
    private TextField portBox;

    @FXML
    private Label portLbl;

    @FXML
    private TextField serverIPBox;

    @FXML
    private Label serverIPLbl;

    @FXML
    private ImageView settingsButton;

    @FXML
    private TextField usernameBox;

    @FXML
    private Label usernameLbl;


    @FXML
    void connectClicked(ActionEvent event) {
        System.out.println("password = " + passwordBox.getText());
    }

    @FXML
    void favoritesClicked(MouseEvent event) {
    }

    @FXML
    void settingsClicked(MouseEvent event) {
    }

    @FXML
    void initialize() {
        assert connectButton != null : "fx:id=\"connectButton\" was not injected: check your FXML file 'passport.fxml'.";
        assert favoritesButton != null : "fx:id=\"favoritesButton\" was not injected: check your FXML file 'passport.fxml'.";
        assert mainAnchor != null : "fx:id=\"mainAnchor\" was not injected: check your FXML file 'passport.fxml'.";
        assert passwordBox != null : "fx:id=\"passwordBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert passwordLbl != null : "fx:id=\"passwordLbl\" was not injected: check your FXML file 'passport.fxml'.";
        assert portBox != null : "fx:id=\"portBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert portLbl != null : "fx:id=\"portLbl\" was not injected: check your FXML file 'passport.fxml'.";
        assert serverIPBox != null : "fx:id=\"serverIPBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert serverIPLbl != null : "fx:id=\"serverIPLbl\" was not injected: check your FXML file 'passport.fxml'.";
        assert settingsButton != null : "fx:id=\"settingsButton\" was not injected: check your FXML file 'passport.fxml'.";
        assert usernameBox != null : "fx:id=\"usernameBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert usernameLbl != null : "fx:id=\"usernameLbl\" was not injected: check your FXML file 'passport.fxml'.";
    }

}

style.css

.root {
    -fx-background-color: cornsilk;
}

passport.fxml

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

<?import javafx.scene.control.*?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import java.net.URL?>
<AnchorPane fx:id="mainAnchor" opacity="1.0" prefHeight="200.0" prefWidth="408.0000999999975" styleClass="back" xmlns:fx="http://javafx.com/fxml" fx:controller="application.MainController">
    <children>
        <Label fx:id="serverIPLbl" layoutX="14.0" layoutY="28.0" prefWidth="175.0" text="Server IP">
            <font>
                <Font name="Segoe UI" size="12.0" fx:id="x1" />
            </font>
        </Label>
        <TextField fx:id="serverIPBox" layoutX="14.0" layoutY="50.0" prefWidth="175.0" />
        <Label fx:id="portLbl" font="$x1" layoutX="215.0" layoutY="28.0" prefWidth="175.0" text="Port" />
        <TextField fx:id="portBox" layoutX="215.0" layoutY="50.0" prefWidth="175.0" />
        <Label fx:id="passwordLbl" font="$x1" layoutX="14.0" layoutY="81.0" prefWidth="175.0" text="Server Password" />
        <ImageView fx:id="settingsButton" fitHeight="23.0" fitWidth="23.0" layoutX="14.0" layoutY="137.0" onMouseClicked="#settingsClicked" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="http://icons.iconarchive.com/icons/hopstarter/soft-scraps/24/Gear-icon.png" />
            </image>
        </ImageView>
        <Button id="startServer" fx:id="connectButton" layoutX="14.0" layoutY="167.0" mnemonicParsing="false" onAction="#connectClicked" prefHeight="22.0" prefWidth="376.0" text="Connect" />
        <Label fx:id="usernameLbl" font="$x1" layoutX="215.0" layoutY="81.0" prefWidth="175.0" text="Username" />
        <TextField id="serverIPBox" fx:id="usernameBox" layoutX="215.0" layoutY="103.0" prefWidth="175.0" promptText="a-z A-Z 0-9 _ - chars allowed" />
        <ImageView id="favoriteButton" fx:id="favoritesButton" fitHeight="23.0" fitWidth="23.0" layoutX="55.0" layoutY="137.0" onMouseClicked="#favoritesClicked" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="http://icons.iconarchive.com/icons/hopstarter/soft-scraps/24/Button-Favorite-icon.png"/>
            </image>
        </ImageView>
        <PasswordField fx:id="passwordBox" layoutX="14.0" layoutY="103.0" prefWidth="175.0" promptText="Optional" />
    </children>
    <stylesheets>
        <URL value="@style.css" />
    </stylesheets>
</AnchorPane>

Sample program UI

sampleui

Sample program output

On entering the magic work "xyzzy" in the password field and pressing connect, the program extracts the password from the field and prints it out to the console:

password = xyzzy

Test system used was OS X 10.9 running Java 8b121.

In future questions like this, you may want to provide a minimal, complete, tested and readable example. Such a sample isn't needed for all questions but sure would have helped with this one.

1
5/23/2017 12:01:18 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon