Here you can find the source of createSelfManagedScrollPane(final Component view, final JComponent parentToRevalidate)
Parameter | Description |
---|---|
view | view to wrap in the scroll pane |
parentToRevalidate | parent to revalidate when the scroll pane decides to change its size |
public static JScrollPane createSelfManagedScrollPane(final Component view, final JComponent parentToRevalidate)
//package com.java2s; //License from project: Apache License import java.awt.Component; import java.awt.Dimension; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import javax.swing.JComponent; import javax.swing.JScrollPane; public class Main { /**//from w ww . j a v a 2 s . c o m * 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; } }