Java : Generate JTree from XML dynamically - code example

In this post, i am going to describe how to generate JTree in swing according to an XML document dynamically.


Used XML File : catalog.xml


<?xml version="1.0" encoding="UTF-8"?>
<catalog>
      <book id="001" lang="ENG">
            <isbn>23-34-42-3</isbn>
            <regDate>1990-05-24</regDate>
            <title>Operating Systems</title>
            <publisher country="USA">Pearson</publisher>
            <price>400</price>
            <authors>
                  <author>Ganesh Tiwari</author>
            </authors>
      </book>
      <book id="002">
            <isbn>24-300-042-3</isbn>
            <regDate>1995-05-12</regDate>
            <title>Distributed Systems</title>
            <publisher country="Nepal">Ekata</publisher>
            <price>500</price>
            <authors>
                  <author>Ganesh T</author>
                  <author>Tiwari Ganesh</author>
                  <author>Test Author</author>
            </authors>
            </book>
</catalog>

Transformation Code : ObjectModelToTreeModel.Java
public class ObjectModelToTreeModel {
public static DefaultMutableTreeNode getTreeOfModel(List<Book> bookL) {
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Catalog");
      for (Book bk : bookL) {
            DefaultMutableTreeNode book = new DefaultMutableTreeNode("Book " + bk.getTitle() + "BookId: " + bk.getId());

            book.add(new DefaultMutableTreeNode("Language: " + bk.getLang()));
            book.add(new DefaultMutableTreeNode("Isbn :" + bk.getIsbn()));
            book.add(new DefaultMutableTreeNode("Registration Date:" + bk.getRegDate().toString()));
            book.add(new DefaultMutableTreeNode("Publisher :" + bk.getPublisher()));

            DefaultMutableTreeNode authors = new DefaultMutableTreeNode("Authors:");
            for (String auths : bk.getAuthors()) {
                  authors.add(new DefaultMutableTreeNode(auths));
            }
            book.add(authors);
            root.add(book);
      }

      return root;
}
}

Test App :XmlToJTreeMain.Java

public class XmlToJTreeMain extends JFrame {
private JPanel jContentPane = null;
JPanel panel;

private JPanel getJContentPane() {
      if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);

            JButton btnGenerate = new JButton("Generate");
            btnGenerate.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent arg0) {
                        SaxParser sp = new SaxParser("catalog.xml");
                        JTree jt = new JTree(ObjectModelToTreeModel.getTreeOfModel(sp.getBookL()));
                        JScrollPane jp = new JScrollPane(jt);
                        jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                        jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                        jp.setBounds(2, 4, 500, 300);
                        panel.add(jp);
                        panel.revalidate();
                        panel.repaint();
                  }
            });
            btnGenerate.setBounds(334, 11, 89, 23);
            jContentPane.add(btnGenerate);
            panel = new JPanel();
            panel.setBounds(10, 11, 314, 290);
            panel.setLayout(null);
            jContentPane.add(panel);

      }
      return jContentPane;
}

public XmlToJTreeMain() {
      super();
      setSize(441, 350);
      setContentPane(getJContentPane());
      setTitle("JFrame");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
}

public static void main(String[] args) {
      new XmlToJTreeMain();
}
}

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...