tornadofx icon indicating copy to clipboard operation
tornadofx copied to clipboard

ScrollPane: issue

Open DarioArena87 opened this issue 4 years ago • 3 comments

Hi, it seems that when a scrollpane has some children that can be hidden/shown based on some property only the last child is considered. Example code:

package com.example.view

import javafx.scene.control.ToggleGroup
import javafx.scene.paint.Color
import tornadofx.*

class MainView : View("Hello TornadoFX") {

    private val toggleGroup = ToggleGroup()

    private val visibleComponent = 1.toProperty()

    init {
        visibleComponent.bind(toggleGroup.selectedValueProperty())
    }

    override val root = vbox {
        vbox {
            radiobutton("None", toggleGroup, 0)
            radiobutton("First", toggleGroup, 1)
            radiobutton("Second", toggleGroup,  2)
            radiobutton("Third", toggleGroup,  3)
        }

        scrollpane {
            prefHeight = 500.0

            vbox {
                visibleWhen(visibleComponent.isEqualTo(1))
                prefHeight = 600.0
                label("First Component")
                style {
                    borderColor += box(Color.DARKBLUE)
                    backgroundColor += Color.BLUE
                }
            }

            vbox {
                visibleWhen(visibleComponent.isEqualTo(2))
                prefHeight = 400.0
                label("Second Component")
                style {
                    borderColor += box(Color.DARKGREEN)
                    backgroundColor += Color.GREEN
                }
            }

            vbox {
                visibleWhen(visibleComponent.isEqualTo(3))
                prefHeight = 500.0
                label("Third Component")
                style {
                    borderColor += box(Color.DARKSLATEGRAY)
                    backgroundColor += Color.DIMGRAY
                }
            }
        }
    }
}

If you run this example i'd expect that the first component is visibile and then, based on the radiobutton that i select, the appropriate component is shown but all seems to work only for the last child of the scrollpane. I'm not really an expert in kotlin (i usually program in plain java or groovy) and i don't have a lot of experience with JavaFX either so i don't know if i'm doing something wrong but maybe this is a bug in how the children gets added to the scrollpane itself

DarioArena87 avatar Nov 29 '20 16:11 DarioArena87

It works for me when you wrap your three vboxs in a parent container like a vbox.

Scrollpane is intended to wrap a single node or layout, so when you keep adding vboxes it is just replacing what was there. This is probably done by repeated calls to a ScrollPanes setContent method. This also explains why only your last vbox is working, because it has replaced whatever was there before.

        scrollpane {
            prefHeight = 500.0
            vbox {
                vbox {  }
                vbox {  }
                vbox {  }
                }
            }
        }

SKeeneCode avatar Dec 01 '20 13:12 SKeeneCode

You are right, i also tried encapsulating content in a vbox and it worked. If this is the intended use of scrollpane maybe it's ok but maybe it will be a nice improvement if TornadoFX handles this for the user (perhaps in an optional way)

DarioArena87 avatar Dec 02 '20 10:12 DarioArena87

can better use

stackpane { // vbox
    vbox {  }
    vbox {  }
    vbox {  }
}

SchweinchenFuntik avatar Dec 03 '20 18:12 SchweinchenFuntik