Java JScrollPane createSelfManagedScrollPane(final Component view, final JComponent parentToRevalidate)

Here you can find the source of createSelfManagedScrollPane(final Component view, final JComponent parentToRevalidate)

Description

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.

License

Apache License

Parameter

Parameter Description
view view to wrap in the scroll pane
parentToRevalidate parent to revalidate when the scroll pane decides to change its size

Return

the created self managed scroll pane

Declaration

public static JScrollPane createSelfManagedScrollPane(final Component view,
        final JComponent parentToRevalidate) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Component;

import java.awt.Dimension;

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JComponent;

import javax.swing.JScrollPane;

public class Main {
    /**//from  w  ww . j  a  v a  2 s  . c o  m
     * Creates and returns a scroll panel which wraps the specified view component.<br>
     * 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. When the view fits into the view port, the scroll pane will not claim the space of the scroll bar.
     * 
     * @param view               view to wrap in the scroll pane
     * @param parentToRevalidate parent to revalidate when the scroll pane decides to change its size
     * 
     * @return the created self managed scroll pane
     */
    public static JScrollPane createSelfManagedScrollPane(final Component view,
            final JComponent parentToRevalidate) {
        final JScrollPane scrollPane = new JScrollPane(view);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 12)); // Only want to restrict the height, width doesn't matter (it takes up whole width)
        scrollPane.getHorizontalScrollBar().setUnitIncrement(10);

        final ComponentListener scrollPaneComponentListener = new ComponentAdapter() {
            @Override
            public void componentResized(final ComponentEvent event) {
                scrollPane.setHorizontalScrollBarPolicy(
                        view.getWidth() < scrollPane.getWidth() ? JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
                                : JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                scrollPane.setPreferredSize(null);
                scrollPane.setPreferredSize(new Dimension(10, scrollPane.getPreferredSize().height));
                parentToRevalidate.revalidate();
            }
        };
        scrollPane.addComponentListener(scrollPaneComponentListener);

        return scrollPane;
    }
}

Related

  1. applyCorrectViewPosition(JComponent targetComponent, JScrollPane scrollPane)
  2. bindHorizontalScrolling(JScrollPane jScrollPane1, JScrollPane jScrollPane2)
  3. buildLabeledScroller(String label, Component contents)
  4. createJScrollPane(JComponent component)
  5. createScrollPane(JComponent content)
  6. defaultAutoScroll(JComponent comp, Point cursorLocn)
  7. defaultGetAutoscrollInsets(JComponent comp)
  8. findScrollPane(Component c)
  9. findScrollPane(Component component)