GuiUtils.java 文件源码

java
阅读 44 收藏 0 点赞 0 评论 0

项目:sc2gears 作者:
/**
 * Creates and returns a scroll panel which wraps the specified view component.<br>
 * The returned scroll panel disables vertical scroll bar, and only displays the horizontal scroll bar when the view does not fit
 * into the size of the view port. When the view fits into the view port, the scroll pane will not claim the space of the scroll bar.
 * 
 * @param view               view to wrap in the scroll pane
 * @param parentToRevalidate parent to revalidate when the scroll pane decides to change its size
 * 
 * @return the created self managed scroll pane
 */
public static JScrollPane createSelfManagedScrollPane( final Component view, final JComponent parentToRevalidate ) {
    final JScrollPane scrollPane = new JScrollPane( view );

    scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
    scrollPane.getHorizontalScrollBar().setPreferredSize( new Dimension( 0, 12 ) ); // Only want to restrict the height, width doesn't matter (it takes up whole width)
    scrollPane.getHorizontalScrollBar().setUnitIncrement( 10 );

    final ComponentListener scrollPaneComponentListener = new ComponentAdapter() {
        @Override
        public void componentResized( final ComponentEvent event ) {
            scrollPane.setHorizontalScrollBarPolicy( view.getWidth() < scrollPane.getWidth() ? JScrollPane.HORIZONTAL_SCROLLBAR_NEVER : JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
            scrollPane.setPreferredSize( null );
            scrollPane.setPreferredSize( new Dimension( 10, scrollPane.getPreferredSize().height ) );
            parentToRevalidate.revalidate();
        }
    };
    scrollPane.addComponentListener( scrollPaneComponentListener );

    return scrollPane;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号