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

voidaddStyle(JScrollPane jScrollPane, String labelName)
add Style
Border line = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
TitledBorder titled = BorderFactory.createTitledBorder(line, labelName);
titled.setTitleFont(new Font("Verdana", 0, 13));
titled.setTitleColor(new Color(213, 225, 185));
Border empty = new EmptyBorder(5, 8, 5, 8);
CompoundBorder border = new CompoundBorder(titled, empty);
jScrollPane.setBorder(border);
jScrollPane.setForeground(new Color(143, 170, 220));
...
voidapplyCorrectViewPosition(JComponent targetComponent, JScrollPane scrollPane)
apply Correct View Position
try {
    Point point = new Point(0, (int) (targetComponent.getSize().getHeight()));
    JViewport vp = scrollPane.getViewport();
    if (vp == null) {
        return;
    vp.setViewPosition(point);
} catch (Throwable ignored) {
...
voidbindHorizontalScrolling(JScrollPane jScrollPane1, JScrollPane jScrollPane2)
bind Horizontal Scrolling
jScrollPane2.getHorizontalScrollBar().setModel(jScrollPane1.getHorizontalScrollBar().getModel());
JScrollPanebuildLabeledScroller(String label, Component contents)
build Labeled Scroller
JScrollPane ret = new JScrollPane(contents);
ret.setBorder(buildLabeledBorder(label));
return ret;
JScrollPanecreateJScrollPane(JComponent component)
create J Scroll Pane
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(component);
return scrollPane;
JScrollPanecreateScrollPane(JComponent content)
creates a scrollpane with the given component embedded.
JScrollPane scrollPane = new JScrollPane(content, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
return scrollPane;
JScrollPanecreateSelfManagedScrollPane(final Component view, final JComponent parentToRevalidate)
Creates and returns a scroll panel which wraps the specified view component.
The returned scroll panel disables vertical scroll bar, and only displays the horizontal scroll bar when the view does not fit into the size of the view port.
final JScrollPane scrollPane = new JScrollPane(view);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 12)); 
scrollPane.getHorizontalScrollBar().setUnitIncrement(10);
final ComponentListener scrollPaneComponentListener = new ComponentAdapter() {
    @Override
    public void componentResized(final ComponentEvent event) {
        scrollPane.setHorizontalScrollBarPolicy(
...
voiddefaultAutoScroll(JComponent comp, Point cursorLocn)
Implements autoscrolling.
Rectangle visible = comp.getVisibleRect();
int x = 0, y = 0, width = 0, height = 0;
if (cursorLocn.x < visible.x + AUTOSCROLL_INSET_SIZE) {
    x = -SCROLL_AMOUNT;
    width = SCROLL_AMOUNT;
else if (cursorLocn.x > visible.x + visible.width - AUTOSCROLL_INSET_SIZE) {
    x = visible.width + SCROLL_AMOUNT;
...
InsetsdefaultGetAutoscrollInsets(JComponent comp)
default Get Autoscroll Insets
Rectangle visible = comp.getVisibleRect();
Dimension size = comp.getSize();
int top = 0, left = 0, bottom = 0, right = 0;
if (visible.y > 0) {
    top = visible.y + AUTOSCROLL_INSET_SIZE;
if (visible.x > 0) {
    left = visible.x + AUTOSCROLL_INSET_SIZE;
...
JScrollPanefindScrollPane(Component c)
find Scroll Pane
while (c != null) {
    if (c instanceof JScrollPane) {
        JScrollPane sp = (JScrollPane) c;
        if (((sp.getHorizontalScrollBar() != null) && sp.getHorizontalScrollBar().isVisible())
                || ((sp.getVerticalScrollBar() != null) && sp.getVerticalScrollBar().isVisible())) {
            return sp;
    c = c.getParent();
return null;