java 对树形结构的实现
package blue;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class BlueTreeTest extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTree tree;
public BlueTreeTest(){
this.createTree();
this.add(tree);
//设定窗口的长和宽
this.setSize(500,660);
TreeNode root = (TreeNode) tree.getModel().getRoot();
this.expandTree(tree, new TreePath(root));
}
public static void main(String[] args) {
JFrame frame = new BlueTreeTest();
frame.setVisible(true);
}
/**获得树的所有数据*/
public void createTree(){
DefaultMutableTreeNode rootTree=new DefaultMutableTreeNode("/");
tree=new JTree(rootTree);
//定义字符串接收项目ID
String str1="BB11-AAMDP-C01-B034T";
String str2="BB11-AAMDP-C01-B324T";
String str3="BB12-11MDP-H00-XXXXT";
String str4="BB12-ASCOP-M00-XXXXT";
String str5="BB13-ABFCX-C01-A597T";
String str6="BB13-ABFCP-M00-XXXXT";
//加工项目ID为去掉-的字符串
String replaceStr1=str1.replace("-", "");
String replaceStr2=str2.replace("-", "");
String replaceStr3=str3.replace("-", "");
String replaceStr4=str4.replace("-", "");
String replaceStr5=str5.replace("-", "");
String replaceStr6=str6.replace("-", "");
//创建List存放加工后的数据
List<String> strList=new ArrayList<String>();
//添加加工后结果字符串
strList.add(replaceStr1);
strList.add(replaceStr2);
strList.add(replaceStr3);
strList.add(replaceStr4);
strList.add(replaceStr5);
strList.add(replaceStr6);
//设定每次的起始位置
int startPosition=0;
//设定每次的结束位置
int endPosition=0;
//步长
int footLength=0;
if(strList!=null&&strList.size()>0){
for(int i=0;i<strList.size();i++){
startPosition=0;
footLength=4;
endPosition=startPosition+footLength;
//查询是否存在对应子节点的标记
int checkExistFlag=0;
//取出公司的信息(第1-4位)
DefaultMutableTreeNode companyTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<rootTree.getChildCount();j++){
if(rootTree.getChildAt(j).toString().equals(companyTree.toString())){
companyTree=(DefaultMutableTreeNode)rootTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
rootTree.add(companyTree);
}
//重置存在标记
checkExistFlag=0;
startPosition=endPosition;
footLength=2;
endPosition=startPosition+footLength;
//取出地区的信息(第5-6位)
DefaultMutableTreeNode areaTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<companyTree.getChildCount();j++){
if(companyTree.getChildAt(j).toString().equals(areaTree.toString())){
areaTree=(DefaultMutableTreeNode)companyTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
companyTree.add(areaTree);
}
//重置存在标记
checkExistFlag=0;
startPosition=endPosition;
footLength=1;
endPosition=startPosition+footLength;
//取出客户的信息(第7位)
DefaultMutableTreeNode customerTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<areaTree.getChildCount();j++){
if(areaTree.getChildAt(j).toString().equals(customerTree.toString())){
customerTree=(DefaultMutableTreeNode)areaTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
areaTree.add(customerTree);
}
//重置存在标记
checkExistFlag=0;
startPosition=endPosition;
footLength=2;
endPosition=startPosition+footLength;
//取出行业ID的信息(第8-9位)
DefaultMutableTreeNode industryTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<customerTree.getChildCount();j++){
if(customerTree.getChildAt(j).toString().equals(industryTree.toString())){
industryTree=(DefaultMutableTreeNode)customerTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
customerTree.add(industryTree);
}
//重置存在标记
checkExistFlag=0;
startPosition=endPosition;
footLength=1;
endPosition=startPosition+footLength;
//取出产品线的信息(第10位)
DefaultMutableTreeNode productTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<industryTree.getChildCount();j++){
if(industryTree.getChildAt(j).toString().equals(productTree.toString())){
productTree=(DefaultMutableTreeNode)industryTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
industryTree.add(productTree);
}
//重置存在标记
checkExistFlag=0;
startPosition=endPosition;
footLength=2;
endPosition=startPosition+footLength;
//取出月份的信息(第11-12位)
DefaultMutableTreeNode monthTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<productTree.getChildCount();j++){
if(productTree.getChildAt(j).toString().equals(monthTree.toString())){
monthTree=(DefaultMutableTreeNode)productTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
productTree.add(monthTree);
}
//重置存在标记
checkExistFlag=0;
startPosition=endPosition;
endPosition=strList.get(i).length();
//取出其它的信息(第x位)
DefaultMutableTreeNode otherTree=new DefaultMutableTreeNode(""+strList.get(i).substring(startPosition, endPosition));
//添加子节点
for(int j=0;j<monthTree.getChildCount();j++){
if(monthTree.getChildAt(j).toString().equals(otherTree.toString())){
otherTree=(DefaultMutableTreeNode)monthTree.getChildAt(j);
checkExistFlag=1;
break;
}
}
if(checkExistFlag!=1){
monthTree.add(otherTree);
}
}
}else{
System.out.println("请确认项目是否存在");
}
}
/**设置树一开始是全部展开的*/
private void expandTree(JTree tree, TreePath parent) {
TreeNode node = (TreeNode) parent.getLastPathComponent();
if (node.getChildCount() >= 0) {
for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
expandTree(tree, path);
}
}
tree.expandPath(parent);
}
}
效果如下: