I´m trying to configure a small GUI with JavaFX. At the start there is the possibility to give some information regarding a date and a time (when you´ve worked) with two textfields. Underneath there is a Checkbox which you can check if you want to “Choose more days?”.
Now there are two possibilities I want to enable:
a) When you check the box you can see more textfields to grant more information about different dates
b) When you check the box by mistake, the textfields should disappear when the box is unchecked and the screen is the same like at the start
I have already the following part of the code. It is already working that I can see more textfields, when I checke the box, but when the Box is unchecked, the textfields are still there.
CheckBox moreDays= new CheckBox("Choose more days?");
EventHandler<ActionEvent> showMoreLabels = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
if (moreDays.isSelected()) {
Label dayTwo= new Label("Day Two:");
pane.add(dayTwo, 0, 7);
final TextField secondDay = new TextField();
pane.add(secondDay, 1, 7);
Label dayThree= new Label("Day Three:");
pane.add(dayThree, 2, 7);
final TextField thirdDay= new TextField();
pane.add(thirdDay, 3, 7);
} else {
}
}
};
moreDays.setOnAction(showMoreLabels);
I´ve tried to create a if-else-statement, when the Checkbox is checked, the textfields should be shown and if the statement is false the textfields shouldn´t be visible. But it as mentioned not working. I hope that somebody can help me.
Thanks already in advance!
CodePudding user response:
Here are a couple of other solutions:
Label dayTwo = new Label("Day Two:");
TextField secondDay = new TextField();
Label dayThree = new Label("Day Three:");
TextField thirdDay = new TextField();
CheckBox moreDays = new CheckBox("Choose more days?");
moreDays.setOnAction(e -> {
if (moreDays.isSelected()) {
pane.add(dayTwo, 0, 7);
pane.add(secondDay, 1, 7);
pane.add(dayThree, 2, 7);
pane.add(thirdDay, 3, 7);
} else {
pane.getChildren().removeAll(dayTwo, secondDay, dayThree, thirdDay);
}
});
or
Label dayTwo = new Label("Day Two:");
pane.add(dayTwo, 0, 7);
TextField secondDay = new TextField();
pane.add(secondDay, 1, 7);
Label dayThree = new Label("Day Three:");
pane.add(dayThree, 2, 7);
TextField thirdDay = new TextField();
pane.add(thirdDay, 3, 7);
bindVisibility(moreDays, dayTwo, secondDay, dayThree, thirdDay);
// ...
private void bindVisibility(CheckBox check, Node... nodes) {
for (Node node : nodes) {
node.visibleProperty().bind(check.selectedProperty());
node.managedProperty().bind(check.selectedProperty());
}
}
CodePudding user response:
Let's change your code a little bit:
Label dayTwo = new Label("Day Two:");
dayTwo.setVisible(false);
pane.add(dayTwo, 0, 7);
TextField secondDay = new TextField();
secondDay.setVisible(false);
pane.add(secondDay, 1, 7);
Label dayThree = new Label("Day Three:");
dayThree.setVisible(false);
pane.add(dayThree, 2, 7);
TextField thirdDay = new TextField();
thirdDay.setVisible(false);
pane.add(thirdDay, 3, 7);
CheckBox moreDays = new CheckBox("Choose more days?");
EventHandler<ActionEvent> showMoreLabels = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
dayTwo.setVisible(moreDays.isSelected());
secondDay.setVisible(moreDays.isSelected());
dayThree.setVisible(moreDays.isSelected());
thirdDay.setVisible(moreDays.isSelected());
}
};
moreDays.setOnAction(showMoreLabels);
I strongly recommend you finding better names for your labels etc. (e.g. labelDayTwo
).