001package jmri.util.swing;
002
003import java.awt.Dimension;
004import java.awt.Rectangle;
005import javax.swing.JPanel;
006import javax.swing.Scrollable;
007
008/**
009 * JPanel extension to handle the Scrollable interface so it can behave well in
010 * a vertical JScrollPane
011 *
012 * @author Bob Jacobsen Copyright 2018
013 * @since 4.13.2
014 */
015public class ScrollablePanel extends JPanel implements Scrollable {
016    private final int increment;
017
018    public ScrollablePanel(int increment) {
019        this.increment = increment;
020    }
021
022    public ScrollablePanel() {
023        this.increment = 16; // just a convenient default
024    }
025
026    /** {@inheritDoc} */
027    @Override
028    public Dimension getPreferredScrollableViewportSize() {
029        // tell the JScrollPane that we want to be our 'preferredSize'
030        // but later, we'll say that vertically, it should scroll.
031        return super.getPreferredSize();
032    }
033
034    /** {@inheritDoc} */
035    @Override
036    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
037        return increment;
038    }
039
040    /** {@inheritDoc} */
041    @Override
042    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
043        return increment;
044    }
045
046    /** {@inheritDoc} */
047    @Override
048    public boolean getScrollableTracksViewportWidth() {
049        return true; // track the width, and re-size as needed.
050    }
051
052    /** {@inheritDoc} */
053    @Override
054    public boolean getScrollableTracksViewportHeight() {
055        return false; // we don't want to track the height, because we want to
056                      // scroll vertically.
057    }
058
059}