在同一个表中以ID和ParentID两个字段来表明各条数据之间的逻辑关系。
请帮忙设计一个取出各个节点的算法,只写写思路也可以.最好有实现.谢了.
------解决方案--------------------
- Java code
package com.xh.java4.TestJtree;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import javax.swing.*;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.*;import com.xh.java.Utils.StringUtils;public class JtreeFrame extends JFrame{ public TreeDBConnect DbConnect = new TreeDBConnect(); TreeMode modeData; private JPanel mainPanel = new JPanel(); private JPanel contextPanel = new JPanel(); private JLabel signCotext = new JLabel("ContextPanel"); private DefaultMutableTreeNode rootNode; private DefaultTreeModel areaMode; private JTree areaTree; private JScrollPane treeScrollpane; private JSplitPane treeAndContext; public JtreeFrame(){ mainPanel.setLayout(new BorderLayout()); init(); this.setContentPane(mainPanel); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(100,100, 600, 400); this.setVisible(true); } public void init(){ modeData = new TreeMode(null,DbConnect.queryEntity("select * from areatreedata")); rootNode = new DefaultMutableTreeNode("湖南省"); DefaultMutableTreeNode changshaNode = new DefaultMutableTreeNode("长沙地区"); rootNode.add(changshaNode); DefaultMutableTreeNode liuyangNode = new DefaultMutableTreeNode("浏阳市"); changshaNode.add(liuyangNode); areaMode = new DefaultTreeModel(rootNode); areaTree = new JTree(modeData);// contextPanel.add(signCotext,BorderLayout.CENTER); areaTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener(){ @Override public void valueChanged(TreeSelectionEvent e) { // TODO Auto-generated method stub TreePath selectPath = e.getPath(); TreeNodeBean selectBean = (TreeNodeBean)selectPath.getLastPathComponent(); String action = selectBean.getActionUrl(); if(StringUtils.hasText(action)){ Class clzz = null; Component comp = null; try { clzz = Class.forName(action); comp = (Component)clzz.newInstance(); contextPanel.removeAll(); contextPanel.add(comp,BorderLayout.CENTER); contextPanel.revalidate(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }else{ signCotext.setText("NodeID:"+selectBean.getNodeID()+"\t" +"ParentID:"+selectBean.getParentID()+"\t" +"Label:"+selectBean.getLabel()+"\t"); contextPanel.removeAll(); contextPanel.add(signCotext,BorderLayout.CENTER); contextPanel.revalidate(); } } }); treeScrollpane = new JScrollPane(areaTree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// treeScrollpane.setSize(100, 300); treeAndContext = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,treeScrollpane,contextPanel); mainPanel.add(treeAndContext,BorderLayout.CENTER);// mainPanel.add(treeScrollpane,BorderLayout.WEST); } public void operator(){ } public static void main(String[] args){ new JtreeFrame(); }}