2

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...)

http://tinypic.com/r/sz94pf/6

Community
  • 1
  • 1
Valentino Ru
  • 4,964
  • 12
  • 43
  • 78
  • 1
    Can you post an actual question? I don't see one in the original post above, just descriptive stuff. Also, I've added a "java" tag to your question. – Hovercraft Full Of Eels May 05 '12 at 20:35
  • Judging from the picture, the scroll container isn't updated when you scroll, and you get those lines stretching from the previous content. You probably just need to do a `SwingUtilities.updateComponentTreeUI(scroll1)` at the end of scrolling so it updates properly. – Ozzy May 05 '12 at 21:25

1 Answers1

2

You made the background of the list transparent;

list1.setBackground(new Color(0,0,0,0));

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.

Bartvbl
  • 2,878
  • 3
  • 28
  • 45
  • Thank you. In fact, I want to have the entire JList transparent, because I want to see the background of the JPanel, on which the Jlist is added (in this test case, the JPanel panel). So you are saying that I should create a custom "myCellRenderer" implements ListCellRenderer? Is there no easier way to just set the Background of the JList transparent? – Valentino Ru May 07 '12 at 16:29
  • @ValentinoRu No. There is not. It's not exactly a common thing to make the background of a component transparent. I did some experimentation, and a custom list cell renderer does actually not work either. The only thing that I can now think of is to make your own implementation of a JList-like component, where you set the background of each list item (JPanel) to a transparent alpha. – Bartvbl May 07 '12 at 18:20
  • @ValentinoRu In case it matters; after you add the list to the scrollpane, call setOpaque(false) on the JList, make the background of both the JList and the JList.getParent() transparent, and make a cell renderer that sets the background of the component to transparent before returning actually makes the list transparent, but will have messed up selection colours. I don't want to spend more time on it myself, but that may actually be a lead to actually getting it working without having to do too much 'hacky' stuff. – Bartvbl May 07 '12 at 18:29