• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Transparent JTree over gradient background

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add this:



(The key is line 17)

And then tree.setCellRenderer(new MyRenderer());
 
bebbo duce
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That worked perfectly.

Thank you very much, now my tree looks fine.
 
bebbo duce
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic