JTree的问题,为什么点击子节点,再点击父节点使其收缩就会出现错误。

larrycheung 2008-04-01 10:50:56


import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;


public class AnimationDemo extends JFrame
{

/**
* @param args
*/


private DefaultTreeModel model;

Color backGroundColor;

DefaultMutableTreeNode root;

JTree animationTree;

JLabel cellImage;

JTextArea imageDesc;

DefaultMutableTreeNode numericalCell;
DefaultMutableTreeNode expCell;

DefaultMutableTreeNode num_vf;




public AnimationDemo()
{


backGroundColor = new Color(192,192,192);
setBackground(backGroundColor);



root = new DefaultMutableTreeNode("动画演示");

DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("水星");


numericalCell = new DefaultMutableTreeNode("数值模型动画");


num_vf = new DefaultMutableTreeNode("数值vf");
// DefaultMutableTreeNode num_karmamovie = new DefaultMutableTreeNode("ff");
// DefaultMutableTreeNode num_fhnsmall = new DefaultMutableTreeNode("dd");

CellAnimation vf = new CellAnimation("num_vf","error");

num_vf.setUserObject(vf);

numericalCell.add(num_vf);
// numericalCell.add(num_karmamovie);
// numericalCell.add(num_fhnsmall);
// num_karmamovie.setUserObject(new CellAnimation("num_karmamovie","karma模型"));

// num_fhnsmall.setUserObject(new CellAnimation("num_fhndopplermoviesmall","FHN模型"));

root.add(numericalCell);


/*

expCell = new DefaultMutableTreeNode("实验模型动画");

DefaultMutableTreeNode exp_calciumwave2s = new DefaultMutableTreeNode("");

exp_calciumwave2s.setUserObject(new CellAnimation("exp_calciumwave2s","钙离子波"));

expCell.add( exp_calciumwave2s);

DefaultMutableTreeNode exp_23largesmall = new DefaultMutableTreeNode("");

exp_23largesmall.setUserObject(new CellAnimation("exp_23largesmall","心肌细胞"));

expCell.add(exp_23largesmall);

root.add(expCell);

*/


root.add(mercury);



CellAnimation cellMercury = new CellAnimation("Mercury","水星");

mercury.setUserObject(cellMercury);



model = new DefaultTreeModel(root);



animationTree = new JTree(model);



cellImage = new JLabel("",JLabel.CENTER);
imageDesc = new JTextArea();



animationTree.addTreeSelectionListener(new
TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent event)
{
// the user selected a different node--update description

TreePath path = animationTree.getSelectionPath();

//就是这里出错,
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();

if(selectedNode==null)
{
JOptionPane.showMessageDialog(null, "SelectNode");
return;


}

if(!selectedNode.isLeaf())
{

JOptionPane.showMessageDialog(null,selectedNode.toString());
return;

}
//Cast chu 错
//
CellAnimation temp = (CellAnimation)selectedNode.getUserObject();

cellImage.setIcon(temp.getImage());

imageDesc.setText(temp.getDescription());

}



});


animationTree.setRootVisible(true);


int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;


animationTree.getSelectionModel().setSelectionMode(mode);
//animationTree.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// set up split panes

JScrollPane jsp = new JScrollPane(animationTree);


//jsp.add(animationTree);



JSplitPane innerPane
= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jsp, cellImage);

innerPane.setDividerSize(10);

innerPane.setDividerLocation(200);

innerPane.setContinuousLayout(true);
innerPane.setOneTouchExpandable(true);

JSplitPane outerPane
= new JSplitPane(JSplitPane.VERTICAL_SPLIT, innerPane, imageDesc);

outerPane.setDividerSize(10);
outerPane.setDividerLocation(200);

add(outerPane, BorderLayout.CENTER);


}




public static void main(String[] args)
{
// TODO 自动生成方法存根

AnimationDemo application = new AnimationDemo();

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

application.setSize(500,350);
application.setVisible(true);
}

}


class CellAnimation
{

private ImageIcon image;

private String name;

private String description;

public CellAnimation(String name,String desc)
{
//super();
this.name = name;
description = desc;

image =new ImageIcon(name + ".gif");

if(image==null)
{
JOptionPane.showMessageDialog(null,"bad Mallocation");
}


}

public ImageIcon getImage() {
return image;
}

public void setImage(ImageIcon image) {
this.image = image;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString()
{
return name;
}


public String getDescription() {
return description;
}


public void setDescription(String description) {
this.description = description;
}


}

...全文
130 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧