public DirTreeModified() { Container c = getContentPane(); // *** Build the tree and a mouse listener to handle clicks root = readNode(); treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); MouseListener ml = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (box.isSelected()) { showDetails(tree.getPathForLocation(e.getX(), e.getY())); } } }; tree.addMouseListener(ml); // *** panel the JFrame to hold controls and the tree controls = new JPanel(); box = new JCheckBox(showString); init(); // ** set colors, fonts, etc. and add buttons c.add(controls, BorderLayout.NORTH); c.add(tree, BorderLayout.CENTER); setVisible(true); // ** display the framed window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ** kills process on exit. }
public MsnTreeTest() { String[] tab = {"hello", "test", "blabla"}; container = getContentPane(); container.setLayout(null); eleve = new DefaultMutableTreeNode("MSN"); worker = new DefaultMutableTreeNode("Worker"); prof = new DefaultMutableTreeNode("Profs"); for (int i = 0; i < tab.length; i++) { worker.add(new DefaultMutableTreeNode(tab[i])); prof.add(new DefaultMutableTreeNode(tab[i])); } // worker.add(new DefaultMutableTreeNode("hello world2")); eleve.add(worker); eleve.add(prof); tree = new JTree(eleve); scroll = new JScrollPane(tree); scroll.setBounds(10, 10, 100, 100); container.add(scroll); setSize(300, 300); setLocation(200, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); // worker.add(n); }
public SelectableTree() { super("JTree Selections"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); String[] namesplitStrings = mainDir.toString().split("\\\\"); String filename = namesplitStrings[namesplitStrings.length - 1]; DefaultMutableTreeNode root = new DefaultMutableTreeNode(filename); addChildren(root, mainDir); tree = new JTree(root); tree.addTreeSelectionListener(this); content.add(new JScrollPane(tree), BorderLayout.CENTER); currentSelectionField = new JTextField("Current Selection: NONE"); content.add(currentSelectionField, BorderLayout.SOUTH); setSize(250, 275); setVisible(true); }
public JTrees() { JFrame frame = new JFrame("JTree Demo"); Container c = frame.getContentPane(); c.setLayout(new BorderLayout()); // Create top node of a tree final DefaultMutableTreeNode top = new DefaultMutableTreeNode("Course"); // Create a subtree UG final DefaultMutableTreeNode UG = new DefaultMutableTreeNode("UG"); top.add(UG); final DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("B.E"); UG.add(a1); final DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("B.C.A"); UG.add(a2); final DefaultMutableTreeNode a3 = new DefaultMutableTreeNode("B.Sc"); UG.add(a3); final DefaultMutableTreeNode a4 = new DefaultMutableTreeNode("B.Com"); UG.add(a4); final DefaultMutableTreeNode a5 = new DefaultMutableTreeNode("B.A"); UG.add(a5); // Create a subtree PG final DefaultMutableTreeNode PG = new DefaultMutableTreeNode("PG"); top.add(PG); final DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("M.E"); PG.add(b1); final DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("M.C.A"); PG.add(b2); final DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("M.Sc"); PG.add(b3); final DefaultMutableTreeNode b4 = new DefaultMutableTreeNode("M.Com"); PG.add(b4); final DefaultMutableTreeNode b5 = new DefaultMutableTreeNode("M.A"); PG.add(b5); // Creating tree final JTree tree = new JTree(top); int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; final JScrollPane jsp = new JScrollPane(tree, v, h); c.add(jsp, BorderLayout.CENTER); final JTextField text = new JTextField("", 20); c.add(text, BorderLayout.SOUTH); tree.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent me) { TreePath tp = tree.getPathForLocation(me.getX(), me.getY()); if (tp != null) text.setText(tp.toString()); else text.setText(""); } }); if (top.children() == b1) { System.out.println("M.E Cource Selected"); } frame.setSize(300, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }