Search code examples
scalascala-swing

Scala Swing: setting relative dividerlocation on SplitPane


It seems almost impossible to set the relative (Double) dividerlocation on a Scala Swing SplitPane at the time of creation of the splitpane to the desired value. The splitter nearly always appears at the top of its parent Component. I've tried using various combinations of revalidate and repaint but nothing seems to work. If I instantiate a frame with a splitpane in the constructor, make it visible and then set the split panes value, the divider appears in the correct position. It also goes to the right position if I set it by calling a function from the user interface. But I want to be able to introduce a Splipanes into already existing Frames.

Is this a known problem? Is there a solution? Should I use a timer to set the divider location after say quarter or half a second? - very messy but should work.

Some sample code, that still doesn't achieve the desired result:

val newSplit = new CSplit(Right(canv1), Right(newMapCanv))        
panel.layout(newSplit.pane) = BorderPanel.Position.Center
thisScn.visible = true
newSplit.pane.dividerLocation = 0.5 
newSplit.pane.bottomComponent = canv1
newSplit.pane.topComponent = canv2
newSplit.pane.dividerLocation = 0.5
newSplit.pane.revalidate
newSplit.pane.repaint
panel.revalidate
panel.repaint
newSplit.pane.dividerLocation = 0.5
newSplit.pane.revalidate
newSplit.pane.repaint
newSplit.pane.dividerLocation = 0.5

Solution

  • Try setting the preferredSize of each component to 0 -> 0. Here is some code I've used while dealing with SplitPane

    oneTouchExpandable = true
    resizeWeight = 0.5
    leftComponent.preferredSize = 0 -> 0
    rightComponent.preferredSize = 0 -> 0