views:

216

answers:

2

I am having some problem understanding layouts in JavaFX. Consider following code.

Stage {
    title: "ListView test"
    scene: Scene {
        width: 400
        height: 400
        content: [
            VBox {
                content: [
                    ListView {
                        height: 200
                        width: 200
                        items: ["item1", "item2"]
                    }
                ]
            }
        ]
    }
}

I was expecting ListView showing up in 200 x 200 dimension but no matter how I tried to fix this, the width and height of ListView seemed fixed. But following code works for showing ListView as I intended.

Stage {
    title: "ListView test"
    scene: Scene {
        width: 400
        height: 400
        content: [
            ListView {
                height: 200
                width: 200
                items: ["item1", "item2"]
            }
        ]
    }
}

So, what is the problem here? I cannot use ListView within layouts?

+1  A: 

Layout nodes are allowed to change the position and size of their children. Simply setting a child's width and height and expecting the layout to know it's supposed to respect your new values is no good, what you need is some way to tell the layout node the values you'd like to respect. Take a look at the LayoutInfo class, and the layoutInfo variable in Node.

Simon Morris
Thanks. LayoutInfo works fine for me. I'll get used to this but it feels strange that you have to do this even though width and height is writable.
Jhonghee
+3  A: 

In JavaFX 1.2.x the width and height information on node in layout is for reading purpose. If you want to specify "preferred" dimension use

ListVIew {
    layoutInfo: LayoutInfo {width: 200, height: 200}
}

This will be processed in layout as preferred size and component will be layout-ed according to this. If you have experience with NetBeans JavaFX Composer Preview you may noticed that generated code use special kind of binding witch allows correct sizing in layout and other containers (Scene, Group).

Ad Broken: The layouts in Java FX are build over Node and uses algorithmic approach to positioning and resizing components which is independent to geometrical position used in Group/Scene. Unfortunately in 1.2 it brings some sort of duality which should be misleading for beginners.

Rastislav Komara
Thanks for a good explanation on layouts.
Jhonghee

related questions