Change side of a jScrollPane & change size

丶灬走出姿态 提交于 2019-12-10 13:25:59

问题


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



来源:https://stackoverflow.com/questions/4474706/change-side-of-a-jscrollpane-change-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!