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?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|