Im am trying to customize a jScrollPane using the code below. It works, It changes the color the way I want it, but hides the arrowbuttons.
What I want is to make them visible again and change them with a custom image. I tried searching on this forum, but I couldnt find any info about it.
I hope someone can help me. Thanks in advance!
private Image imageThumb, imageTrack;
private JButton b = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(0, 0);
}
};
public YourScrollbarUI () {
imageThumb = WrapImage .create(45, 45, new Color(46,218,163));
imageTrack = WrapImage .create(32, 32, new Color(90,90,90));
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
g.setColor(Color.blue);
((Graphics2D) g).drawImage(imageThumb,
r.x, r.y, r.width, r.height, null);
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
((Graphics2D) g).drawImage(imageTrack,
r.x, r.y, r.width, r.height, null);
}
@Override
protected JButton createDecreaseButton(int orientation) {
return b;
}
@Override
protected JButton createIncreaseButton(int orientation) {
return b;
}
private static class WrapImage {
static public Image create(int w, int h, Color c) {
BufferedImage bi = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setPaint(c);
g2d.fillRect(0, 0, w, h);
g2d.dispose();
return bi;}}
Here is the problem:
In your code
b
button is the responsible for painting the arrows throughcreateDecreaseButton
andcreateIncreaseButton
methods. If its preferred size is(0,0)
then logically it won't be visible.You need to modify
createDecreaseButton
andcreateIncreaseButton
to make them return a newJButton
with the desired icon.Update
Look at this working example.
MyScrollbarUI
extends from BasicScrollBarUI just as your class does. You'll see the key is overrding the button'sgetPreferredSize()
method and setting the appropriate icon as needed.In this regard I should say BasicScrollBarUI.createDecreaseButton(int orientation) and BasicScrollBarUI.createIncreaseButton(int orientation) methods are poorly documented (there's no javadoc). But if you dive into this class using an IDE then you'll see
orientation
paramenter can take one of these values:SwingConstants.NORTH, SwingConstants.SOUTH, SwingConstants.EAST, SwingConstants.WEST
. Keep this in mind when look atgetAppropriateIcon(int orientation)
method.These are used icons:
Screenshot