Set scrollbar thickness

2019-04-05 15:11发布

问题:

Is there a way to adjust the thickness of the scrollbar within a JScrollPane? the default is kind of clunky.

回答1:

A quick but also dirty solution is to set the width/height explicitely to e.g. 10 pixels via

jScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
jScrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 10));

Other way would be to provide a proper ScrollBarUI.



回答2:

int verticalScrollBarWidthCoefficient = 3;

scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(
        (int) scrollPane.getVerticalScrollBar().getPreferredSize()
                .getWidth() * verticalScrollBarWidthCoefficient,
        (int) scrollPane.getVerticalScrollBar().getPreferredSize().getHeight()
));


回答3:

The following applies to the default LNF (Metal).

If you just want to set a specific size, then you can put this code before you create the JScrollPane (replace 40 with whatever size you prefer)...

UIManager.put("ScrollBar.width", 40);

If you want to scale the scrollbar based on the default size then something like this (before you create the JScrollPane)...

UIManager.put("ScrollBar.width", (int) ((int) UIManager.get("ScrollBar.width") * 2.5));

If you want to change it after creating the JScrollPane things are a bit more complex, but not too bad...

int scrollbarSize = <some dynamic value>;
UIManager.put("ScrollBar.width", scrollbarSize);
scrollPane.setVerticalScrollBar(scrollPane.createVerticalScrollBar());
scrollPane.setHorizontalScrollBar(scrollPane.createHorizontalScrollBar());

I hope that helps someone.



回答4:

simplest way for me was to edit the jquery.jscrollpane.css file directly. changing the width of .jspVerticalBar. :)