I am not sure wether I formulated the question well. I have a problem. I created a class that provides my window (javafx Pane) with a row of buttons from top to down. Now I want to have the same with textfields. But I just don't know how to manage it without have to copy and paste the whole code to overload a second or third constructor again and again just to change a button for a label or a textfield. Has anyone an idea how to make it work, that my code stays compact. Thank you in advance. Here the code.
public class ArrBtn {
private ArrayList<Button> arrBtn = new ArrayList<Button>();
private int index;
private double setX;
ArrBtn (int arraySize, Pane root ) { // It ads itself by itself by giving the reference of the Pane.
double distX = 60; // Sets the distance in x-direction between the buttons.
for (int i = 0; i < arraySize -1; i++) {
arrBtn.get(index).setLayoutX (30 +setX*distX); // Sets the x-position of the Buttons.
arrBtn.get(index).setLayoutY (30 ); // Sets the y-position of the Buttons.
arrBtn.get(index).setPrefWidth (40 ); // Sets the width of the Buttons.
arrBtn.get(index).prefHeight (40 ); // Sets the height of the Buttons.
index++;
setX++; // Makes the next button is set on another x-position.
}
}
}
Aucun commentaire:
Enregistrer un commentaire