JScrollPane scrollbar color

Can we change the JScrollPane scrollbar color?How?I dont want to change look n feel...just want to change scroller color...
[144 byte] By [Kuntilanaka] at [2007-11-27 4:33:06]
# 1
You could set the UIDefault for ScrollBar.foreground, but that changes all scrollbars in your application.
PhHeina at 2007-7-12 9:42:56 > top of Java-index,Desktop,Core GUI APIs...
# 2

You could try

class MyScrollBarUI extends BasicScrollBarUI {

// this draws scroller

protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {

g.setColor(Color.BLUE);

g.fillRect((int)trackBounds.getX(),(int)trackBounds.getY(),

(int)trackBounds.getWidth(),(int)trackBounds.getHeight());

}

// this draws scroller background

protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {

g.setColor(Color.RED);

g.fillRect((int)trackBounds.getX(),(int)trackBounds.getY(),

(int)trackBounds.getWidth(),(int)trackBounds.getHeight());

}

// and methods creating scrollbar buttons

protected JButton createDecreaseButton(int orientation) {

JButton button = super.createDecreaseButton(orientation);

button.setBackground(Color.BLACK);

button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));

return button;

}

protected JButton createIncreaseButton(int orientation) {

JButton button = super.createIncreaseButton(orientation);

button.setBackground(Color.BLACK);

button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));

return button;

}

}

...and when you create JScrollPane, to modify let's say vertical scrollbar, simply call --

scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());

Jimzya at 2007-7-12 9:42:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thank U...but i got this error while compiling it

cannot find symbol

symbol: class BasicScrollBarUI

class MyScrollBarUI extends BasicScrollBarUI {

i already import javax.swing..but still cannot find symbol...why?

Kuntilanaka at 2007-7-12 9:42:56 > top of Java-index,Desktop,Core GUI APIs...
# 4
> i already import javax.swing..but still cannot find symbol...why? Because its not in that package.Learn how to read the API to find the package it belongs to.
camickra at 2007-7-12 9:42:56 > top of Java-index,Desktop,Core GUI APIs...
# 5

ok..i its solved....they need

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Rectangle;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.plaf.basic.BasicScrollBarUI;

then still got problem with

cannot find symbol

symbol : variable trackBounds

location: class MyScrollBarUI

g.fillRect((int)trackBounds.getX(),(int)trackBounds.getY(),

then i declare variable trackBounds

Rectangle trackBounds = null;

then its successful compiled

but when i call this class to setUI i got this error...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at MyScrollBarUI.paintThumb(MyScrollBarUI.java:15)

at javax.swing.plaf.basic.BasicScrollBarUI.paint(BasicScrollBarUI.java:3

66)

please give me some clue....is it because rectangle was null?

Kuntilanaka at 2007-7-12 9:42:56 > top of Java-index,Desktop,Core GUI APIs...
# 6
sorry it is because rectangle was set to null, just set rectangle like this...Rectangle trackBounds = new Rectangle(10,10,10,10);subject closed, thanks to all...=)
Kuntilanaka at 2007-7-12 9:42:56 > top of Java-index,Desktop,Core GUI APIs...