How to set text alignment


Question

I have this dialog with text.

public class DX57DC extends Application
{

    public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws URISyntaxException, MalformedURLException
    {

        // Image
        ImageView iv = new ImageView(getClass().getResource("system-help.png").toExternalForm());

        // Text
        Text t = new Text();
        t.setTextAlignment(TextAlignment.LEFT);
        t.setText("Do you want to quit?");

        HBox thbox = new HBox(8); // spacing = 8
        thbox.getChildren().addAll(thbox);
        thbox.setAlignment(Pos.CENTER);

        // Buttons
        Button btnYes = new Button("Yes");
        Button btnNo = new Button("No");
        btnYes.setStyle("-fx-background-color:\n"
                + "        rgba(0,0,0,0.08),\n"
                + "        linear-gradient(#9a9a9a, #909090),\n"
                + "        linear-gradient(white 0%, #f3f3f3 50%, #ececec 51%, #f2f2f2 100%);\n"
                + "    -fx-background-insets: 0 0 -1 0,0,1;\n"
                + "    -fx-background-radius: 5,5,4;\n"
                + "    -fx-padding: 10 26 10 26;\n"
                + "    -fx-text-fill: #242d35;\n"
                + "    -fx-font-size: 14px;");

        btnNo.setStyle("-fx-background-color:\n"
                + "        rgba(0,0,0,0.08),\n"
                + "        linear-gradient(#9a9a9a, #909090),\n"
                + "        linear-gradient(white 0%, #f3f3f3 50%, #ececec 51%, #f2f2f2 100%);\n"
                + "    -fx-background-insets: 0 0 -1 0,0,1;\n"
                + "    -fx-background-radius: 5,5,4;\n"
                + "    -fx-padding: 10 26 10 26;\n"
                + "    -fx-text-fill: #242d35;\n"
                + "    -fx-font-size: 14px;");

        // Image layout
        VBox vbox = new VBox(7);
        vbox.getChildren().addAll(iv);
        vbox.setAlignment(Pos.CENTER);

        // Buttons layout
        HBox hbox = new HBox(8); // spacing = 8
        //hbox.setStyle("-fx-padding: 10; -fx-font-size: 10pt;");
        hbox.getChildren().addAll(btnYes, btnNo);
        hbox.setAlignment(Pos.CENTER);

        BorderPane bp = new BorderPane();
        bp.setStyle("-fx-background-color: linear-gradient(#ffffff,#f3f3f4);\n"
                + "    -fx-border-width: 1 1 1 1;\n"
                + "    -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;\n"
                + "    -fx-font-size: 1.083333em;\n"
                + "    -fx-text-fill: #292929;");

        bp.setPadding(new Insets(15, 20, 15, 20));
        //Button btnTop = new Button("Top");
        bp.setTop(null);
        //Button btnLeft = new Button("Left");
        bp.setLeft(vbox);
        //Button btnCenter = new Button("Center");
        bp.setCenter(t);
        //Button btnRight = new Button("Right");
        bp.setRight(null);
        //Button btnBottom = new Button("Bottom");
        bp.setBottom(hbox);
        Scene scene = new Scene(bp, 500, 160);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Question Dialog");
        primaryStage.show();
    }
}

Can you tell me how I can move the text close to the picture. Now the text is positioned at the center of the border pane.

1
3
6/8/2013 8:44:19 AM

Accepted Answer

The same like you did for the ImageView, wrap it in a VBox :)

VBox vbox2 = new VBox();
vbox2.getChildren().addAll(t);
vbox2.setAlignment(Pos.CENTER_LEFT);

Then add it to the center of your borderpane:

bp.setCenter(vbox2);
5
6/8/2013 2:01:25 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon