Color of visited node in JTree

I have created a JTree. I know how to change color, icons or background when I click one of the nodes. But the color goes back to the default if I click another node. What I would like to know is how I can keep the color of the node that I have visited (cliked)? For expamle, on a web page, visited links are different color and they don't chagne. Is there any way like this?
Thanks!

You can use a TreeCellRenderer to specify the colour or icon or background of a node based on various criteria. See the tutorial on How to Use Trees at http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html for more information about that. If you want the colour to depend on whether you have ever selected the node, then the object you store in the node must be capable of knowing this information. In other words, it must have a setVisited() method, which you call each time the node is selected, and a getVisited() method which you call in your renderer to decide what colour it should be.

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);

  • 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

  • Help needed with finding node in JTree

    Hi,
    In my application i will be entering some string data in a text box for search criteria.
    When I click on search, I will get data from database which will be shown as a JTree.
    This Jtree will have some nodes in which, node with string entered in the search text box exists.
    I need to focus on this node and I have only a string to find this node.
    How can I do this?How can I find a node in JTree using string value?
    Is there any alternate option by which I can achive my need?
    Please suggest.
    Thanks.

    @OP: please assign your dukes.
    @Andre_Uhres: if you don't get rewarded in the next couple of days, please let me know here to get at least one duke for your effort.
    Bye.

  • Problem inserting new node into JTree with depthFirstEnumeration()

    Hello,
    I'm currently trying to use depthFirstEnumeration() to add and delete nodes for a simple JTree. I've written a method to handle this, but it keeps coming back with an exception saying that 'new child is an ancestor'.
    All I'm trying to do is add and delete nodes from a JTree using add and remove buttons within a swing program. When a user adds the new node the JTree needs to be updated with new labels in sequential order dependent upon a depthFirst traversal of the JTree.
    My current code for the add button is as follows,
    public void actionPerformed(ActionEvent event) 
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            Enumeration e = rootNode.depthFirstEnumeration();
            if(event.getSource().equals(addButton))
                if (selectedNode != null)
                    while (e.hasMoreElements()) {
                    // add the new node as a child of a selected node at the end
                    DefaultMutableTreeNode newNode = (DefaultMutableTreeNode)e.nextElement();
                     // treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
                    //System.out.print(newNode.getUserObject() + "" + newNodeSuffix++);
                    String label = "Node"+i;
                    treeModel.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
                    i++;
                      //make the node visible by scrolling to it
                    TreeNode[] totalNodes = treeModel.getPathToRoot(newNode);
                    TreePath path = new TreePath(totalNodes);
                    tree.scrollPathToVisible(path);
                    //System.out.println();
            else if(event.getSource().equals(deleteButton))
                //remove the selected node, except the parent node
                removeSelectedNode();           
        } As you can see in the above code, I've tested the incrementing of the new nodes in a System.out.print() statement and they appear to be incrementing correctly. I just can't see how to correctly output the labels for the nodes.
    Any help and advice always appreciated.

    This is about the 5th posting on the same question. Here is the link to the last posting so you can see what has already been suggested so you don't waste your time making the same suggestion.
    http://forum.java.sun.com/thread.jspa?threadID=704980
    The OP doesn't seem to understand that nodes don't just rename themselves and that renderers will only display the text of the current node. If you want the text displayed to changed, then you need to change the text associated with each node, which means iterating through the entire tree to update each node with new text. Then again, maybe I don't understand the question, but the OP keeps posting the same question without any additional information.

  • Changing Safari Font Color for Visited Links

    Is there a utility or some other way to change the color of visited links in Safari? The current blue/purple colors are very difficult to distinguish and I would like a more dramatic color change for visited links. Thank you for any help.

    cadcard --
    Welcome!
    Here's a link to a thread where this was discussed before.
    Hope it is helpful:
    http://discussions.apple.com/thread.jspa?messageID=5204995&#5204995

  • Drag and Drop nodes in JTree

    Is there a way to drap and drop JTree nodes without resorting to hacks? http://www.javaworld.com/javatips/jw-javatip97.html outlines a hack to get drag and drop working but it is kinda old. I am wondering whether there is a now a way to do this in Swing. http://weblogs.java.net/blog/shan_man/archive/2006/01/first_class_dra.html outlines a seemingly promising way but when I tried it it still wouldn't let me drag JTree nodes. (btw, if you are going to try it, you must replace TransferHandler.TransferInfo with TransferHandler.TransferSupport. It has apprently been renamed.)

    I have implemented drag and drop of nodes in JTree they same way it is explained in JavaWorld link that you mentioned, I customized it for my application and it worked great.

  • Cant change color on visited sites without changing background /foreground colors that hide options on the sitec

    I want to change the color of visited sites, but am forced to set background and foreground colors which don't properly
    show all of the colors for controls etc on many websites.
    I have to uncheck the button to Allow pages to choose their own colors....
    Google is a good example after doing a search, then change visited sites color, uncheck the allow pages button and then you can't see the search button and most of the options in the top right corner in google.
    I am a little color blind, so can't differentiate blue and purple for links on visited sites...
    thx

    I think the problem is that many sites use background colors to create buttons, so you lose that when you override the page's colors.
    Maybe you could use an add-on to fine-tune colors instead of Firefox's all-or-nothing setting. I found a couple extensions that say they can change page colors, but I haven't tried them myself:
    * [https://addons.mozilla.org/firefox/addon/color-that-site/ Color That Site!]
    * [https://addons.mozilla.org/firefox/addon/colorific-1/ Colorific]
    * [https://addons.mozilla.org/firefox/addon/toggledocumentcolors-198916/ ToggleDocumentColors_]
    * [https://addons.mozilla.org/firefox/addon/color-toggle/ Color toggle]
    For a truly custom approach to link colors, there are two general purpose ways to apply your own style rules to specific sites: the Stylish extension and a userContent.css files. You would need to set a preferred text ''and'' background color for visited and unvisited links so that you are guaranteed to be able to read the text.
    Hope one of these fits the bill!

  • Wordwrap nodes in jtree

    Anyone know how we can do wordwrap of nodes in jtree?
    Please help...
    Thanks.

    Any example on this? what html code should I use? The simplest possible, eg
    yourNode.setText("<html>" + yourNodeText + "</html>");Most JComponents can render this html string and produce a good result. But if you wanna get a little more complex and use styles and other web things, then do as said, (two posts above) and use a JEditorPane specifically for cell renderer
    ICE

  • How do I change color on visited sites without causing websites to not view properly?

    I go to Content/Colors, change color of visited sites, uncheck "allow pages to choose own colors instead of my selections above".
    The visited sites change colors, but the websites that I go to do not view properly. They do not have names or headings or shopping carts or search boxes and are very hard to maneuver through.

    You can use code in userContent.css or use the Stylish extension to override the page colors.
    I use code like this:
    <pre><nowiki>a:visited {color:#e08!important;background-color:#eee}
    a:visited * {color:#e08!important;outline:1px dotted red!important;outline-offset:-1px}
    a:visited img {background-color:transparent}
    </nowiki></pre>
    *Stylish: https://addons.mozilla.org/firefox/addon/stylish/
    *http://userstyles.org/help/stylish_firefox
    The customization files userChrome.css (user interface) and userContent.css (websites) are located in the <b>chrome</b> folder in the Firefox profile folder.
    *http://kb.mozillazine.org/Editing_configuration

  • How to set the default tree node in JTree?

    I want to set a default tree node in JTree when start the program,
    what is the method or the function to call?
    Thanks~

    If you by "default" mean "selected", then you can use one of the setSelectionXXX methods in the JTree API.
    If you by "default" mean something else, never mind this post :-)

  • Change color of visited link

    Hello,
    I would like to change the color of visited column link in my report. After user clicks on it, it opens some info region, but user wants to know what info was already accessed after this region is closed, so link should stay colored.
    I

    Here is my solution.
    On click I store the record key in page item, say P100_KEYS, and append keys (with ; separator) each time the link is clicked in any record. Then DECODE(SUBSTR(:P100_KEYS,INSTR(:P100_KEYS,REC_KEY)-1,LENGTH(REC_KEY)+2),';'||TO_CHAR(REC_KEY)||';','v','') in the report sql analyzes the keys, and check marks relevant records when page resubmitted. Check mark could be colored, but because link is also a gif image, it looks well as is.
    Thanks for your help and suggestions.

  • Color of visited links

    Hi,
    we are using self created HTML Content WPC Webpages.
    The problem is, that once you have clicked on the link the color of this link is exactly the same as the background color. We have changed no settings concerning the links or the background color.
    Anyone knows where I can set the color of visited links?
    I can not find the correct setting in the Theme editor.
    Best,
    Stephan

    Hi,
    thanks for the reply. I think the settings in the theme editor only work if the link I create has a certain css class. But my Links do not have any css class attribute.
    I need to find, where I can globaly set the a:visited setting in the portal.
    Best,
    Stephan

  • Add node to JTree

    I have an already populated JTree. At Runtime I add one or more node to JTree, using something like
    parentNode.add(newNode)....
    If I call a repaint method, the new node isn't visible, I have to reload model using
    DefaultTreeModel dtm = ((DefaultTreeModel)myTree.getModel());
    TreeNode rootNode = (TreeNode) dtm.getRoot();
    dtm.reload(rootNode);
    Unfortunately this method cause the lost of focus on the currently selected node and the collapse of all JTree around its root node; If I change an existing node (for example changing its text or icon) I must reload the model too!!!
    Does exist an other more "soft" method to change the aspect of a JTree without reload the entire model?

    You must use or implement the
        public void valueForPathChanged(javax.swing.tree.TreePath path,java.lang.Object newNode) {
            Object[]    p = path.getPath();
            Object[]    pp = p;
            //insert node here
            int[] ci = new int[] {index};
            Object[] cc = new Object[] {node};
            fireTreeNodesChanged(this, pp, ci, cc);
            fireTreeStructureChanged(this, pp, ci, cc);
        }from the TreeModel. To fire the last two is essensial.
    I implemented a GP in JAVA so i had the same problem wehn i started.

  • Color of visited web sites

    On NoSquint, I checked the box for color of visited sites and nothing happens.
    The color of un-visited sites is OK

    NoSquint:
    * https://addons.mozilla.org/firefox/addon/nosquint/
    *https://urandom.ca/nosquint/
    Make sure that you do not run Firefox in permanent Private Browsing mode.
    *https://support.mozilla.com/kb/Private+Browsing
    *You enter Private Browsing mode if you select: Firefox > Preferences > Privacy > History: Firefox will: "Never Remember History"
    *To see all History and Cookie settings, choose: Firefox > Preferences > Privacy, choose the setting <b>Firefox will: Use custom settings for history</b>
    * Deselect: [ ] "Permanent Private Browsing mode"

Maybe you are looking for

  • Restore an i phone too many times

    what happens when you restore an iPhone too many times

  • Mail Issues

    OK, so after nearly three years of being a happy Mac owner and having little or no issues with hardware or software, I'm freaking out because Mail is letting me down. Beginning yesterday (3.25.09), Mail began quitting unexpectedly after I emailed a "

  • Problem with relative positioning

    Why would this work: x="{this.width/2}" but not x="{this.width-2}" or x="{this.width}" Makes no sense to me. I get the number if I trace it.

  • ProColor RGB or Adobe RGB ?

    I have always run Adobe RGB on PS (now CS3)... have it "automatically convert". *When I go to PS it is normally because the Output will be to a Printer in the End (and also a web version) ... (also have my camera set to Adobe RGB, "although" I always

  • Help plzzzzzzzz​zzzzzzzz

    Hi I have pearl 8120 and I downloaded BlackBerry Desktop Software (v 6.1.0.35) and when I connected my phone it says new software available so I upgraded it it's 4.5 Bundle 174(v4.5.124,Platform 2.7.092) and I hate it soooooooooo much I want the old