2
|
1 package rep;
|
|
2
|
|
3 import java.awt.Component;
|
|
4
|
|
5 import javax.swing.JFrame;
|
|
6 import javax.swing.JTree;
|
|
7 import javax.swing.event.TreeSelectionEvent;
|
|
8 import javax.swing.event.TreeSelectionListener;
|
|
9 import javax.swing.tree.DefaultMutableTreeNode;
|
|
10
|
|
11 public class SessionViewer implements TreeSelectionListener {
|
|
12 /**
|
|
13 *
|
|
14 */
|
|
15 private static final long serialVersionUID = 1L;
|
|
16 DefaultMutableTreeNode nRoot = new DefaultMutableTreeNode("top");
|
|
17 DefaultMutableTreeNode nJapan = new DefaultMutableTreeNode("Session:1");
|
|
18 DefaultMutableTreeNode nUsa = new DefaultMutableTreeNode("Session:2");
|
|
19 DefaultMutableTreeNode nKantoh = new DefaultMutableTreeNode("Editor:1");
|
|
20 DefaultMutableTreeNode nKansai = new DefaultMutableTreeNode("Editor:2");
|
|
21 DefaultMutableTreeNode nTokyo = new DefaultMutableTreeNode("Editor:3");
|
|
22 private JTree tree;
|
|
23 SessionViewer() {
|
|
24 tree = new JTree(nRoot);
|
|
25 tree.setRootVisible(true);
|
|
26 tree.addTreeSelectionListener(this);
|
|
27 //getContentPane().add(tree);
|
|
28
|
|
29 nRoot.add(nJapan);
|
|
30 nRoot.add(nUsa);
|
|
31 nJapan.add(nKantoh);
|
|
32 nJapan.add(nKansai);
|
|
33 nUsa.add(nTokyo);
|
|
34 //nJapan.remove(0);
|
|
35
|
|
36 //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
37 //setTitle("JTreeTest");
|
|
38 //setSize(200, 150);
|
|
39 //setVisible(true);
|
|
40 }
|
|
41 public void valueChanged(TreeSelectionEvent e) {
|
|
42 JTree tree = (JTree)e.getSource();
|
|
43 DefaultMutableTreeNode node =
|
|
44 (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
|
|
45 if (node != null) {
|
|
46 System.out.println((String)node.getUserObject());
|
|
47 }
|
|
48 }
|
|
49 public static void main(String[] args) {
|
|
50 new SessionViewer();
|
|
51 }
|
|
52 public Component getTree() {
|
|
53 // TODO Auto-generated method stub
|
|
54 return tree;
|
|
55 }
|
|
56 public void addSessionTree(int sid) {
|
|
57 // TODO Auto-generated method stub
|
|
58 nRoot.add(new DefaultMutableTreeNode("Session:"+sid));
|
|
59 }
|
|
60 }
|