Reputation: 79
I wanna the buttons B and D will be remained on their places when the A and C will be disappeared, but instead of it there are shifted to the center. How could I shift them back to their places?
fifty.setOnAction(e->{
fifty.setDisable(false);
int counter = 2;
ArrayList<String> variants = new ArrayList<>(Arrays.asList("a","b","c","d"));
variants.remove(trueAnswerIndex);
String variant;
int n = 2;
while(counter>0){
variant = variants.get(randInt(0,n));
switch(variant){
case "a":
gridButtons.getChildren().remove(a);
variants.remove("a");
break;
case "b":
gridButtons.getChildren().remove(b);
variants.remove("b");
break;
case "c":
gridButtons.getChildren().remove(c);
variants.remove("c");
break;
case "d":
gridButtons.getChildren().remove(d);
variants.remove("d");
break;
}
counter--;
n--;
}
});
https://i.sstatic.net/pPpC4.jpg
Upvotes: 1
Views: 225
Reputation: 2058
Just disable the visibility of the buttons instead of removing the buttons.
fifty.setOnAction(e->{
fifty.setDisable(false);
int counter = 2;
ArrayList<String> variants = new ArrayList<>(Arrays.asList("a","b","c","d"));
variants.remove(trueAnswerIndex);
String variant;
int n = 2;
while(counter>0){
variant = variants.get(randInt(0,n));
switch(variant){
case "a":
a.setVisible(false);
variants.remove("a");
break;
case "b":
b.setVisible(false);
variants.remove("b");
break;
case "c":
c.setVisible(false);
variants.remove("c");
break;
case "d":
d.setVisible(false);
variants.remove("d");
break;
}
counter--;
n--;
}
});
Upvotes: 1
Reputation: 1986
You can either use some dummy components, like empty Text
nodes, or manually set RowConstraints
and ColumnConstraints
for your GridPane
.
The latter is my preferred approach. In the example below I set these constraints for first two rows and columns, so even if the button gets hidden or removed, other columns will not be automatically resized.
I also put the GridPane
inside another Node
, because otherwise it would fill the whole Scene
.
public class JavaFXTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
GridPane pane = new GridPane();
for (int i = 0; i < 2; i++) {
RowConstraints rc = new RowConstraints();
rc.setPrefHeight(100);
ColumnConstraints cc = new ColumnConstraints();
cc.setPercentWidth(100);
pane.getRowConstraints().add(rc);
pane.getColumnConstraints().add(cc);
}
Button b1 = new Button("1");
b1.setPrefSize(100, 100);
Button b2 = new Button("2");
b2.setPrefSize(100, 100);
Button b3 = new Button("3");
b3.setPrefSize(100, 100);
Button b4 = new Button("4");
b4.setPrefSize(100, 100);
b1.setOnAction(e -> {
b2.setVisible(false);
});
b2.setOnAction(e -> {
b3.setVisible(false);
});
b3.setOnAction(e -> {
b4.setVisible(false);
});
b4.setOnAction(e -> {
b1.setVisible(false);
});
pane.add(b1, 0, 0);
pane.add(b2, 1, 0);
pane.add(b3, 0, 1);
pane.add(b4, 1, 1);
root.getChildren().add(pane);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 0