Class ConstrainedViewportLayout

  • All Implemented Interfaces:
    java.awt.LayoutManager, java.io.Serializable

    public class ConstrainedViewportLayout
    extends javax.swing.ViewportLayout
    This is a tweaked ViewportLayout implementation to work round an issue with scrollbar positioning.

    The problem arises when you have scrollbar policies VERTICAL_SCROLLBAR_AS_NEEDED and HORIZONTAL_SCROLLBAR_NEVER. When the vertical scrollbar appears, it is not accounted for in the preferred size of the scrollpane component. This can, depending on the parent layout, lead to the right hand side of the view component being obscured by the scrollbar.

    You can apparently work round the problem with this class, by doing

        scrollPane.getViewport().setLayout(new ConstraintedViewportLayout());
     
    I found this solution at stack overflow. I don't really understand why it works.
    Since:
    27 Nov 2017
    Author:
    Mark Taylor, https://stackoverflow.com/users/463018/meyertee
    See Also:
    Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.awt.Dimension preferredLayoutSize​(java.awt.Container parent)  
      • Methods inherited from class javax.swing.ViewportLayout

        addLayoutComponent, layoutContainer, minimumLayoutSize, removeLayoutComponent
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ConstrainedViewportLayout

        public ConstrainedViewportLayout()
    • Method Detail

      • preferredLayoutSize

        public java.awt.Dimension preferredLayoutSize​(java.awt.Container parent)
        Specified by:
        preferredLayoutSize in interface java.awt.LayoutManager
        Overrides:
        preferredLayoutSize in class javax.swing.ViewportLayout