Change side of a jScrollPane & change size

Christian 'fuzi' Orgler picture Christian 'fuzi' Orgler · Dec 17, 2010 · Viewed 9.4k times · Source

I want to change the side of my JScrollPane from the right (default) to the left side. How do I do this?

AND: I want to change the size of JScrollBar because it's a touch-display (22") so that it can be scrolled easily.

Thanks a lot!

Answer

aioobe picture aioobe · Dec 17, 2010

Following @camickr suggestion for the placement, here is a complete example

import java.awt.*;
import javax.swing.*;
public class Test {
    public static void main(String[] args) {
        JFrame jf = new JFrame("Scorllbar test");
        JPanel jp = new JPanel() {{ add(new JComponent() {
                { setPreferredSize(new Dimension(450, 450)); }
                public void paintComponent(Graphics g) {
                    g.drawLine(0, 0, getWidth(), getHeight());
                    g.drawLine(getWidth(), 0, 0, getHeight());
                }});}};

        JScrollPane sp = new JScrollPane(jp,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        // Get the scroll-bar and make it a bit wider.
        JScrollBar sb = sp.getVerticalScrollBar();
        sb.setPreferredSize(new Dimension(50, 0));

        // Put it to the left.
        sp.remove(sb);
        JPanel tmp = new JPanel(new BorderLayout());
        tmp.add(sp, BorderLayout.CENTER);
        tmp.add(sb, BorderLayout.WEST);

        jf.add(tmp);
        jf.setSize(300, 300);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }
}

enter image description here