Я пытаюсь установить, что представленный контент изменяется в соответствии со значением выбранного RadioButton . Проблема в том, что он не реагирует на выбор кнопки .
Может быть, моя реализация неисправна.
Иногда это работает, когда я устанавливаю событие onAction , но это порождает некоторые другие проблемы, поэтому я хотел бы найти решение для этого.
Label title = new Label("USER REGISTRATION");
title.setId("title");
Label pickUserLabel = new Label("User type");
pickUserLabel.getStyleClass().add("labels");
RadioButton pickStudent = new RadioButton("Student");
RadioButton pickProfessor = new RadioButton("Professor");
/*
* Selection of user type -> adds additional fields if needed
*/
ToggleGroup userTypeGroup = new ToggleGroup();
pickStudent.setToggleGroup(userTypeGroup);
pickStudent.setSelected(true);
pickProfessor.setToggleGroup(userTypeGroup);
pickStudent.getStyleClass().add("radio");
pickProfessor.getStyleClass().add("radio");
/*
* text fields and labels below are used whenever additional ones are needed
*/
TextField textField1 = new TextField();
TextField textField2 = new TextField();
TextField textField3 = new TextField();
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
ChoiceBox<AcademicTitles> academicTitleChoice = new ChoiceBox<AcademicTitles>();
academicTitleChoice.getSelectionModel().selectedItemProperty().addListener((v, oldValue, newValue) -> System.out.println(newValue));
if(pickStudent.isSelected()) {
label1.setText("Email");
label1.getStyleClass().add("labels");
textField1.setPromptText("E-mail");
label2.setText("Phone Number");
label2.getStyleClass().add("labels");
textField2.setPromptText("Phone number");
label3.setText("Recordbook Number");
label3.getStyleClass().add("labels");
textField3.setPromptText("Recordbook number");
academicTitleChoice.hide();
};
if(pickProfessor.isSelected()) {
label1.setText("Email");
label1.getStyleClass().add("labels");
textField1.setPromptText("E-mail");
layout.getChildren().remove(label2);
layout.getChildren().remove(textField2);
layout.getChildren().remove(label3);
layout.getChildren().remove(textField3);
academicTitleChoice.getItems().addAll(AcademicTitles.PROFESSOR, AcademicTitles.ASSOCIATE_PROFESSOR,
AcademicTitles.ASSISTANT_PROFESSOR, AcademicTitles.RESEARCH_PROFESSOR,
AcademicTitles.TEACHING_ASSISTANT);
academicTitleChoice.setValue(AcademicTitles.PROFESSOR);
academicTitleChoice.setMaxWidth(150);
academicTitleChoice.setId("academicTitleChoice");
};
Separator separator = new Separator();
/*
* First Name
*/
Label firstNameLabel = new Label("Enter First Name");
firstNameLabel.getStyleClass().add("labels");
TextField firstName = new TextField();
firstName.setPromptText("First name");
firstName.setMaxWidth(200);
/*
* Last Name
*/
Label lastNameLabel = new Label("Enter Last Name");
lastNameLabel.getStyleClass().add("labels");
TextField lastName = new TextField();
lastName.setPromptText("Last name");
lastName.setMaxWidth(200);
/*
* Username
*/
Label usernameLabel = new Label("Enter Username");
usernameLabel.getStyleClass().add("labels");
Label usernameInfo = new Label("(length must be >= 4)");
usernameInfo.getStyleClass().add("info");
TextField username = new TextField();
username.setPromptText("Username");
username.setMaxWidth(200);
/*
* Password
*/
Label passwordLabel = new Label("Enter Password");
passwordLabel.getStyleClass().add("labels");
Label passwordInfo = new Label("(length must be >= 6)");
passwordInfo.getStyleClass().add("info");
PasswordField password = new PasswordField();
password.setPromptText("Password");
password.setMaxWidth(200);
Button registrationButton = new Button("Register User");
registrationButton.setOnAction(e -> register());
GridPane.setConstraints(title, 0, 0);
GridPane.setConstraints(pickUserLabel, 0, 1);
GridPane.setConstraints(pickStudent, 0, 2);
GridPane.setConstraints(pickProfessor, 0, 3);
GridPane.setConstraints(separator, 0, 5);
GridPane.setConstraints(firstNameLabel, 0, 6);
GridPane.setConstraints(firstName, 1, 6);
GridPane.setConstraints(lastNameLabel, 0, 7);
GridPane.setConstraints(lastName, 1, 7);
GridPane.setConstraints(academicTitleChoice, 2, 7);
GridPane.setConstraints(usernameLabel, 0, 8);
GridPane.setConstraints(username, 1, 8);
GridPane.setConstraints(usernameInfo, 2, 8);
GridPane.setConstraints(passwordLabel, 0, 9);
GridPane.setConstraints(password, 1, 9);
GridPane.setConstraints(passwordInfo, 2, 9);
GridPane.setConstraints(label1, 0, 10);
GridPane.setConstraints(textField1, 1, 10);
GridPane.setConstraints(label2, 0, 11);
GridPane.setConstraints(textField2, 1, 11);
GridPane.setConstraints(label3, 0, 12);
GridPane.setConstraints(textField3, 1, 12);
GridPane.setConstraints(registrationButton, 1, 13);
GridPane.setHalignment(registrationButton, HPos.RIGHT);
layout.getChildren().addAll(title, pickUserLabel, pickStudent, pickProfessor, separator, firstNameLabel, firstName, lastNameLabel, lastName, usernameLabel, username, passwordLabel, password, passwordInfo, academicTitleChoice, label1, label2, textField2, label3, textField3, registrationButton);
layout.setAlignment(Pos.CENTER);
layout.setVgap(10);
layout.setHgap(10);
layout.setId("rightSide");
layout.setPrefSize(620, 550);
return layout;
В результате, если выбран профессор RadioButton, поля изменяются, то же самое касается и студента.