0

This is the login logic of the login pane, when user input their username and password, if both are 1, then they will enter the main pane and the label at the right top in the main pane will show their username.

login_btn.setOnAction(event -> {
        String input_name = username.getText();
        String input_pw = password.getText();
        String true_name = "1";
        String true_pw = "1";

        System.out.println("username: " + input_name);
        System.out.println("password: "+ input_pw);

        log_condition = input_name.equals(true_name) && input_pw.equals(true_pw);

        System.out.println("login condition: " + log_condition);

        if (log_condition) {
            try {
                user = input_name;
                System.out.println("user: " + user);
                Main.stage.close();
                main_pane();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

This code is how to set the username in the label but it does not work. the Label id will show nothing after the user enters this pane. The loginPanel_ui is the code above.

public class mainPane_ui implements Initializable {
    @FXML
    public static Button newer;
    @FXML
    public static Button recommend;
    @FXML
    public static Label id = new Label();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        id.setText(loginPanel_ui.user);
        id.setTextFill(Color.WHITE);
    }
  • Two problems: (1) `static` fields do not work with `@FXML`, and (2) you're setting the text on a `Label` instance that is not added to the displayed scene graph (never manually initialize `@FXML`-annotated fields). – Slaw Dec 02 '21 at 07:39
  • @Slaw Thank you so much, I delete the static field and it works. – SDesolator Dec 02 '21 at 07:45
  • Glad to help. And note that I'm slightly mistaken regarding my second point. If a `Label` is injected for `id` then it will replace the `new Label()` instance. And as you set properties in the `initialize` method, you are manipulating the injected instance. However, you should still remove `= new Label()`. – Slaw Dec 02 '21 at 07:51

0 Answers0