Java Utililty Methods JScrollPane

List of utility methods to do JScrollPane

Description

The list of methods to do JScrollPane are organized into topic(s).

Method

voidscrollToTop(final JScrollPane scrollPane)
Scrolls scroll pane to the top left corner.
JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
verticalScrollBar.setValue(verticalScrollBar.getMinimum());
horizontalScrollBar.setValue(horizontalScrollBar.getMinimum());
voidscrollToTop(JScrollPane scrollPane)
scroll To Top
final JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
verticalScrollBar.setValue(verticalScrollBar.getMinimum());
voidscrollToTopLater(final JScrollPane scrollPane)
Scrolls scroll pane to the top left corner a bit later.
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        scrollToTop(scrollPane);
});
voidscrollToVisible(Component comp, JScrollPane parent)
Scrolls a component to the view position of a scrollpane if necessary
Point p0 = parent.getViewport().getViewPosition();
Point p1 = comp.getLocation();
Rectangle r0 = parent.getViewport().getBounds();
Rectangle r1 = comp.getBounds();
if (p0.x > p1.x || p0.y > p1.y || (p0.x < p1.x && (p0.x + r0.width < p1.x + r1.width))
        || (p0.y < p1.y && (p0.y + r0.height < p1.y + r1.height)))
    parent.getViewport().setViewPosition(comp.getBounds().getLocation());
voidsetDefaultScrollbarUnits(JScrollPane scrollPane)
Set reasonable unit increments for a scroll pane that does not contain a Scrollable.
int unit = new JLabel().getFont().getSize() * 2;
scrollPane.getHorizontalScrollBar().setUnitIncrement(unit);
scrollPane.getVerticalScrollBar().setUnitIncrement(unit);
voidsetScrollpane(JScrollPane scrollpane)
OS X shows scrollpanes even if there is no data to scroll.
scrollpane.setHorizontalScrollBarPolicy(Integer.parseInt(System.getProperty("scrollbar.horizontal")));
scrollpane.setVerticalScrollBarPolicy(Integer.parseInt(System.getProperty("scrollbar.vertical")));
voidsetScrollPaneSize(JScrollPane scrollPane, int rows, int columns)
set Scroll Pane Size
final Component view = scrollPane.getViewport().getView();
final FontMetrics fontMetrics = view.getFontMetrics(view.getFont());
final int width = fontMetrics.charWidth('m') * columns;
scrollPane.setPreferredSize(new Dimension(width, fontMetrics.getHeight() * rows));
voidsetUnitIncrement(JScrollPane scroller)
set Unit Increment
scroller.getVerticalScrollBar().setUnitIncrement(20);
scroller.getHorizontalScrollBar().setUnitIncrement(20);
JFrameshow(JComponent comp, String windowName, boolean hasScrollBars, int x, int y, int width, int height)
show
JFrame frame = new JFrame(windowName);
frame.setLocation(x, y);
if (hasScrollBars) {
    JScrollPane scroll = new JScrollPane(comp);
    frame.add(scroll, BorderLayout.CENTER);
} else {
    frame.add(comp, BorderLayout.CENTER);
frame.setSize(width, height);
frame.setVisible(true);
return frame;
PointtoScrollPane(Component component, Point componentPoint, Point resultPoint)
to Scroll Pane
int px = componentPoint.x;
int py = componentPoint.y;
while (!(component instanceof JScrollPane)) {
    px += component.getX();
    py += component.getY();
    component = component.getParent();
if (resultPoint == null) {
...