import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
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.TreePath;
import javax.swing.tree.TreeSelectionModel;
public class TreeDemoBinaer extends JFrame {
private JTree tree;
private DefaultMutableTreeNode root;
private TreeSelectionModel selectionModel;
private DefaultTreeModel model;
public TreeDemoBinaer() {
root = new DefaultMutableTreeNode("root");
this.initializeNode(root, 0);
this.model = new DefaultTreeModel(root);
this.add(new JScrollPane(tree = new JTree(model)));
this.selectionModel = tree.getSelectionModel();
selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
selectionModel.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
for (int i=0; i<root.getChildCount(); i++)
resetStatus((DefaultMutableTreeNode)root.getChildAt(i));
TreePath[] path = tree.getSelectionPaths();
if (path==null)
return;
Object[] nodes = path[0].getPath();
for (int i=1; i<nodes.length; i++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)nodes[i];
CustomObject co = (CustomObject)node.getUserObject();
co.setChecked(true);
}
tree.repaint();
}
});
tree.setCellRenderer(new CustomTreeCellRenderer());
}
class CustomObject {
String label;
boolean checked = false;
CustomObject(String label) {
this.label = label;
}
public void setChecked(boolean b) {
checked = b;
}
public boolean isChecked() {
return checked;
}
public String getLabel() {
return label;
}
}
class CustomTreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (!node.equals(model.getRoot())) {
CustomObject co = (CustomObject)node.getUserObject();
this.setText(co.getLabel());
if (co.isChecked())
this.setIcon(checkIcon);
else
this.setIcon(emptyIcon);
}
return this;
}
}
int count = 0;
private void initializeNode(DefaultMutableTreeNode node, int level) {
if (level>3)
return;
for (int i=0; i<2; i++) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode(new CustomObject("AA" + level));
this.initializeNode(child, level +1);
node.add(child);
}
}
private void resetStatus(DefaultMutableTreeNode node) {
for(int i=0; i<node.getChildCount(); i++)
this.resetStatus((DefaultMutableTreeNode)node.getChildAt(i));
CustomObject co = (CustomObject)node.getUserObject();
co.setChecked(false);
}
public static void main(String[] args) {
JFrame frame = new TreeDemoBinaer();
frame.setBounds(0, 0, 500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private Icon checkIcon = new ImageIcon() {
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.RED);
g.fillRect(0, 0, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 10;
}
public int getIconHeight() {
return 10;
}
};
private Icon emptyIcon = new ImageIcon() {
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.RED);
g.drawRect(0, 0, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 10;
}
public int getIconHeight() {
return 10;
}
};
}