Issue
I am working on creating a loan calculator but now sure how to align the flow pane positioning so that everything is centered, as it is currently skewed.
public class Main extends Application {
private HBox hboxAnnualIntRate = new HBox(lbAnnualIntRate,tfAnnualInterestRate);
private HBox hboxNumberOfYears = new HBox(lbNumberOfYears,tfNumberOfYears);
private HBox hboxLoanAmount = new HBox(lbLoanAmount,tfLoanAmount);
private HBox hboxMonthlyPayment = new HBox(lbMonthlyPayment,tfMonthlyPayment);
private HBox hboxTotalPayment = new HBox(lbTotalPayment,tfTotalPayment);
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Loan Calculator");
FlowPane rootNode = new FlowPane(20,20);
rootNode.setAlignment(Pos.CENTER);
Scene scene = new Scene (rootNode, 500, 300);
primaryStage.setScene(scene);
calculateButton.setOnAction(actionEvent -> {
calculatePayment();
});
rootNode.getChildren().addAll(hboxAnnualIntRate,hboxNumberOfYears,hboxLoanAmount,hboxMonthlyPayment,hboxTotalPayment,calculateButton);
primaryStage.show();
}
Solution
As @trashgod mention in the comments you can use a GridPane
:
public class App extends Application {
@Override
public void start(Stage stage) {
Label lbl1 = new Label("Annual Interest Rate:");
Label lbl2 = new Label("Number Of Years:");
Label lbl3 = new Label("Loan Amount:");
Label lbl4 = new Label("Monthly Payment:");
Label lbl5 = new Label("Total Payment:");
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TextField tf3 = new TextField();
TextField tf4 = new TextField();
TextField tf5 = new TextField();
Button button = new Button("Calculate");
GridPane gp = new GridPane();
gp.addColumn(0, lbl1, lbl2, lbl3, lbl4, lbl5);
gp.addColumn(1, tf1, tf2, tf3, tf4, tf5);
gp.add(button, 2, 4);
gp.setHgap(5d);
gp.setVgap(10d);
gp.setPadding(new Insets(20));
Scene scene = new Scene(new StackPane(gp));
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Output:
Answered By - Oboe