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:
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
public class TestTree {
public enum DaysOfTheWeek {
MONDAY("Monday", "MON", "First day of the work week."), TUESDAY("Tuesday", "TUE", "Second day of the work week");
// etc ...
private final String fullName;
private final String abbrvName;
private final String description;
private DaysOfTheWeek(String fullName, String abbrvName, String description) {
this.fullName = fullName;
this.abbrvName = abbrvName;
this.description = description;
}
public String getFullName() {
return fullName;
}
public String getAbbrvName() {
return abbrvName;
}
public String getDescription() {
return description;
}
}
public class MyTreeCellRenderer extends DefaultTreeCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
Component cell = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (value instanceof DefaultMutableTreeNode && ((DefaultMutableTreeNode) value).getUserObject() instanceof DaysOfTheWeek) {
((JLabel) cell).setText(((DaysOfTheWeek) ((DefaultMutableTreeNode) value).getUserObject()).getFullName());
}
return cell;
}
}
private JFrame f;
private JTree tree;
protected void initUI() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Days");
for (DaysOfTheWeek dotw : DaysOfTheWeek.values()) {
root.add(new DefaultMutableTreeNode(dotw));
}
final DefaultTreeModel model = new DefaultTreeModel(root);
tree = new JTree(model);
tree.setRootVisible(true);
tree.setShowsRootHandles(true);
ToolTipManager.sharedInstance().registerComponent(tree);
tree.setCellRenderer(new MyTreeCellRenderer());
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.add(new JScrollPane(tree));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTree().initUI();
}
});
}
}
Take a look at the JTree tutorial.