Hello friends,

I am King here. I am new to Java Swing programming.

I got new task in my company. Here, i need to generate a dynamic XML file from Editable JTree.

I have successfully coded for editable JTree. In this tree we can add and delete new nodes. Means, the number of elements will vary from user to user. User will create their own customized tree according to their requirements.

If they click on GenerateXML, we need to generate an XML file according to the tree structure. I am new to swing programming. So, can any one help me for this please............

my Existing code is as follows:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author King Kranthi
*/

package xmlapp.examples;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class EditableTree extends JFrame {

private DefaultMutableTreeNode m_rootNode = new DefaultMutableTreeNode("new");

private DefaultTreeModel m_model = new DefaultTreeModel(m_rootNode);

private JTree m_tree = new JTree(m_model);

private JButton m_addButton = new JButton("Add Node");

private JButton m_delButton = new JButton("Delete Node");

private JButton m_xmlButton = new JButton("Generate XML");

private JButton m_searchButton = new JButton("Search Node");

private JButton m_searchAndDeleteButton = new JButton("Search and Delete Node");

private JTextField m_searchText;

public EditableTree() {

m_tree.setEditable(true);
m_tree.setSelectionRow(0);

JScrollPane scrollPane = new JScrollPane(m_tree);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();

m_addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree
.getLastSelectedPathComponent();
if (selNode == null) {
return;
}
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("new");
m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

TreeNode[] nodes = m_model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
m_tree.scrollPathToVisible(path);
m_tree.setSelectionPath(path);
m_tree.startEditingAtPath(path);
}
});
panel.add(m_addButton);

m_delButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree
.getLastSelectedPathComponent();
removeNode(selNode);
}
});
panel.add(m_delButton);

m_xmlButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree
.getLastSelectedPathComponent();
generateXML();
}
});
panel.add(m_xmlButton);

JPanel searchPanel = new JPanel();
searchPanel.setBorder(BorderFactory.createEtchedBo rder());

m_searchText = new JTextField(10);
searchPanel.add(m_searchText);

m_searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = searchNode(m_searchText.getText());
if (node != null) {
TreeNode[] nodes = m_model.getPathToRoot(node);
TreePath path = new TreePath(nodes);
m_tree.scrollPathToVisible(path);
m_tree.setSelectionPath(path);
System.out.println(nodes.length);
} else {
System.out.println("Node with string '" + m_searchText.getText() + "' not found");
}
}
});
searchPanel.add(m_searchButton);

m_searchAndDeleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = searchNode(m_searchText.getText());
if (node != null) {
removeNode(node);
} else {
System.out.println("Node with string '" + m_searchText.getText() + "' not found");
}
}
});
searchPanel.add(m_searchAndDeleteButton);
panel.add(searchPanel);
getContentPane().add(panel, BorderLayout.SOUTH);
setSize(1000, 700);
setVisible(true);
}

public DefaultMutableTreeNode searchNode(String nodeStr) {
DefaultMutableTreeNode node = null;
Enumeration e = m_rootNode.breadthFirstEnumeration();
while (e.hasMoreElements()) {
node = (DefaultMutableTreeNode) e.nextElement();
if (nodeStr.equals(node.getUserObject().toString())) {
System.out.println("==>"+node);
return node;
}
}
return null;
}

public void removeNode(DefaultMutableTreeNode selNode) {
if (selNode == null) {
return;
}
MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
if (parent == null) {
return;
}
MutableTreeNode toBeSelNode = getSibling(selNode);
if (toBeSelNode == null) {
toBeSelNode = parent;
}
TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode);
TreePath path = new TreePath(nodes);
m_tree.scrollPathToVisible(path);
m_tree.setSelectionPath(path);
m_model.removeNodeFromParent(selNode);
}

private MutableTreeNode getSibling(DefaultMutableTreeNode selNode) {
MutableTreeNode sibling = (MutableTreeNode) selNode.getPreviousSibling();
if (sibling == null) {
sibling = (MutableTreeNode) selNode.getNextSibling();
}
return sibling;
}

public void generateXML(){
DefaultMutableTreeNode node = null;
//Enumeration e = m_rootNode.breadthFirstEnumeration(); //BFS
int e = m_rootNode.getChildCount();
System.out.println(e);
}

public static void main(String[] arg) {
EditableTree editableTree = new EditableTree();
}
}