Skip to main content

Non-rectangular image JList (housed in a JViewport) paint problem

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2011-03-11
Points: 0

I want to create a non-rectangular scrollable JList containing images. Needless to say, I am implememting my own ListCellRenderer and returning a JLabel containing ImageIcon from it. I don't want to put the JList inside JScrollPane because I want precise control over the scrolling behaviour and not "discrete" scrolling implemented by JScrollPane. So, I house the JList within a JViewport. I extend the JViewport and override its paintChildern() method - in it, I use .setClip() to have clip on Graphics with desired shape (non-rectangular). When I display the frame, I get non-rectangular JList. So far so good.

But when I click any list cell that is partially visible with mouse, the cell gets displayed in its entirety in rectangular form - thus spoiling the non-rectangular display. It is completely disregarding the clip set on its JList. Same thing happens when I navigate list cells with up/down arrow keys. It seems getListCellRendererComponent() completely disregards the JList clip for which it is being called for. Upon further investigation, I found out that when selected cell is changing, the paintChildren() of JList is not at all getting called, thus disregarding the clip. Please note that I am using JList in SINGLE_SELECTION.

How should I fix this?