List of usage examples for javax.swing.event TreeExpansionListener TreeExpansionListener
TreeExpansionListener
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); }//from w w w . jav a 2 s. 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(String args[]) { JFrame f = new JFrame("JTree Demo"); f.setSize(260, 240);/*from w w w. j a va2 s. c o 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:gov.sandia.umf.platform.ui.jobs.RunPanel.java
public RunPanel() { root = new NodeBase(); model = new DefaultTreeModel(root); tree = new JTree(model); tree.setRootVisible(false);// w w w . j a v a 2s .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:eu.apenet.dpt.standalone.gui.APETabbedPane.java
/** * Creates the XML tree of the file or of the converted file (if file already converted) * @param file The file represented in the list * @return A JComponent containing the tree if it exists, or an error message if not */// w ww . java2 s . c om public JComponent createEditionTree(File file) { labels = dataPreparationToolGUI.getLabels(); if (file == null) { editionPane.setViewportView(createMsgEditionTree(labels.getString("noTreeBuild") + "...")); return null; } FileInstance fileInstance = dataPreparationToolGUI.getFileInstances().get(file.getName()); if (tree != null && ((XMLTreeTableModel) tree.getTreeTableModel()).getName().equals(file.getName()) && fileInstance.getLastOperation().equals(FileInstance.Operation.EDIT_TREE)) return null; // fileInstance.setLastOperation(FileInstance.Operation.CREATE_TREE); try { ReaderInputStream readerInputStream; if (fileInstance.getCurrentLocation() == null || fileInstance.getCurrentLocation().equals("")) { Reader reader = new FileReader(file.getAbsolutePath()); readerInputStream = new ReaderInputStream(reader, "UTF-8"); } else { Reader reader = new FileReader(fileInstance.getCurrentLocation()); readerInputStream = new ReaderInputStream(reader, "UTF-8"); } Document doc = DOMUtil.createDocument(readerInputStream); tree = new JXTreeTable(); tree.setTreeTableModel(new XMLTreeTableModel(doc, dataPreparationToolGUI.getDateNormalization(), dataPreparationToolGUI.getLevelList(), labels, fileInstance, dataPreparationToolGUI)); tree.setTreeCellRenderer(new XMLTreeTableRenderer()); tree.addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent treeExpansionEvent) { int row = tree.getRowForPath(treeExpansionEvent.getPath()); tree.scrollRowToVisible(row); tree.scrollRowToVisible(row + 1); tree.scrollRowToVisible(row + 2); tree.scrollRowToVisible(row + 3); tree.scrollRowToVisible(row + 4); } public void treeCollapsed(TreeExpansionEvent treeExpansionEvent) { } }); expandFirstLevel(); // tree.expandAll(); // tree.setHighlighters(HighlighterFactory.createSimpleStriping (HighlighterFactory.QUICKSILVER)); JScrollPane paneTest = new JScrollPane(tree); editionPane.setViewportView(paneTest); tree.setEditable(true); dataPreparationToolGUI.enableSaveBtn(); dataPreparationToolGUI.setTree(tree); ((XMLTreeTableModel) tree.getTreeTableModel()).setName(file.getName()); } catch (Exception e) { LOG.info("Error creating tree", e); tree = null; editionPane .setViewportView(createMsgEditionTree(labels.getString("edition.error.fileNoXmlOrCorrupted"))); } return tree; }
From source file:nl.tudelft.goal.SimpleIDE.FilePanel.java
/** * Adds all relevant listeners to this FilePanel's tree. *///from ww w .ja va 2 s .com private void addListeners() { // add mouse listener this.fileTree.addMouseListener(new myMouseListener()); // add tree selection listener this.fileTree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { // theIDE.refreshMenuItemsAndButtons(); ActionFactory.broadcastStateChange(FilePanel.this.ideState); } }); // tree expansion listener this.fileTree.addTreeExpansionListener(new TreeExpansionListener() { @Override public void treeExpanded(TreeExpansionEvent event) { // theIDE.refreshMenuItemsAndButtons(); ActionFactory.broadcastStateChange(FilePanel.this.ideState); } @Override public void treeCollapsed(TreeExpansionEvent event) { // theIDE.refreshMenuItemsAndButtons(); ActionFactory.broadcastStateChange(FilePanel.this.ideState); } }); }
From source file:org.openehealth.coms.cc.consent_applet.applet.ConsentApplet.java
/** * Listener which handles the expansion of TreeNodes * //from w w w. ja v a 2s. co m * @return */ private TreeExpansionListener getMyTreeExpansionListener() { return new TreeExpansionListener() { public void treeCollapsed(TreeExpansionEvent arg0) { } public void treeExpanded(TreeExpansionEvent arg0) { //Normal Tree, load child Nodes from the server if (arg0.getPath().getLastPathComponent() instanceof LazyOIDTreeNode) { LazyOIDTreeNode node = (LazyOIDTreeNode) arg0.getPath().getLastPathComponent(); if (!node.isLeaf() && !node.hasBeenExpanded()) { node.setHasBeenExpanded(true); requestTreeNode(node); } } //Search Tree, expand as usual else if (arg0.getPath().getLastPathComponent() instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) arg0.getPath().getLastPathComponent(); JTree tree = (JTree) arg0.getSource(); tree.expandPath(new TreePath(node.getPath())); } }; }; }
From source file:org.openmicroscopy.shoola.agents.treeviewer.browser.BrowserUI.java
/** * Creates a new instance.//from w w w . jav a 2s . c om * The {@link #initialize(BrowserControl, BrowserModel) initialize} method * should be called straight after to link this View to the Controller. */ BrowserUI() { sorter = new ViewerSorter(); nodesToReset = new HashSet<TreeImageDisplay>(); listener = new TreeExpansionListener() { public void treeCollapsed(TreeExpansionEvent e) { onNodeNavigation((TreeImageDisplay) e.getPath().getLastPathComponent(), false); } public void treeExpanded(TreeExpansionEvent e) { onNodeNavigation((TreeImageDisplay) e.getPath().getLastPathComponent(), true); } }; }