To keep order in my other question it was recommended to ask the following seperately:
The following is the code of the test-class, in which I add my custom JScrollBar
public class TestScrollBar {
private static JFrame f;
private static Container pane;
private static JScrollPane scroll1;
private static JScrollBar scrollbar;
private static JPanel panel;
private static JList<String> list1;
public static void main(String[] arg){
createBasic();
createComponents();
f.setVisible(true);
}
private static void createBasic(){
f = new JFrame("ScrollbarTest");
f.setBounds(100,100,300,300);
pane = f.getContentPane();
pane.setLayout(null);
panel = new JPanel();
panel.setBackground(Color.GREEN);
panel.setLayout(null);
panel.setBounds(50,50,200,150);
}
private static void createComponents(){
String[] data = {"ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg", "ggggg"};
list1 = new JList<String>(data);
list1.setBackground(new Color(0,0,0,0));
scrollbar = new JScrollBar();
CustomScrollBarUI ui = new CustomScrollBarUI();
scrollbar.setUI(ui);
scrollbar.setOpaque(false);
scroll1 = new JScrollPane(list1);
scroll1.setBounds(20,20,160,110);
scroll1.setOpaque(false);
scroll1.getViewport().setOpaque(false);
scroll1.setVerticalScrollBar(scrollbar);
panel.add(scroll1);
pane.add(panel);
}
}
The custom ScrollBarUI can be seen here: Custom JScrollBar-Thumb is painted, but doesn't move
The only thing I changed (thanks to mgarin) is
g.drawImage(img, thumbBounds.x, thumbBounds.y, new Color(255,255,255,0), null);
And the following happens, if I move the thumb (Please don't mind about the design, it is just to test some Opaque stuff...)
You made the background of the list transparent;
If you remove that line it draws fine.
On another note: if you want to give your cells a custom background colour, try using a custom ListCellRenderer, using the setCellRenderer() method on the JList. You can then set the background colour of the component you return.