1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
public class MyTreeIcon extends JPanel
implements TreeSelectionListener {
private JTree tree;
DefaultMutableTreeNode top;
public MyTreeIcon(String title) {
super(new GridLayout(1, 0));
top = new DefaultMutableTreeNode(title);
//Create a tree that allows one selection at a time.
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Set the icon for leaf nodes.
ImageIcon leafIcon = new ImageIcon("./ressources/plus.png");
if (leafIcon != null) {
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(leafIcon);
tree.setCellRenderer(renderer);
} else {
System.err.println("Leaf icon missing; using default.");
}
//Listen for when the selection changes.
tree.addTreeSelectionListener(this);
//JTree properties
tree.setShowsRootHandles(true);
tree.setToggleClickCount(1);
//Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
add(treeView);
}
/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node == null) {
return;
}
Object nodeInfo = node.getUserObject();
System.out.println(nodeInfo.toString());
}
private void addNodes(DefaultMutableTreeNode category,String title) {
category.add(new DefaultMutableTreeNode(title));
}
private void addNodes(DefaultMutableTreeNode category, DefaultMutableTreeNode title) {
category.add(title);
}
public void create(){
DefaultMutableTreeNode category = new DefaultMutableTreeNode("test1");
addNodes(top,category);
addNodes(category,"Proc1.xml");
addNodes(category,"Proc2.xml");
addNodes(category,"Proc3,xml");
addNodes(category,"Proc4.xml");
addNodes(category,"Proc5.xml");
addNodes(category,"Proc6.xml");
addNodes(category,"Proc7.xml");
addNodes(category,"Proc8.xml");
addNodes(category,"Proc9.xml");
addNodes(category,"Proc10.xml");
category = new DefaultMutableTreeNode("test2");
addNodes(top,category);
addNodes(category,"Proc1.xml");
addNodes(category,"Proc2.xml");
addNodes(category,"Proc3,xml");
addNodes(category,"Proc4.xml");
addNodes(category,"Proc5.xml");
addNodes(category,"Proc6.xml");
addNodes(category,"Proc7.xml");
addNodes(category,"Proc8.xml");
addNodes(category,"Proc9.xml");
addNodes(category,"Proc10.xml");
}
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("TreeIconDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
MyTreeIcon newContentPane = new MyTreeIcon("Bibliothèque");
newContentPane.create();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
} |
Partager