Java Utililty Methods JScrollPane Scroll

List of utility methods to do JScrollPane Scroll

Description

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

Method

voidscrollComponentTo(JComponent c, Rectangle r)
Scroll a component contained in a JScrollPane to its end
int extraHeight = 40; 
Rectangle rr = new Rectangle(r.x, r.y - extraHeight, r.width, 2 * extraHeight + r.height);
Rectangle rrr = rr.intersection(c.getBounds());
if (rrr.height > 0)
    c.scrollRectToVisible(rrr);
voidscrollComponentToEnd(JComponent c)
Scroll a component contained in a JScrollPane to its end
Rectangle r = c.getBounds();
scrollComponentTo(c, new Rectangle(0, r.height - 2, r.width, 2));
JScrollPanescrolleable(final JComponent comp)
scrolleable
final JScrollPane scroll = new JScrollPane();
if (comp != null) {
    scroll.setViewportView(comp);
scroll.setOpaque(false);
scroll.getViewport().setOpaque(false);
return scroll;
JComponentscrollH(JComponent content)
scroll H
JScrollPane scroll = new JScrollPane(content);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
return scroll;
voidscrollRectLater(final JComponent comp, final Rectangle r)
scroll Rect Later
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        comp.scrollRectToVisible(r);
});
voidscrollRectToVisible(Component component, Rectangle aRect)
scroll Rect To Visible
Container parent;
int dx = component.getX(), dy = component.getY();
for (parent = component.getParent(); parent != null
        && (!(parent instanceof JViewport) || (((JViewport) parent)
                .getClientProperty("HierarchicalTable.mainViewport") == null)); parent = parent
                        .getParent()) {
    Rectangle bounds = parent.getBounds();
    dx += bounds.x;
...
voidscrollToComponent(Component cmp)
scroll To Component
Container parent = cmp.getParent();
while (parent != null) {
    if (parent instanceof JScrollPane) {
        JScrollPane pane = (JScrollPane) parent;
        Point location = getRelativeLocation(pane.getViewport().getView(), cmp);
        Rectangle bounds = cmp.getBounds();
        bounds.setLocation(location);
        pane.getViewport().scrollRectToVisible(bounds);
...
voidscrollToVisible(int row, int col)
Assumes table is contained in a JScrollPane.
scrollToVisible(getJTable(), row, col);
JComponentscrollV(String title, JComponent content)
scroll V
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createTitledBorder(title));
JScrollPane scroll = new JScrollPane(content);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
p.add(scroll);
return p;
voidscrollVertically(JComponent c, Rectangle r)
scroll Vertically
scrollVertically(c, r.y, r.y + r.height);