change icon in jtree

B

Bumsys

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Jtree renderers 0
JTable within JTree 1
[SWING] Problem with JTree 2
JTree problem 0
TreeCellRenderer 2
TreeCellRenderer in Linux 16
[Help] Viewing filesystem with JTree 1
TreeCellRenderer does not work? 2

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top