Search code examples
javaswingmiglayoutshrinkautogrow

Force shrinking of MigLayout-Component inside JScrollPane


I have a Component that uses MigLayout as its LayoutManager. It is set to grow inside the JScrollPane to occupy the whole available width. So if I resize the appliation's frame, the JScrollPane is as wide as the frame is and the inside component is as well.

However, when I shrink the frame, the grown component inside the JScrollPane just keeps its width and does not shrink, so horizontal scrollbars are displayed.

I am aware of JScrollpane needs to shrink its width and implemented the Scrollable-Interface with getScrollableTracksViewportWidth=true but MigLayout does not seem to honor it.

Any advice? thanks


Solution

  • I solved it with following scala-snippet. Scala's Scrollable is just a fake wrapper, you are supposed to implement the Java-Scrollable...

    import javax.swing.JPanel
    import javax.swing.{Scrollable => JScrollable}
    import javax.swing.BoxLayout
    import java.awt.Dimension
    import java.awt.Rectangle
    import scala.swing.SequentialContainer
    import scala.swing.Orientation
    import scala.swing.Scrollable
    import scala.swing.Panel
    
    class ScrollablePanel extends Panel with SequentialContainer.Wrapper with Scrollable.Wrapper {
    
      var scrollIncrement: Int = 10
      var blockScrollIncrement: Int = 50
    
      val allowVerticalScrolling: Boolean   = true
      val allowHorizontalScrolling: Boolean = false
    
      override lazy val peer = new JPanel with SuperMixin with JScrollable {
        def getPreferredScrollableViewportSize: Dimension =
          getPreferredSize
    
        def getScrollableTracksViewportHeight: Boolean =
          !allowVerticalScrolling
    
        def getScrollableTracksViewportWidth: Boolean =
          !allowHorizontalScrolling
    
        def getScrollableBlockIncrement(visibleRect: Rectangle, orientation: Int, direction: Int): Int =
          scrollIncrement
    
        def getScrollableUnitIncrement(visibleRect: Rectangle, orientation: Int, direction: Int): Int =
          blockScrollIncrement
      }
    
      final protected def scrollablePeer: JScrollable = peer
    }