File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Jtree adding nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtree adding nodes" Watch "Jtree adding nodes" New topic
Author

Jtree adding nodes

Rupali Desai
Ranch Hand

Joined: May 29, 2001
Posts: 42
Hello,
I am trying to build a Jtree as follows(like in an IDE)
Clasname
Fields
Field1
Field2
and so on.
Everything works fine upto building the the child nodes for Class(like Fields Methods..)
but when I try to add Field1,Field2.. so on to Field
I get a nullpointer Exception
My code is as follows

public void addNode(String nodeName)
{

String prefix = className;
int startRow = 0;

TreePath path = tree.getNextMatch(prefix, startRow, javax.swing.text.Position.Bias.Forward);
System.out.println("path"+path);
MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();;
System.out.println("Node:"+node);
MutableTreeNode newNode = new DefaultMutableTreeNode(nodeName);
model.insertNodeInto(newNode, node, node.getChildCount());

}




public void addChild(String parentNode,String []NodeList)
{

int startRow = 0;
String prefix = parentNode;
TreeNode rootnode = (TreeNode)tree.getModel().getRoot();
String nn = rootnode.getChildAt(0).toString();
TreePath path = tree.getNextMatch(prefix, startRow, javax.swing.text.Position.Bias.Forward);
System.out.println("path"+path);
MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
for(int i = 0 ;i < NodeList.length; ++i)
{
model.insertNodeInto(new DefaultMutableTreeNode(NodeList[i]),node,node.getChildCount());
}
}

and I am calling it this way
String superclassList[] = ClassInformation.getSuperClasses(c);
if(superclassList.length != 0)
{
addNode("SuperClass");
addChild("SuperClass",superclassList);
}
Why does the path return null at the following line in addChild()
TreePath path = tree.getNextMatch(prefix, startRow, javax.swing.text.Position.Bias.Forward);

Thanks
Rupali Desai
kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27
//this source maybe help u.
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
JTree tree = new JTree();
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
TreeSelectionModel selectionModel = tree.getSelectionModel();
JButton removeButton = new JButton("Remove selected node");
JButton addButton = new JButton("Add node");
public Test() {
Container contentPane = getContentPane();
selectionModel.setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
contentPane.add(new ControlPanel(), BorderLayout.NORTH);
contentPane.add(tree, BorderLayout.CENTER);
tree.addTreeSelectionListener(
new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
boolean nodesAreSelected = (path != null);
addButton.setEnabled(nodesAreSelected);
removeButton.setEnabled(nodesAreSelected);
}
});
model.addTreeModelListener(new TreeModelListener() {
public void treeNodesInserted(TreeModelEvent e) {
showInsertionOrRemoval(e, " added to ");
}
public void treeNodesRemoved(TreeModelEvent e) {
showInsertionOrRemoval(e, " removed from ");
}
private void showInsertionOrRemoval(TreeModelEvent e,
String s) {
Object[] parentPath = e.getPath();
int[] indexes = e.getChildIndices();
Object[] children = e.getChildren();
Object parent = parentPath[parentPath.length-1];
JOptionPane.showMessageDialog(Test.this,
"Node \"" + children[0].toString() +
"\"" + s + parent.toString() +
" at index " + indexes[0],
"Node Added or Removed",
JOptionPane.INFORMATION_MESSAGE);
}
public void treeNodesChanged(TreeModelEvent e) {}
public void treeStructureChanged(TreeModelEvent e) {}
});
}
class ControlPanel extends JPanel {
public ControlPanel() {
addButton.setEnabled(false);
removeButton.setEnabled(false);
add(addButton);
add(removeButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();
MutableTreeNode parent, node =
(MutableTreeNode)path.getLastPathComponent();
if(path.getPathCount() > 1)
parent = (MutableTreeNode)node.getParent();
else
parent = (MutableTreeNode)node;
int index = parent.getIndex(node) + 1;
String s = JOptionPane.showInputDialog(
Test.this,
"Enter a name for the new node:",
"New Tree Node",
JOptionPane.QUESTION_MESSAGE);
MutableTreeNode newNode =
new DefaultMutableTreeNode(s);
model.insertNodeInto(newNode, parent, index);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath path =
selectionModel.getSelectionPath();
if(path.getPathCount() == 1) {
JOptionPane.showMessageDialog(ControlPanel.this,
"Can't remove root node!");
return;
}
MutableTreeNode node =
(MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
}
});
}
}
public static void main(String args[]) {
Test t = new Test();
t.setSize(400,300);
t.show();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jtree adding nodes