强制缩小JScrollPane中的MigLayout-Component

时间:2012-01-21 00:38:27

标签: java swing miglayout shrink autogrow

我有一个使用MigLayout作为其LayoutManager的Component。它设置为在JScrollPane内部增长以占用整个可用宽度。因此,如果我调整appliation框架的大小,则JScrollPane与框架一样宽,内部组件也是如此。

但是,当我缩小框架时,JScrollPane内部生长的组件只保持其宽度而不缩小,因此会显示水平滚动条。

我知道JScrollpane needs to shrink its width并使用getScrollableTracksViewportWidth = true实现了Scrollable-Interface,但MigLayout似乎并不尊重它。

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:3)

我用以下scala-snippet解决了它。 Scala的Scrollable只是一个假的包装器,你应该实现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
}