Example usage for javax.swing JScrollPane getVerticalScrollBar

List of usage examples for javax.swing JScrollPane getVerticalScrollBar

Introduction

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

Prototype

@Transient
public JScrollBar getVerticalScrollBar() 

Source Link

Document

Returns the vertical scroll bar that controls the viewports vertical view position.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<Vector<String>> rowData = new Vector<Vector<String>>();
    Vector<String> columnName = new Vector<String>(Arrays.asList("Column 1"));
    for (int i = 0; i < 2000; i++) {
        rowData.add(new Vector<String>(Arrays.asList(Integer.toString(i))));
    }/*from   ww  w  .  ja  v  a 2s .  c o m*/
    JTable table = new JTable(rowData, columnName);
    JScrollPane scrollPane = new JScrollPane(table);
    JScrollBar vertical = scrollPane.getVerticalScrollBar();
    vertical.setPreferredSize(new Dimension(0, 0));
    f.add(scrollPane);
    f.pack();
    f.setVisible(true);
    JViewport view = scrollPane.getViewport();
    Component[] components = view.getComponents();
    for (int i1 = 0; i1 < components.length; i1++) {
        if (components[i1] instanceof JTable) {
            System.out.println("got");
        }
    }
}

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 ww  . jav  a  2s . co 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

public static void main(String[] args) throws UnsupportedEncodingException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JPanel buttons = new JPanel();
    JScrollPane pane = new JScrollPane(buttons);
    pane.getViewport().addChangeListener(e -> {
        System.out.println("Change in " + e.getSource());
        System.out.println("Vertical visible? " + pane.getVerticalScrollBar().isVisible());
        System.out.println("Horizontal visible? " + pane.getHorizontalScrollBar().isVisible());
    });/* ww  w .j a va2 s. co  m*/
    panel.add(pane);
    frame.setContentPane(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(800);
                buttons.add(new JButton("Hello " + i));
                buttons.revalidate();
            }
            return null;
        }
    };
    worker.execute();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);

    // Listen for value changes in the scroll pane's scrollbars
    AdjustmentListener listener = new MyAdjustmentListener();
    pane.getHorizontalScrollBar().addAdjustmentListener(listener);
    pane.getVerticalScrollBar().addAdjustmentListener(listener);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    JTextArea b = new JTextArea();
    b.setPreferredSize(new Dimension(600, 600));
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: ");
            dumpInfo(e);//from  w  w w .j  av  a  2s  . c  om
        }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Vertical: ");
            dumpInfo(e);
        }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JButton b = new JButton(icon);
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: ");
            dumpInfo(e);//from  w  w w .ja  v  a2  s .  co m
        }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Vertical: ");
            dumpInfo(e);
        }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:AdjustmentTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JButton b = new JButton(icon);
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: ");
            dumpInfo(e);/*from w  w  w  .j a v  a  2  s  .  co  m*/
        }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Vertical: ");
            dumpInfo(e);
        }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.show();
}

From source file:Main.java

/**
 * Move the cursor to the end of ScrollPane.
 *
 * @param pane the ScrollPane//  ww w .j  ava2s.co m
 */
public static void moveEnd(JScrollPane pane) {
    JScrollBar bar = pane.getVerticalScrollBar();
    bar.setValue(bar.getMaximum());
}

From source file:Main.java

/**
 * Scrolls scroll pane to the top left corner.
 *//*ww  w.  j  a  v  a2 s  . c om*/
public static void scrollToTop(final JScrollPane scrollPane) {
    JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
    JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
    verticalScrollBar.setValue(verticalScrollBar.getMinimum());
    horizontalScrollBar.setValue(horizontalScrollBar.getMinimum());
}

From source file:Main.java

/**
 * Scrolls a JScrollPane to a position in the JTextArea.
 * @param jScrollPane//from   w w  w.j a  v  a 2  s .  co m
 * @param jTextArea
 * @param position
 */
public static void scrollToPosition(JScrollPane jScrollPane, JTextArea jTextArea, int position) {
    jScrollPane.getVerticalScrollBar().setValue((getLineNumber(jTextArea, position) - 3) * 18);
}