Java – JScrollPane needs to be narrowed down
I have a JScrollPane with a JPanel inside (the panel contains some jlabels)
I want to resize the scroll pane to actually change its size (which may be lower than the preferred size of internal components), not just the size of the viewport
When the user narrows the scroll window too small, the goal is that the internal panel just disappears (using the specific zoom priority in my miglayout, etc.)
Solution
Perhaps the best way is to include components that are always the same width as the viewport To do this, the first included component (a child object of jviewport, passed to the JScrollPane constructor or set to viewportview) needs to implement javax swing. Scrollable. The key method is getscrollabletracksviewportwidth, which should return true
This is a fast and dirty scrollable JPanel:
public class ScrollablePanel extends JPanel implements Scrollable { public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } public int getScrollableUnitIncrement(Rectangle visibleRect,int orientation,int direction) { return 10; } public int getScrollableBlockIncrement(Rectangle visibleRect,int direction) { return ((orientation == SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width) - 10; } public boolean getScrollableTracksViewportWidth() { return true; } public boolean getScrollableTracksViewportHeight() { return false; } }