I populate my JTree with nodes from enum values. But the nodes display in all uppercase text. This is undesirable as I would like the nodes to display in lower case.
example:
public enum DaysOfTheWeek {
MONDAY("Monday", "MON", "First day of the work week."),
//etc ...
private final String fullName;
private final String abbrvName;
private final String description;
DaysOfTheWeek(String fullName, String abbrvName, String description) {
this.fullName = fullName;
//etc ...
}
public String getFullName() {
return fullName;
}
}
I have tried:
List<DefaultMutableTreeNode> daysOfWeekNodes = new ArrayList<>();
for(DaysOfTheWeek dotw : DaysOfTheWeek.values()) {
daysOfWeekNodes.add(new DefaultMutableTreeNode(dotw.getFullName()));
daysOfWeekNodes.get(dotw.ordinal()).setUserObject(dotw);
}
The node displays as: MONDAY But I want it to display as: Monday
text based graphic example:
Days
|
---Monday
|
---Tuesday
not
Days
|
---MONDAY
|
---TUESDAY
How do I get my tree node associated with the enum value, but its text on the tree to use the full name String? Or in other words, how to set a tree node user object but have its name different?
*note - an easy fix is to go against convention and name my values how I want them displayed in the tree, not all uppercase.
You need to use a
TreeCellRenderer
.Here is one way to implement this:
Take a look at the JTree tutorial.