List of usage examples for javax.swing JTree addTreeExpansionListener
public void addTreeExpansionListener(TreeExpansionListener tel)
TreeExpansion
events. From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JTree Demo"); f.setSize(260, 240);/*from www . ja v a 2s .co m*/ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A"); root.add(aNode); DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B"); aNode.add(bNode); DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C"); aNode.add(cNode); cNode.add(new DefaultMutableTreeNode("D")); cNode.add(new DefaultMutableTreeNode("E")); DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F"); root.add(fNode); DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G"); fNode.add(gNode); fNode.add(new DefaultMutableTreeNode("H")); gNode.add(new DefaultMutableTreeNode("I")); JTree jtree = new JTree(root); jtree.setEditable(true); TreeSelectionModel tsm = jtree.getSelectionModel(); tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); jtree.addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent tee) { TreePath tp = tee.getPath(); System.out.println("Expansion: " + tp.getLastPathComponent()); } public void treeCollapsed(TreeExpansionEvent tee) { TreePath tp = tee.getPath(); System.out.println("Collapse: " + tp.getLastPathComponent()); } }); jtree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent tse) { TreePath tp = tse.getPath(); System.out.println("Selection event: " + tp.getLastPathComponent()); } }); jtree.getModel().addTreeModelListener(new TreeModelListener() { public void treeNodesChanged(TreeModelEvent tme) { TreePath tp = tme.getTreePath(); Object[] children = tme.getChildren(); DefaultMutableTreeNode changedNode; if (children != null) changedNode = (DefaultMutableTreeNode) children[0]; else changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent(); System.out.println("Model change path: " + tp + "New data: " + changedNode.getUserObject()); } public void treeNodesInserted(TreeModelEvent tme) { } public void treeNodesRemoved(TreeModelEvent tme) { } public void treeStructureChanged(TreeModelEvent tme) { } }); f.add(new JScrollPane(jtree)); f.setVisible(true); }
From source file:TreeExpansionListenerDemo.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); root.add(new DefaultMutableTreeNode("A")); root.add(new DefaultMutableTreeNode("B")); root.add(new DefaultMutableTreeNode("C")); JTree tree = new JTree(root); TreeExpansionListener treeExpandListener = new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent event) { TreePath path = event.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("Expanded: " + data); }/*w w w . j a va2s. c o m*/ public void treeCollapsed(TreeExpansionEvent event) { TreePath path = event.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("Collapsed: " + data); } }; tree.addTreeExpansionListener(treeExpandListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main() { JTree tree = new JTree(); tree.addTreeWillExpandListener(new MyTreeWillExpandListener()); tree.addTreeExpansionListener(new MyTreeExpansionListener()); JFrame f = new JFrame(); f.add(new JScrollPane(tree)); f.setSize(300, 300);//from w ww . j av a 2s . c o m f.setVisible(true); }
From source file:gov.sandia.umf.platform.ui.jobs.RunPanel.java
public RunPanel() { root = new NodeBase(); model = new DefaultTreeModel(root); tree = new JTree(model); tree.setRootVisible(false);/*from w w w . j a v a2s .c o m*/ tree.setShowsRootHandles(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override 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); NodeBase node = (NodeBase) value; Icon icon = node.getIcon(expanded); // A node knows whether it should hold other nodes or not, so don't pass leaf to it. if (icon == null) { if (leaf) icon = getDefaultLeafIcon(); else if (expanded) icon = getDefaultOpenIcon(); else icon = getDefaultClosedIcon(); } setIcon(icon); return this; } }); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { NodeBase newNode = (NodeBase) tree.getLastSelectedPathComponent(); if (newNode == null) return; if (newNode == displayNode) return; if (displayThread != null) synchronized (displayText) { displayThread.stop = true; } displayNode = newNode; if (displayNode instanceof NodeFile) viewFile(); else if (displayNode instanceof NodeJob) viewJob(); } }); tree.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int keycode = e.getKeyCode(); if (keycode == KeyEvent.VK_DELETE || keycode == KeyEvent.VK_BACK_SPACE) { delete(); } } }); tree.addTreeWillExpandListener(new TreeWillExpandListener() { public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException { TreePath path = event.getPath(); // TODO: can this ever be null? Object o = path.getLastPathComponent(); if (o instanceof NodeJob) ((NodeJob) o).build(tree); } public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException { } }); tree.addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent event) { Rectangle node = tree.getPathBounds(event.getPath()); Rectangle visible = treePane.getViewport().getViewRect(); visible.height -= node.y - visible.y; visible.y = node.y; tree.repaint(visible); } public void treeCollapsed(TreeExpansionEvent event) { Rectangle node = tree.getPathBounds(event.getPath()); Rectangle visible = treePane.getViewport().getViewRect(); visible.height -= node.y - visible.y; visible.y = node.y; tree.repaint(visible); } }); Thread refreshThread = new Thread() { public void run() { try { // Initial load synchronized (running) { for (MNode n : AppData.runs) running.add(0, new NodeJob(n)); // This should be efficient on a doubly-linked list. for (NodeJob job : running) root.add(job); } EventQueue.invokeLater(new Runnable() { public void run() { model.nodeStructureChanged(root); if (model.getChildCount(root) > 0) tree.setSelectionRow(0); } }); // Periodic refresh to show status of running jobs int shortCycles = 100; // Force full scan on first cycle. while (true) { NodeBase d = displayNode; // Make local copy (atomic action) to prevent it changing from under us if (d instanceof NodeJob) ((NodeJob) d).monitorProgress(RunPanel.this); if (shortCycles++ < 20) { Thread.sleep(1000); continue; } shortCycles = 0; synchronized (running) { Iterator<NodeJob> i = running.iterator(); while (i.hasNext()) { NodeJob job = i.next(); if (job != d) job.monitorProgress(RunPanel.this); if (job.complete >= 1) i.remove(); } } } } catch (InterruptedException e) { } } }; refreshThread.setDaemon(true); refreshThread.start(); displayText = new JTextArea(); displayText.setEditable(false); final JCheckBox chkFixedWidth = new JCheckBox("Fixed-Width Font"); chkFixedWidth.setFocusable(false); chkFixedWidth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int size = displayText.getFont().getSize(); if (chkFixedWidth.isSelected()) { displayText.setFont(new Font(Font.MONOSPACED, Font.PLAIN, size)); } else { displayText.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, size)); } } }); displayPane.setViewportView(displayText); ActionListener graphListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (displayNode instanceof NodeFile) { NodeFile nf = (NodeFile) displayNode; if (nf.type == NodeFile.Type.Output || nf.type == NodeFile.Type.Result) { String graphType = e.getActionCommand(); if (displayPane.getViewport().getView() instanceof ChartPanel && displayGraph.equals(graphType)) { viewFile(); displayGraph = ""; } else { if (graphType.equals("Graph")) { Plot plot = new Plot(nf.path.getAbsolutePath()); if (!plot.columns.isEmpty()) displayPane.setViewportView(plot.createGraphPanel()); } else // Raster { Raster raster = new Raster(nf.path.getAbsolutePath(), displayPane.getHeight()); displayPane.setViewportView(raster.createGraphPanel()); } displayGraph = graphType; } } } } }; buttonGraph = new JButton("Graph", ImageUtil.getImage("analysis.gif")); buttonGraph.setFocusable(false); buttonGraph.addActionListener(graphListener); buttonGraph.setActionCommand("Graph"); buttonRaster = new JButton("Raster", ImageUtil.getImage("prnplot.gif")); buttonRaster.setFocusable(false); buttonRaster.addActionListener(graphListener); buttonRaster.setActionCommand("Raster"); Lay.BLtg(this, "C", Lay.SPL(Lay.BL("C", treePane = Lay.sp(tree)), Lay.BL("N", Lay.FL(chkFixedWidth, Lay.FL(buttonGraph, buttonRaster), "hgap=50"), "C", displayPane), "divpixel=250")); setFocusCycleRoot(true); }
From source file:org.kepler.objectmanager.library.LibraryManager.java
/** * Add a reference to a JTree that is using the Library Tree Model so that * it can be updated when the tree model changes. Whenever getTreeModel() is * used in a gui component, the JTree reference should be added using this * method so that it can get refreshed when the library model changes using * the refreshJTrees() method./*from ww w . j ava 2s. c o m*/ * *@param ptree * The feature to be added to the LibraryComponent attribute */ public void addLibraryJTree(JTree ptree) { if (isDebugging) { log.debug("addLibraryComponent(" + ptree.getName() + ")"); } if (isDebugging) log.debug("addLibraryComponent(" + ptree + ")"); WeakReference<JTree> ptreeWR = new WeakReference<JTree>(ptree); _trees.addElement(ptreeWR); if (isDebugging) log.debug("_trees size:" + _trees.size()); // add this tree to the map and register for expansion events _treeExpansionMap.put(ptree, new LinkedList<TreePath>()); ptree.addTreeExpansionListener(this); }
From source file:org.kepler.objectmanager.library.LibraryManager.java
/** * Refresh and redraw any JTrees that the LibraryManager is keeping track * of.//from ww w. j a v a 2 s . c o m * *@exception IllegalActionException * Description of the Exception */ public void refreshJTrees() throws IllegalActionException { if (isDebugging) { log.debug("refresh"); } // Iterate over all the registered JTrees and reset their models. //Iterator<WeakReference> treeItt = _trees.iterator(); int size = _trees.size(); int i = 0; while (i < size) { WeakReference<JTree> wf = _trees.elementAt(i); JTree ptree = (JTree) wf.get(); if (ptree == null) { _trees.remove(wf); size--; } else { ptree.setModel(getTreeModel()); // stop listening for expansion events since we are going to // expand the paths and do not want to modify the list ptree.removeTreeExpansionListener(this); // expand all the paths that were previously expanded final List<TreePath> expansions = _treeExpansionMap.get(ptree); for (TreePath path : expansions) { //System.out.println("expanding " + path); ptree.expandPath(path); } // start listening again for expansion events ptree.addTreeExpansionListener(this); /* EntityTreeModel etm = (EntityTreeModel) ptree.getModel(); // Of course this would be easier if // ptolemy.vergil.tree.EntityTreeModel // had a reload method similar to DefaultTreeModel // that did this for us // TODO etm.reload() ArrayList<NamedObj> path = new ArrayList<NamedObj>(); path.add(0, (NamedObj) etm.getRoot()); TreePath tp = new TreePath(path); etm.valueForPathChanged(tp, etm.getRoot()); */ i++; } } }