change icon in jtree

Discussion in 'Java' started by Bumsys@gmail.com, Feb 18, 2008.

  1. Guest

    private ActionListener changeFileIconLoadAction = new ActionListener()
    {
    public void actionPerformed(final ActionEvent e) {
    getNameValueNode(true);
    }
    };

    private TreeCellRenderer filesLoadTreeCellRenderer = new
    DefaultTreeCellRenderer() {

    public Component getTreeCellRendererComponent(final JTree
    tree,
    final Object value, final boolean selected,
    final boolean expanded, final boolean leaf, final int
    row,
    final boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, selected,
    expanded,
    leaf, row, hasFocus);
    if (value.toString().equals(nameNode)) {
    setIcon(iconLoad);
    setText(valueNode);
    } else {
    for (int i = 0; i < treeRoot.getChildCount(); i++) {
    DefaultMutableTreeNode parent =
    (DefaultMutableTreeNode) treeRoot
    .getChildAt(i);
    if (((TreeFile)
    parent.getUserObject()).toString().equals(
    value.toString())) {
    setText(((TreeFile)
    parent.getUserObject()).getPath());
    }
    for (int j = 0; j < parent.getChildCount(); j++) {
    DefaultMutableTreeNode node =
    (DefaultMutableTreeNode) parent
    .getChildAt(j);
    TreeFile tf = (TreeFile) node.getUserObject();
    String fileName = tf.getPath();
    if (tf.toString().equals(value.toString())) {
    setText(fileName);
    }
    }
    }
    }
    return this;
    }
    };

    private ActionListener changeFileIconNoLoadAction = new
    ActionListener() {
    public void actionPerformed(final ActionEvent e) {
    getNameValueNode(false);
    }
    };

    private void getNameValueNode(boolean loading) {
    nameNode =
    treeFiles.getLastSelectedPathComponent().toString();
    for (int i = 0; i < treeRoot.getChildCount(); i++) {
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode)
    treeRoot
    .getChildAt(i);
    for (int j = 0; j < parent.getChildCount(); j++) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
    parent
    .getChildAt(j);
    TreeFile tf = (TreeFile) node.getUserObject();
    String fileName = tf.getPath();
    if (tf.toString().equals(

    treeFiles.getLastSelectedPathComponent().toString())) {
    valueNode = fileName;
    if (loading) {

    treeFiles.setCellRenderer(filesLoadTreeCellRenderer);
    } else {

    treeFiles.setCellRenderer(filesNoLoadTreeCellRenderer);
    }
    }
    }
    }
    }

    private TreeCellRenderer filesNoLoadTreeCellRenderer = new
    DefaultTreeCellRenderer() {

    public Component getTreeCellRendererComponent(final JTree
    tree,
    final Object value, final boolean selected,
    final boolean expanded, final boolean leaf, final int
    row,
    final boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, selected,
    expanded,
    leaf, row, hasFocus);
    if (value.toString().equals(nameNode)) {
    setIcon(iconNotLoad);
    setText(valueNode);
    } else {
    for (int i = 0; i < treeRoot.getChildCount(); i++) {
    DefaultMutableTreeNode parent =
    (DefaultMutableTreeNode) treeRoot
    .getChildAt(i);
    if (((TreeFile)
    parent.getUserObject()).toString().equals(
    value.toString())) {
    setText(((TreeFile)
    parent.getUserObject()).getPath());
    }
    for (int j = 0; j < parent.getChildCount(); j++) {
    DefaultMutableTreeNode node =
    (DefaultMutableTreeNode) parent
    .getChildAt(j);
    TreeFile tf = (TreeFile) node.getUserObject();
    String fileName = tf.getPath();
    if (tf.toString().equals(value.toString())) {
    setText(fileName);
    }
    }
    }
    }
    return this;
    }
    };
    }

    When I choose TreeCellRenderer filesLoadTreeCellRenderer the selected
    node in jtree change icon, but if I choose TreeCellRenderer
    filesNoLoadTreeCellRenderer the selected node in jtree change icon but
    previously changings icon do not save. Why?
    , Feb 18, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miro Durcik

    app.rc resource's icon to .net Icon

    Miro Durcik, Oct 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,379
    Miro Durcik
    Oct 9, 2004
  2. Replies:
    1
    Views:
    3,953
    Thomas Weidenfeller
    Mar 31, 2005
  3. Dan Polansky
    Replies:
    3
    Views:
    786
    Larry Barowski
    Feb 12, 2007
  4. jumpz
    Replies:
    3
    Views:
    613
    Jean-Baptiste Nizet
    Nov 12, 2007
  5. Bodi
    Replies:
    1
    Views:
    269
    Lasse Reichstein Nielsen
    Nov 18, 2003
Loading...

Share This Page