Setting the color of a node in JTree

Hi all!
I'm new to Java and facing a problem with JTrees.
I'm using the following snippet to create a JTree :
DeafultMutableTreeNode root = new DefaultMutableTeeNode("Letters");
DeafultMutableTreeNode parent1 = new DefaultMutableTeeNode("uppercase");
DeafultMutableTreeNode parent2 = new DefaultMutableTeeNode("lowercase");
DeafultMutableTreeNode child1 = new DefaultMutableTeeNode("A");
DeafultMutableTreeNode child2 = new DefaultMutableTeeNode("B");
DeafultMutableTreeNode child3 = new DefaultMutableTeeNode("C");
DeafultMutableTreeNode child4 = new DefaultMutableTeeNode("a");
DeafultMutableTreeNode child5 = new DefaultMutableTeeNode("b");
DeafultMutableTreeNode child6 = new DefaultMutableTeeNode("c");
root.add(parent1);
root.add(parent2);
parent1.add(child1);
parent1.add(child2);
parent1.add(child3);
parent2.add(child4);
parent2.add(child5);
parent2.add(child6);
JTree tree = new JTree(root);
JScrollPane pane = new JScrollPane(tree);
TreePath path = tree.getNextMatch("lowercase", 0, javax.swing.text.Position.Bias.Forward);
tree.setSelectionPath(path);
tree.expandPath(path);
In the above JTree, the node labeled "lowercase" gets selected and expanded as soon as the program is run.
What i want to know is that, is there any way in which i can set only the "lowercase" node's background to a different color (say, YELLOW) and have it so, no matter if the node is expanded or collapsed or other nodes are expanded or collapsed.
ps: Sample snippet would really b helpful.

You need to write your own renderer for this.
Read the tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display

Similar Messages

  • Setting custom color for selected node in JTree?

    Hi,
    i want to set my own color for selected node in JTree. i don't want to set for all the nodes...how to set Color for Selected node using TreeCellRender class?
    Thanks
    Mani

    I assume you are not setting a custom tree cell renderer...
    javax.swing.JTree theTree = ...;
    java.awt.Color newBGColor = ...;
    ((javax.swing.tree.DefaultTreeCellRenderer)theTree.getCellRenderer())
        .setBackgroundSelectionColor(newColor);

  • Dynamically setting the color of the title in a Panel

    Hello, I know I can set the color & font of the title in a Panel using the titleStyleName in a stylesheet.  How can I dynamically change the color though?  I have a component:
             <mx:Panel id="myPanel"borderColor="{this.color1}" />
    where this.color1 is a value that changes dynamically, and thus changes the border color.  How do I do this for the color of the text in the title?

    Hi,
    Try this
    var css:CSSStyleDeclaration = StyleManager.getStyleDeclaration(".myPanelTitle");
    css.setStyle("color",0x00FF00);

  • How to set the color to the particular row in a report

    Hi
    I have a report in which I have an edit link(I changed the id as edit link)..When I click that link, all the details(the whole row details) will be appearing on the items nearer to the report to save the changes....
    I want to set the color for the particular row when a link is clicked and the color should not be changed until I save the changes in items...After saving the changes, the report should come in the normal color...this is my requirement..i refered many forums regarding this...But could not find solution....If anyone knows,please help me...
    Fazila

    Here is how I would implement
    Create renderer
    Add SkinnableContainer element and create a skin for this container namely SkinnableContainerSkin
    find the fill section in the skin
    add bitmap fill to the fill section and give the source of the image which you are planning to use(Hopefully this is a direct way of doing, may be you need to hack this a little to get it to work)
    I've never tried this, but should help you give some idea.

  • How to set the color scale so that we can get same color for same intensity of color in various runs

    Hi
    I am trying to get the color pattern of mashed potato trays and diferent trays has different color intensity but if IF i am chsooing rainbow plateu it's giveing red color for dark region of each tray but two tray has different intensity of dark color. Can you suugest me how should I set the color scale so that I can distinguish which tray has higher degree of intensity of darkness.
    I will be obliged to get the suggestions

    Hello,
    Thank you for contacting National Instruments. I just have a few questions for you to help us clarify your application:
    What API, such as LabVIEW or C, are you programming your application in?
    Are you using Vision, and if so what specific functions are you currently using?
    Is the lighting consistent and just the color of the trays changing?
    How are you displaying the image in your program? Are you using an Image Display control?/li>
    Depending on what functions you have available, then we may be able to work on finding a way to distinguish between the different intensities. If you could provide a little information to your application based on these questions then will be able to better understand and help you with your app
    lication.
    Regards,
    Michael
    Applications Engineer
    National Instruments

  • Link Color not changing for Visited Pages - How do I set the Color?

    In earlier versions of Safari, the user could select the color used to display links that have already been visited. I can't find that option in Preferences anymore. Some visited links do show a different color, some don't. Is it just random? How do I set the color in Safari 4.0.4 and activate the behavior?

    HI,
    I think you change visited pages link colors using Google preferences.
    http://www.googleguide.com/preferences.html
    Carolyn

  • Setting the color (or background color) of a substring

    Is it possible to set the color of a substring. For example:  If I had the string "Steve is stuck and needs help" and I wanted to change it to become "Steve is stuck and needs help".
    At present I am trying to modify the text inside of a String Indicator, but am open to suggestions.
    I have been reading the LV help and it seems to suggest that you can do this using the Text.SelStart and Text.SelEnd properties, but I can't get over that last hurdle that actually updates the string.
    Regards
    Steve
    https://decibel.ni.com/content/blogs/DailyCLAD
    Solved!
    Go to Solution.

    So close.
    Thanks All
    https://decibel.ni.com/content/blogs/DailyCLAD

  • Can you set the color of a specific element in a JList?

    Is it possible to set the color of a single element in a JList without writing lines and lines of your own class? I know setForgroundColor exists, it just changes all element in a JList, so is there a simple way to tweak it? i am still a newbie, so re-writing swing, would be a bit of a problem for me. Any Suggestions? thanks.

    you write your own renderer, about 6-8 lines, setting the color determined by an
    if/else statement - note: you must set the color in both if and else
    search the forums for
    "implements ListCellRenderer"
    and you should find plenty of examples
    if you just want the selected element a different color, you can do this with
    UIManager.put("List.selectionBackground", new javax.swing.plaf.ColorUIResource(Color.YELLOW));
    but you must do this before you create the JList, and it will affect all JLists in your program.

  • Can u tell me how to set the color of the button and its border

    hi to all,can u tell me how to set the color of the button and its border

    There are sample code on the java.sun.com
    http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/index.html
    try this web page ... There are lots of examples... Good luck ... "Gan en "

  • Setting the color of a disabled checkbox...

    Hi,
    how do I set the color of a (disabled) JCheckBox because the following code does not work for me:
    jCheckBox1.setForeground(Color.BLACK);The background is that I don't want to have the checkbox in this grey (disabled) style, instead it should look like a "normal" checkbox (but disabled).
    Any ideas?
    Best regards
    - Stephan

    WindowsLookAndFeel uses native APIs to draw most of the controls. So, a disabled checkbox under Windows LAF will use the same API as the native disabled checkbox. You can install a custom JXLayer on that checkbox to prevent all the events from being passed to it - not the cleanest solution, but it will get you there (and make your users annoyed that they can't click on that checkbox).

  • Setting the color on selected Tab

    Hi,
    I can't seem to figure out how to set the color on the
    selected tab in a JTabbedPane. I've added a
    ChangeListener but when I try to use it, it only
    changes the color after the tab has been selected.
    I want to change it while it is selected.
    How can I do this?

    Maybe this helps:
    http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
    Kurta

  • Setting the color of 3D EM Structures

    i have no issues setting the color i like for the 2D layer drawings. 
    and they are also correct on the 3D of the schematic layout
    however my problem is in setting the color in 3D layer for EM structures. They seem to be different.
    I tried setting the color of my material definitions (topmetal) under the material definitions tab of the STACKUP.
    but the color does not correctly translate in the 3D EM structure.
    please advise.

    i got it correctly setting the color of the 2D and 3D for EM structures by following your suggestion in setting the COLOR KEY. that's new to me. thanks
    but my major problem is still unsolved where the color settings of my 2D/3D schematic layouts is different from 2D/3D EM structure layout. I always set the color in the LPF file. for my case for example, my topmetal is defined in lpf as RED. but then when i draw shapes in 2D EM structure, the color for this topmetal layer becomes BLUE...quite lost on this because there's only 1 way to set the color in LPF under "General, Drawing layer 2d"...

  • Setting the color of the window title in Forms 6i

    How does one control or set the color of the title of the MDI and module windows
    gordon
    [email protected]

    Gordon, I'm pretty sure this is actually outside the realms of Forms since the Window Manager is controlled by the O/S and so the only way you can set this is via the PC desktop (there is nothing exposed in Set_Window_Property that can do this).
    Hope this helps.
    Regards
    Grant Ronald
    Forms Product Management

  • Setting the color of individual pixels in a bufferedimage

    i am trying to set the color of each pixel in a loop as follows:-
    BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE-INT_RGB);
    for(int j=0;j<h;j++){
    for(int i=0;i<w;i++){
    pixel=translate(i,j);
    clr=findcolor(pixel);
    if(clr==-1){
    bi.setRGB(i,j,0);
    else
    bi.setRGB(i,j,clr);
    findcolor() function just returns an int, so how do take this integer value and set the appropriate color?
    when i execute this code,in the PaintComponent method of a JPanel class, my entire panel becomes black, this is not because of my first "if" condition.i checked the return value of my findcolor function.
    However if i multiply the color value by a large number like 10000, then i get a few colors only(black,blue, green)
    So please let me know how i can get all colors, without having to multiply the value.

    What do the translate and findcolor methods do?

  • How can I set the color for a textfield

    Hi all,
    I use a textfield and I want to set the color for characters which are typed in the textfield. I didn't find anywhere a solution for this. Do you know how I should implement this?
    Thanks

    Not possible. If you want this, you'll need to make a custonItem that implements this.

Maybe you are looking for