This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Transparent JTree over gradient background Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Transparent JTree over gradient background" Watch "Transparent JTree over gradient background" New topic
Author

Transparent JTree over gradient background

bebbo duce
Greenhorn

Joined: Nov 14, 2009
Posts: 8
Greetings everyone,
I'm having a lot of trouble with transparent JTrees,
I've extracted the portion of code that gives problems in a simple executable class:



The real piece of code inside my application is a little (a lot) more complicated, but i found out that a simple class like this one above presents similar problems to mine.
If you play a little with the tree (open and close various nodes several times) you will notice that the nodes tend to overlap one with another, giving an awful visual effect.
Please notice that this does not depend on the GradientPane that contains the tree, because it does the same with a normal JPane.
However, i found a workaround in the case of a non-gradient background, but that's not the thing i'm interested in.
The problem gets even worse if you put the GradientPane into a scrollPane and/or if you use a custom cellRenderer, you will get a real mess as you move around the scroll bars.
It's really important to me to have a gradient background under that tree, but it seems that my experience with swing is really not enough.

P.S.
If that could matter, i'm running on windows.
I don't know if the problem can be reproduced on other systems.
Thanks to everyone kind enough to help.
bebbo duce
Greenhorn

Joined: Nov 14, 2009
Posts: 8
(I'm very sorry about posting again but I cant' find a way to edit the previous post)

I found a dirty way to partially solve the issue, it seems to work ok if you add super.updateUI() at line 54.
The problem is, this solution doesn't work anymore if the tree is contained inside a scroll panel like this:



Here's a list of the graphical issues this piece of code has:
1) sometimes, expanding and/or collapsing the nodes does not properly repaint the tree;
2) when the horizzontal scroll bar appears, the tree seems to lose it's transparent background in favour of a default grey one (this is courious beacuse in another piece of code, very similar to this one, this strange behaviour belongs to the vertical scroll bar instead);
3) scrolling the panel leaves awful after-images of the tree;
I am pretty sure that all of these have a common solution...

I apologize again for bothering, not being able to update the other post.
I'll be grateful to everyone able to help me.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Add this:



(The key is line 17)

And then tree.setCellRenderer(new MyRenderer());


GenRocket - Experts at Building Test Data
bebbo duce
Greenhorn

Joined: Nov 14, 2009
Posts: 8
Thank you for your quick reply, however setting a custom renderer fails to solve my issues.
Transparent labels on the tree are very cool, but with them the problem gets even worse!
If you have time to spare, try and run that code i posted so you can understand what I mean.

Thanks anyway for your patience.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Yea, I saw the problem after playing with it a bit more. I don't know the exact cause of the issue, but the problem is that when the scrollpane decides to show up the panel doesn't realize it needs to repaint itself. So you see the distortion in the panel with all the lines and double tree nodes, etc. The fix for this is to simply call repaint() at the end of your paintComponent method. I'm posting my code here because even though your code was just a sample, there were a few little things that I think you can skip over. For example, the way you were adding the scrollpane to the gradient then the gradient to the content pane, etc. Just more than needed to be done. Anyway, here's code that works the way you want.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

BTW, repaint() is a better choice than updateUI, in case you were wondering. updateUI should only be used when changing the L&F.
bebbo duce
Greenhorn

Joined: Nov 14, 2009
Posts: 8
That worked perfectly.

Thank you very much, now my tree looks fine.
bebbo duce
Greenhorn

Joined: Nov 14, 2009
Posts: 8
Sorry to bother again, but your solution has a little problem...
Calling repaint() inside paintComponent() causes an infinite loop, that causes the continuos rapaint of the tree.
You can notice that when your example is running the cpu usage goes over 70-80%.

Any ideas on how to call repaint only once?
Thanks in advance.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

bebbo duce wrote:Sorry to bother again, but your solution has a little problem...
Calling repaint() inside paintComponent() causes an infinite loop, that causes the continuos rapaint of the tree.
You can notice that when your example is running the cpu usage goes over 70-80%.

Any ideas on how to call repaint only once?
Thanks in advance.



:rolling: Yea, that makes sense since repaint() would inevitably call paintComponent. Hm, back to the drawing board. I'm afraid I'm at a loss about this one.
bebbo duce
Greenhorn

Joined: Nov 14, 2009
Posts: 8
I resolved adding an AdjustmentListener to the scroll pane and a TreeSelectionListener to the tree, both of whom call a repaint of the gradient pane.
Hope this will be useful to someone.
 
GeeCON Prague 2014
 
subject: Transparent JTree over gradient background