Reputation: 992
I have the following situation:
For ech tree node I have a different type of image icon.
The way I actually set these icons is by overriding getTreeCellRendererComponent
, verifying the user object (the title), and then painting the icon.
Else, set the icon to an object icon (for o3) because it does not have a stable name.
private final String OBJECTS, OBJECT, MATERIAL, DIMENSIONS, L, W, H,
LEFT, RIGHT, FRONT, BACK, TOP, BOTTOM; //=...
@Override
public 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);
node = (DefaultMutableTreeNode) value;
String str = node.getUserObject().toString().toLowerCase();
if (str.contains("objects")) {
setIcon(new ImageIcon(OBJECTS));
}
else if (str.contains("components")) {
setIcon(new ImageIcon(OBJECTS));
}
else if (str.contains("dimensions")) {
setIcon(new ImageIcon(DIMENSIONS));
}
else if (str.contains("materials")) {
setIcon(new ImageIcon(MATERIAL));
}
else if (str.contains("length")) {
setIcon(new ImageIcon(L));
}
else if (str.contains("width")) {
setIcon(new ImageIcon(W));
}
else if (str.contains("height")) {
setIcon(new ImageIcon(H));
}
else if (node.getParent().toString().toLowerCase().contains("components") ||
node.getParent().toString().toLowerCase().contains("objects")) {
setIcon(new ImageIcon(OBJECT));
}
return this;
}
I am stuck at painting Materials' children. I know the order (up, down, L, R, front, bottom), but I need a way to identify the nodes. I was thinking to get their parent and somehow identify them by their number in the childer list. Need help here.
Upvotes: 1
Views: 514
Reputation: 197767
You have the problem to formulate the decision that maps an image/icon to the node.
I suggest you use the strategy pattern so that you can change the way on how to find out which icon should be used for the node in question.
You can then try different ways to solve the problem and also the routine does not get stuck with all the ifs and such.
Upvotes: 1