Example usage for javax.swing JScrollPane setLayout

List of usage examples for javax.swing JScrollPane setLayout

Introduction

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

Prototype

public void setLayout(LayoutManager layout) 

Source Link

Document

Sets the layout manager for this JScrollPane.

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   w  w  w. j  a  v a  2 s .  c  o m
    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

/**
 * @param scrollPane/* www .  ja v  a  2 s . c o  m*/
 * @return
 */
public static GridBagConstraints initScrollPane(JScrollPane scrollPane) {
    scrollPane.setLayout(new ScrollPaneLayout());
    scrollPane.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    GridBagConstraints gb = new GridBagConstraints();
    gb.gridx = 0;
    gb.gridy = 0;
    gb.insets = new Insets(2, 2, 2, 2);
    return gb;
}