Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2006

Tip: Looking for answers? Try searching our database.

Displaying background color for treenodes of JTree

Thread view: 
chandu - 09 Jun 2006 20:43 GMT
Hi,
 As part of tree, i wanted to display a alternative background row
color for each node of JTree.
Could you please let me know how can i do it. Please send me some
example programs.

Thanks.
steve - 09 Jun 2006 22:45 GMT
> Hi,
>   As part of tree, i wanted to display a alternative background row
[quoted text clipped - 3 lines]
>
> Thanks.

you need to override :

CustomCellRenderer extends JLabel implements TreeCellRenderer

then inside the  paint

   public void paint(Graphics g) {
       Color bColor;

   

       // Set the correct background color
       bColor = bSelected
                ? SystemColor.LIGHT_GRAY
                : otree.getBackground();    // ensure background is same

       g.setColor(bColor);
       this.setOpaque(false);
       this.setBackground(null);

       // Draw a rectangle in the background of the cell
       g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
       super.paint(g);
   }

but you better make sure the colours you choose work on all platforms
I know for a fact that some "blues" work on windows & osx, but under  linux
kde are drawn as solid black, and hide the text.

using the above and a little imagination will solve both your questions in
your  posts.
chandu - 11 Jun 2006 01:28 GMT
> > Hi,
> >   As part of tree, i wanted to display a alternative background row
[quoted text clipped - 33 lines]
> using the above and a little imagination will solve both your questions in
> your  posts

I tried this solution. But i am not getting the alternative colors for
full rows.

Thanks
steve - 11 Jun 2006 22:58 GMT
>>> Hi,
>>> As part of tree, i wanted to display a alternative background row
[quoted text clipped - 38 lines]
>
> Thanks

that is because it is code to show you how to set the colour , it is NOT code
to  completely do the job for you. you need extra SIMPLE logic ,  to change
the colours.
I.E you need to write about 5 more lines of code yourself.

Steve
chandu - 12 Jun 2006 16:37 GMT
I have added couple of logic to it. Still i am not getting full row.

public void paint(Graphics g) {
        if(rowNo%2 == 0)
       {
           g.setColor(Color.gray);
       }
       else
       {
           g.setColor(Color.white);
       }
       this.setOpaque(false);
       this.setBackground(null);

      Rectangle r = tree.getBounds();
      System.out.println("X value  "+r.x);

        //Draw a rectangle in the background of the cell
       g.fillRect(r.x, 0, 400, getHeight()-1);

       super.paint(g);
   }

> >>> Hi,
> >>> As part of tree, i wanted to display a alternative background row
[quoted text clipped - 45 lines]
>
> Steve
steve - 12 Jun 2006 22:40 GMT
> I have added couple of logic to it. Still i am not getting full row.
>
[quoted text clipped - 70 lines]
>>
>> Steve

what do you mean not getting a full row?
this is a jtree, not a jtable
also where did you set rowNo, and how did you calculate it?
The only way I can see this working is some sort of complex logic, starting
at the root.

I can assure you that the paint method IS called for each node, therefore it
is just a matter of you logic to flip the colours.

but i still do not understand why you are trying to apply a method ,that is
normally used for JTables to a JTree
steve - 12 Jun 2006 22:47 GMT
> I have added couple of logic to it. Still i am not getting full row.
>
[quoted text clipped - 70 lines]
>>
>> Steve

Ahh , when you say the full row, you mean including the ICON!!

Well i can tell you that it will get a lot more complex, if you want to try
and repaint the icons background on the fly.
basically the above code will only paint the label of the node.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.