Search code examples
javafxlabeltextareajavafx-2gridpane

Align a label with textarea


How can I align the position of my "Description" label such that it corresponds in line with the "Enter description" text (Label - TextArea). These components are inside an gridPane and coded in javaFX ( so no Scene Builder tips can help me here ). Code:

Label descriptionLabel = new Label("Description:");
descriptionLabel.setPadding(new Insets(5, 5, 5, 5));
JFXTextArea descriptionTextArea = new JFXTextArea();
descriptionTextArea.setPadding(new Insets(5, 0, 5, 0));
descriptionTextArea.setPromptText("Enter description...");
gridPane.add(descriptionLabel, 0, 2);
gridPane.add(descriptionTextArea, 1, 2);

I've tried with descriptionLabel.setAlignment(Pos.TOP_LEFT); but neither that helped me out


Solution

  • You have to use GridPane Constraints. Valignment set to Top and Halignment set to Right. Java Doc

    GridPane.setHalignment(descriptionLabel, HPos.RIGHT);
    GridPane.setValignment(descriptionLabel, VPos.TOP);
    

    After looking at you picture again, I think you only need setValignment.