Reputation: 67
for a GUI i have to set multiple buttons to a GridPane
. For this i want to ask if there is a way to add multiple buttons in a Grid in a more elegant way than i do.
Here is an extractof my Code.
Button button0 =new Button("0");
Button button1 =new Button("1");
Button button2 =new Button("2");
Button button3 =new Button("3");
Button button4 =new Button("4");
Button button5 =new Button("5");
Button button6 =new Button("6");
Button button7 =new Button("7");
Button button8 =new Button("8");
Button button9 =new Button("9");
Button cancel = new Button("C");
Button plus = new Button("+");
Button minus = new Button("-");
Button multiplicate = new Button("*");
Button divide = new Button("/");
Button equal = new Button("=");
For all of those buttons i have to set the row and column index separately.
root.setPadding(new Insets(5,5,5,5));
GridPane.setConstraints(root, 4, 3);
root.getChildren();
root.add(label, 1, 1, 3, 1);
root.add(button1, 2, 2);
root.add(button2, 3, 2);
root.add(button3, 4, 2);
root.add(button4, 2, 3);
root.add(button5, 3, 3);
root.add(button6, 4, 3);
root.add(button7, 2, 4);
root.add(button8, 3, 4);
root.add(button9, 4, 4);
root.add(button0, 2, 5);
root.add(cancel, 3, 5);
root.add(plus, 5,2);
root.add(minus, 5,3);
root.add(multiplicate, 5, 4);
root.add(divide, 5,5);
root.add(equal, 4, 5);
I would be very greateful for any suggestions.
Upvotes: 2
Views: 4756
Reputation: 21829
You should use an FXML file as was suggested in a comment also.
But if you want to stick with Java code, you can do something like this (my suggestion is still have an FXML file):
Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane root = new GridPane();
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Button b = new Button();
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
GridHelper.addToGrid(root, new Button("0"), 2, 5);
GridHelper.addToGrid(root, new Button("1"), 2, 2);
GridHelper.addToGrid(root, new Button("2"), 3, 2);
GridHelper.addToGrid(root, new Button("3"), 4, 2);
GridHelper.addToGrid(root, new Button("4"), 2, 3);
GridHelper.addToGrid(root, new Button("5"), 3, 3);
GridHelper.addToGrid(root, new Button("6"), 4, 3);
GridHelper.addToGrid(root, new Button("7"), 2, 4);
GridHelper.addToGrid(root, new Button("8"), 3, 4);
GridHelper.addToGrid(root, new Button("9"), 4, 4);
GridHelper.addToGrid(root, new Button("C"), 3, 5, e -> System.out.println("C"));
GridHelper.addToGrid(root, new Button("+"), 5, 2, e -> System.out.println("+"));
GridHelper.addToGrid(root, new Button("-"), 5, 3);
GridHelper.addToGrid(root, new Button("*"), 5, 4);
GridHelper.addToGrid(root, new Button("/"), 5, 5);
GridHelper.addToGrid(root, new Button("="), 4, 5);
root.setPadding(new Insets(5, 5, 5, 5));
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
static class GridHelper {
static void addToGrid(GridPane grid, Node c, int col, int r) {
grid.add(c, col, r);
}
static void addToGrid(GridPane grid, Node c, int col, int r, EventHandler<ActionEvent> event) {
grid.add(c, col, r);
addButtonEvent(c, event);
}
static void addToGrid(GridPane grid, Node c, int col, int r, int cs, int rs) {
grid.add(c, col, r, cs, rs);
}
static void addToGrid(GridPane grid, Node c, int col, int r, int cs, int rs, EventHandler<ActionEvent> event) {
grid.add(c, col, r, cs, rs);
addButtonEvent(c, event);
}
static void addButtonEvent(Node button, EventHandler<ActionEvent> event) {
if (button instanceof Button)
((Button) button).setOnAction(event);
}
}
public static void main(String[] args) {
launch(args);
}
}
It just simply introduces a static class that exposes some static methods to add a Node
to a GridPane
to the specified position, and optionally set the spans (row/colum). If it is a Button
you can add a press event also. Could be generalized further of course, it is just a sample for the exact question.
Also you could make a loop for Button
s 0 - 9 (as it was proposed in the comment).
Upvotes: 1
Reputation: 36
public class MainController implements Initializable {
int z1=0,y2=0,x=0;
@FXML
GridPane gp;
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
while(z1<4){ //add 4 buttons
addButton();
z1++;
}
}
private void addButton() {
// TODO Auto-generated method stub
final Button temp1=new Button("Button "+ y2);
final int numberButton=y2;
temp1.setId(""+y2);
temp1.setText("whatever u want!!");
temp1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("id("+temp1.getId()+")="+numberButton);
}
});
gp.add(temp1, x, 0); //x is column index and 0 is row index
x++;
}
}
Upvotes: 0