Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Understanding alignment

cshDec 5 2011 — edited Dec 5 2011
I am trying out layouts and alignments and want to understand them...

Consider this little piece of code:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TestApp extends Application {
    public static void main(String[] args) throws Exception {
        launch(args);
    }


    public void start(Stage stage) throws Exception {
        VBox root = new VBox();
        Button button1 = new Button("Button1");

        HBox hbox = new HBox();
        Button button2 = new Button("Button2");
        Button button3 = new Button("Button3");

        hbox.getChildren().add(button2);
        hbox.getChildren().add(button3);


        root.getChildren().add(button1);
        root.getChildren().add(hbox);

        root.setAlignment(Pos.BOTTOM_RIGHT);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}
button1 is layouted at bottom right, while hbox is not.


I read this article:
http://docs.oracle.com/javafx/2.0/layout/size_align.htm

and wondered about this sentence:

>
The setAlignment() method for the HBox pane centers the HBox pane within its layout area and also centers the nodes within the HBox pane.
>

So, why is the hbox unaffected by the alignment of its parent?

If I set an alignment on button1, it has no effect. In this case you must set the alignment on the PARENT.
If I set an alignment on hbox, it has an effect. In this case you must set the alignment on the node itself, while setting it on the parent has no effect.

So it is confusing to me, ... when do I have to set the alignment on the parent, and when do I need to set on the node itself. Why isn't there consistent behavior?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 2 2012
Added on Dec 5 2011
2 comments
337 views