JTree With Nodes From An XML File

Sample coding:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.tree.*;

import java.util.*;

public class Test3 extends JFrame {

JTree jt1 = new JTree(), jt2 = new JTree(new DefaultMutableTreeNode());

public Test3() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container content = getContentPane();

Vector v = new Vector();

Enumeration e = ((DefaultMutableTreeNode)((DefaultTreeModel)jt1.getModel()).getRoot()).breadthFirstEnumeration();

while (e.hasMoreElements()) v.add(e.nextElement());

final JList jl = new JList(v);

content.add(new JScrollPane(jl), BorderLayout.WEST);

jl.addListSelectionListener(new ListSelectionListener() {

public void valueChanged(ListSelectionEvent lse) {

((DefaultTreeModel)jt2.getModel()).setRoot((TreeNode)jl.getSelectedValue());

}

});

JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,new JScrollPane(jt1),

new JScrollPane(jt2));

jsp.setResizeWeight(0.5);

content.add(jsp);

jt1.addTreeSelectionListener(new TreeSelectionListener() {

public void valueChanged(TreeSelectionEvent tse) {

((DefaultTreeModel)jt2.getModel()).setRoot((TreeNode)tse.getPath().getLastPathComponent());

}

});

setSize(500,500);

setVisible(true);

}

public static void main(String[] arghs) { new Test3(); }

}

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.