Change side of a jScrollPane & change size

2019-06-19 18:46发布

问题:

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!

回答1:

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);
    }
}



回答2:

  1. Use a panel with a BorderLayout
  2. add the scrollpane to the Center
  3. add the vertical scrollbar to the West

If you want to change the width of all the scrollbars then you can try using the UIManager. See UIManager Defaults