Skip to content

Commit a6d8604

Browse files
committed
StaticSwingUtils: add JScrollPane utility methods
These are useful for programmatically scrolling a JScrollPane to the bottom of its view, and for detecting that situation.
1 parent 2426510 commit a6d8604

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/main/java/org/scijava/ui/swing/StaticSwingUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@
5858

5959
import javax.swing.AbstractButton;
6060
import javax.swing.Action;
61+
import javax.swing.BoundedRangeModel;
6162
import javax.swing.ImageIcon;
6263
import javax.swing.InputMap;
6364
import javax.swing.JButton;
6465
import javax.swing.JComponent;
6566
import javax.swing.JFrame;
6667
import javax.swing.JMenuItem;
6768
import javax.swing.JPopupMenu;
69+
import javax.swing.JScrollPane;
70+
import javax.swing.JViewport;
6871
import javax.swing.KeyStroke;
6972
import javax.swing.SwingUtilities;
7073

@@ -302,4 +305,22 @@ public static void dispatchToEDTWait(final Runnable runnable) {
302305
runnable.run();
303306
}
304307
}
308+
309+
// -- Utility methods - scrolling --
310+
311+
/** Scrolls the given {@link JScrollPane} to its bottom left corner. */
312+
public static void scrollToBottom(final JScrollPane scrollPane) {
313+
final JViewport viewport = scrollPane.getViewport();
314+
viewport.setViewPosition(new Point(0, viewport.getView().getHeight()));
315+
}
316+
317+
/**
318+
* Checks whether the given {@link JScrollPane} is currently scrolled to the
319+
* bottom of its view.
320+
*/
321+
public static boolean isScrolledToBottom(final JScrollPane scrollPane) {
322+
final BoundedRangeModel m = scrollPane.getVerticalScrollBar().getModel();
323+
return m.getValue() + m.getExtent() >= m.getMaximum();
324+
}
325+
305326
}

0 commit comments

Comments
 (0)