package com.demo;
import java.awt.Component;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
public class JFrameDemo {
public static void main(String[] args) {
JTree jtreeCategory = new JTree();
jtreeCategory.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
jtreeCategory.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jtreeCategory.getSelectionPath()
.getLastPathComponent();
if (selectedNode.getUserObject() instanceof Category) {
Category category = (Category) selectedNode.getUserObject();
System.out.println(category.toString());
}
}
});
List<Category> categories = new ArrayList<Category>();
categories.add(new Category(1, "Category 1", "photo1.gif"));
categories.add(new Category(2, "Category 2", "photo2.gif"));
categories.add(new Category(3, "Category 3", "photo3.gif"));
categories.add(new Category(4, "Category 4", "photo4.gif"));
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Categories");
for (Category category : categories) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(category);
root.add(node);
}
DefaultTreeModel defaultTreeModel = new DefaultTreeModel(root);
jtreeCategory.setModel(defaultTreeModel);
jtreeCategory.setCellRenderer(new CategoryTreeCellRenderer());
}
public static class CategoryTreeCellRenderer extends DefaultTreeCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
Component component = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (node.getUserObject() instanceof Category) {
Category category = (Category) node.getUserObject();
ImageIcon imageIcon = new ImageIcon(new ImageIcon("src\\resources\\" + category.getPhoto()).getImage()
.getScaledInstance(50, 50, Image.SCALE_DEFAULT));
this.setIcon(imageIcon);
this.setText(category.getName());
}
return component;
}
}
public static class Category {
private int id;
private String name;
private String photo;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public Category(int id, String name, String photo) {
this.id = id;
this.name = name;
this.photo = photo;
}
public Category() {
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", photo=" + photo + "]";
}
}
}