Example usage for javax.swing JScrollPane getBounds

List of usage examples for javax.swing JScrollPane getBounds

Introduction

In this page you can find the example usage for javax.swing JScrollPane getBounds.

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTextArea cmp = new JTextArea();
    String str = "a";
    for (int i = 0; i < 20; i++) {
        cmp.append(str + str + "\n");
    }/*from  www.  j a  va  2  s  . c om*/
    JScrollPane scrollPane = new JScrollPane(cmp);
    scrollPane.setComponentZOrder(scrollPane.getVerticalScrollBar(), 0);
    scrollPane.setComponentZOrder(scrollPane.getViewport(), 1);
    scrollPane.getVerticalScrollBar().setOpaque(false);

    scrollPane.setLayout(new ScrollPaneLayout() {
        @Override
        public void layoutContainer(Container parent) {
            JScrollPane scrollPane = (JScrollPane) parent;

            Rectangle availR = scrollPane.getBounds();
            availR.x = availR.y = 0;

            Insets parentInsets = parent.getInsets();
            availR.x = parentInsets.left;
            availR.y = parentInsets.top;
            availR.width -= parentInsets.left + parentInsets.right;
            availR.height -= parentInsets.top + parentInsets.bottom;

            Rectangle vsbR = new Rectangle();
            vsbR.width = 12;
            vsbR.height = availR.height;
            vsbR.x = availR.x + availR.width - vsbR.width;
            vsbR.y = availR.y;

            if (viewport != null) {
                viewport.setBounds(availR);
            }
            if (vsb != null) {
                vsb.setVisible(true);
                vsb.setBounds(vsbR);
            }
        }
    });
    scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(scrollPane);
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:Main.java

public static boolean canVScroll(JViewport viewport) {
    JScrollPane scrollPane = (JScrollPane) viewport.getParent();
    Rectangle availR = scrollPane.getBounds();

    Component view = viewport.getView();
    Dimension viewPrefSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
    Dimension extentSize = viewport.toViewCoordinates(availR.getSize());

    boolean canVScroll = true;
    if (view instanceof Scrollable)
        canVScroll = !((Scrollable) view).getScrollableTracksViewportHeight();
    if (canVScroll && (viewPrefSize.height <= extentSize.height))
        canVScroll = false;//from   w  w  w . j a v a  2 s . c  o m

    return canVScroll;
}

From source file:Main.java

public static boolean canHScroll(JViewport viewport) {
    JScrollPane scrollPane = (JScrollPane) viewport.getParent();
    Rectangle availR = scrollPane.getBounds();

    Component view = viewport.getView();
    Dimension viewPrefSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
    Dimension extentSize = viewport.toViewCoordinates(availR.getSize());

    boolean canHScroll = true;
    if (view instanceof Scrollable)
        canHScroll = !((Scrollable) view).getScrollableTracksViewportWidth();
    if (canHScroll && (viewPrefSize.width <= extentSize.width))
        canHScroll = false;//from w  ww. j  av  a2 s  . c  o  m

    return canHScroll;
}