-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTreeCellRenderer.java
More file actions
28 lines (23 loc) · 1.14 KB
/
CustomTreeCellRenderer.java
File metadata and controls
28 lines (23 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
// mainly used to show either file or folder icon for nodes in tree view
public class CustomTreeCellRenderer extends DefaultTreeCellRenderer {
@Override
public java.awt.Component getTreeCellRendererComponent(
JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
// Check the type of the node (User or Group)
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object userObject = node.getUserObject();
// Set the icon based on the type
if (userObject instanceof User) {
setIcon(UIManager.getIcon("FileView.fileIcon")); // Use the file icon
} else if (userObject instanceof UserGroup) {
setIcon(UIManager.getIcon("FileView.directoryIcon")); // Use the directory icon
}
}
return this;
}
}