List of usage examples for javax.swing.tree MutableTreeNode getParent
TreeNode getParent();
TreeNode
of the receiver. From source file:MainClass.java
public static void main(String[] args) { // create a hierarchy of nodes MutableTreeNode root = new DefaultMutableTreeNode("A"); MutableTreeNode bNode = new DefaultMutableTreeNode("B"); MutableTreeNode cNode = new DefaultMutableTreeNode("C"); root.insert(bNode, 0);/*from w w w . j a va2 s . c o m*/ root.insert(cNode, 1); bNode.insert(new DefaultMutableTreeNode("1"), 0); bNode.insert(new DefaultMutableTreeNode("2"), 1); cNode.insert(new DefaultMutableTreeNode("1"), 0); cNode.insert(new DefaultMutableTreeNode("2"), 1); final DefaultTreeModel model = new DefaultTreeModel(root); final JTree tree = new JTree(model); final JTextField nameField = new JTextField("Z"); final JButton button = new JButton("Add"); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath tp = tree.getSelectionPath(); MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent(); int insertIndex = 0; if (insertNode.getParent() != null) { MutableTreeNode parent = (MutableTreeNode) insertNode.getParent(); insertIndex = parent.getIndex(insertNode) + 1; insertNode = parent; } MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText()); model.insertNodeInto(node, insertNode, insertIndex); } }); JPanel addPanel = new JPanel(new GridLayout(2, 1)); addPanel.add(nameField); addPanel.add(button); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath tp = e.getNewLeadSelectionPath(); button.setEnabled(tp != null); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.getContentPane().add(new JScrollPane(tree)); frame.getContentPane().add(addPanel, BorderLayout.SOUTH); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) { MutableTreeNode root = new DefaultMutableTreeNode("A"); MutableTreeNode beams = new DefaultMutableTreeNode("B"); MutableTreeNode gears = new DefaultMutableTreeNode("C"); root.insert(beams, 0);/* w w w . j a v a 2 s .c o m*/ root.insert(gears, 1); beams.insert(new DefaultMutableTreeNode("4 "), 0); beams.insert(new DefaultMutableTreeNode("6 "), 1); beams.insert(new DefaultMutableTreeNode("8 "), 2); beams.insert(new DefaultMutableTreeNode("12 "), 3); gears.insert(new DefaultMutableTreeNode("8t"), 0); gears.insert(new DefaultMutableTreeNode("24t"), 1); gears.insert(new DefaultMutableTreeNode("40t"), 2); final DefaultTreeModel model = new DefaultTreeModel(root); final JTree tree = new JTree(model); final JTextField nameField = new JTextField("16t"); final JButton button = new JButton("Add a part"); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath tp = tree.getSelectionPath(); MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent(); int insertIndex = 0; if (insertNode.getParent() != null) { MutableTreeNode parent = (MutableTreeNode) insertNode.getParent(); insertIndex = parent.getIndex(insertNode) + 1; insertNode = parent; } MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText()); model.insertNodeInto(node, insertNode, insertIndex); } }); JPanel addPanel = new JPanel(new GridLayout(2, 1)); addPanel.add(nameField); addPanel.add(button); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath tp = e.getNewLeadSelectionPath(); button.setEnabled(tp != null); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.getContentPane().add(new JScrollPane(tree)); frame.getContentPane().add(addPanel, BorderLayout.SOUTH); frame.setVisible(true); }
From source file:plugin.notes.gui.NotesTreeNode.java
/** * adds a MutableTreeNode// w ww. j a v a 2s . c o m * * @param node * Node to add */ public void add(MutableTreeNode node) { if ((node != null) && (node.getParent() == this)) { insert(node, getChildCount() - 1); } else { insert(node, getChildCount()); } }
From source file:plugin.notes.gui.NotesTreeNode.java
/** * Inserts a new MutableTreeNode into this node as a child. * * @param child//from w ww . ja v a2 s . c o m * Child to insert * @param index * Location to insert it. */ @Override public void insert(MutableTreeNode child, int index) { if (!allowsChildren) { throw new IllegalStateException("node does not allow children"); } else if (child == null) { throw new IllegalArgumentException("new child is null"); } else if (isNodeAncestor(child)) { throw new IllegalArgumentException("new child is an ancestor"); } if (!hasBeenPopulated) { populate(); } MutableTreeNode oldParent = (MutableTreeNode) child.getParent(); if (oldParent != null) { oldParent.remove(child); } child.setParent(this); if (children == null) { children = new Vector<>(); } children.insertElementAt(child, index); }