关于jtree中DefaultTreeCellRenderer的问题,请各位高手帮忙!

newman0708 2002-12-25 07:22:01
怎么使结点的图片能整个显示出来,为什么我的只能显示一小部份,图标的其它部份好像被截去了一样?

请指点一下,具体的问题处在。

public class FileTreePanel extends JPanel {
public static final ImageIcon m_icoRoot =new ImageIcon(FileTreePanel.class.getResource("image/SCR.GIF"));
public static final ImageIcon m_icoFolderClose =new ImageIcon(FileTreePanel.class.getResource("image/dirclose.gif"));//dirclose.gif"));
public static final ImageIcon m_icoFolderOpen =new ImageIcon(FileTreePanel.class.getResource("image/directory.gif"));//diropen.gif"));
public static final ImageIcon m_icoDisk =new ImageIcon(FileTreePanel.class.getResource("image/ROOT.gif"));

......
}
DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData("Computer",this.m_icoRoot));

DefaultMutableTreeNode node;
File[] roots = File.listRoots();//取出根目录的文件(盘符)
for (int k=1; k < roots.length; k++){
node = new IconData(roots[k],this.m_icoDisk);
top.add(node);
//在A盘下加上一个boolean型,表示有子目录
System.out.println(roots[k].getAbsolutePath()+" is created.");
}
m_model = new DefaultTreeModel(top);//创建默认的模型
m_tree=new JTree();
m_tree = new JTree(m_model);//创建树
m_tree.putClientProperty("JTree.lineStyle", "Angled");//加上连线
m_tree.setCellRenderer(new IconNodeRenderer());
System.out.println("setCellRenderer is ok.");
m_tree.getSelectionModel().setSelectionMode(//设置为单选
TreeSelectionModel.SINGLE_TREE_SELECTION);


class IconData extends DefaultMutableTreeNode {

protected ImageIcon m_icoIcon;//正常时图标
protected ImageIcon m_icoExpandedIcon;//打开时
protected boolean m_bisExpanded;//是否扩展
protected String m_szIconName;//名字

/**
* 构造函数(用于盘符,它的两种图标是一样)
* @param userObject 对象 此处用文件 File
* @param icon 正常时图标
*/
public IconData(Object userObject,ImageIcon icon) {
this(userObject,icon,icon);
}

/**
* 构造函数(它的两种图标是不一样)
*
* 注: 一般就用它,其它几个都不用
*
* @param userObject 当前用户操作对象 此处用文件 File
* @param icon 正常,没有打开
* @param expandedIcon 打开时
*/
public IconData(Object userObject,ImageIcon icon, ImageIcon expandedIcon){
super(userObject);
this.m_bisExpanded =false;
this.m_szIconName="";
this.m_icoIcon= icon;
this.m_icoExpandedIcon=expandedIcon;
}

/**
* 设置正常图标
* @param icon
*/
public void setIcon(ImageIcon icon) {
this.m_icoIcon=icon;
}

/**
* 设置扩展图标
* @param icon
*/
public void setExpandedIcon(ImageIcon icon) {
this.m_icoExpandedIcon=icon;
}

/**
* 正常图标
* @return
*/
public ImageIcon getIcon() {
return this.m_icoIcon;
}

/**
* 扩展图标
* @return
*/
public ImageIcon getExpandedIcon() {
return this.m_icoExpandedIcon ;
}

/**
* 图标名字
* @return
*/
public String getIconName() {
if (this.m_szIconName != null) {
return this.m_szIconName;
}
else {
String str = userObject.toString();
int index = str.lastIndexOf(".");
if (index != -1) {
return str.substring(++index);
} else {
return null;
}
}
}

/**
* 设置图标名字
* @param name
*/
public void setIconName(String name) {
this.m_szIconName= name;
}

/**
* 是否扩展
* @return
*/
public boolean isExpanded(){
return this.m_bisExpanded;
}

/**
* 不允许扩展
* @return
*/
public boolean noExpanded(){
this.m_bisExpanded= false;
return this.m_bisExpanded;
}
}

class IconNodeRenderer extends DefaultTreeCellRenderer {

public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf,int row, boolean hasFocus) {

super.getTreeCellRendererComponent(tree, value,sel, expanded, leaf, row, hasFocus);

if (value instanceof Boolean){
setText("Retrieving data...");
//Debug.print("once more!");
}
else if ((value instanceof IconData)||(value instanceof String)){
IconData idata = (IconData)value;
if(!leaf){
if (hasFocus)
setIcon(idata.getExpandedIcon());
else
setIcon(idata.getIcon());
}
}
else
setIcon(null);
this.setPreferredSize(new Dimension(this.getIcon().getIconWidth(),this.getIcon().getIconHeight()));
return this;
}
...全文
210 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
george2956 2002-12-29
  • 打赏
  • 举报
回复
祝元旦愉快!
newman0708 2002-12-25
  • 打赏
  • 举报
回复
对不起我纠正一下,程序应该是这样的。

怎么使结点的图片能整个显示出来,为什么我的只能显示一小部份,图标的其它部份好像被截去了一样?

请指点一下,具体的问题处在。

public class FileTreePanel extends JPanel {
public static final ImageIcon m_icoRoot =new ImageIcon(FileTreePanel.class.getResource("image/SCR.GIF"));
public static final ImageIcon m_icoFolderClose =new ImageIcon(FileTreePanel.class.getResource("image/dirclose.gif"));//dirclose.gif"));
public static final ImageIcon m_icoFolderOpen =new ImageIcon(FileTreePanel.class.getResource("image/directory.gif"));//diropen.gif"));
public static final ImageIcon m_icoDisk =new ImageIcon(FileTreePanel.class.getResource("image/ROOT.gif"));

......
//}纠正的地方
DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData("Computer",this.m_icoRoot));

DefaultMutableTreeNode node;
File[] roots = File.listRoots();//取出根目录的文件(盘符)
for (int k=1; k < roots.length; k++){
node = new IconData(roots[k],this.m_icoDisk);
top.add(node);
//在A盘下加上一个boolean型,表示有子目录
System.out.println(roots[k].getAbsolutePath()+" is created.");
}
m_model = new DefaultTreeModel(top);//创建默认的模型
m_tree=new JTree();
m_tree = new JTree(m_model);//创建树
m_tree.putClientProperty("JTree.lineStyle", "Angled");//加上连线
m_tree.setCellRenderer(new IconNodeRenderer());
System.out.println("setCellRenderer is ok.");
m_tree.getSelectionModel().setSelectionMode(//设置为单选
TreeSelectionModel.SINGLE_TREE_SELECTION);


}//纠正的地方

class IconData extends DefaultMutableTreeNode {

protected ImageIcon m_icoIcon;//正常时图标
protected ImageIcon m_icoExpandedIcon;//打开时
protected boolean m_bisExpanded;//是否扩展
protected String m_szIconName;//名字

/**
* 构造函数(用于盘符,它的两种图标是一样)
* @param userObject 对象 此处用文件 File
* @param icon 正常时图标
*/
public IconData(Object userObject,ImageIcon icon) {
this(userObject,icon,icon);
}

/**
* 构造函数(它的两种图标是不一样)
*
* 注: 一般就用它,其它几个都不用
*
* @param userObject 当前用户操作对象 此处用文件 File
* @param icon 正常,没有打开
* @param expandedIcon 打开时
*/
public IconData(Object userObject,ImageIcon icon, ImageIcon expandedIcon){
super(userObject);
this.m_bisExpanded =false;
this.m_szIconName="";
this.m_icoIcon= icon;
this.m_icoExpandedIcon=expandedIcon;
}

/**
* 设置正常图标
* @param icon
*/
public void setIcon(ImageIcon icon) {
this.m_icoIcon=icon;
}

/**
* 设置扩展图标
* @param icon
*/
public void setExpandedIcon(ImageIcon icon) {
this.m_icoExpandedIcon=icon;
}

/**
* 正常图标
* @return
*/
public ImageIcon getIcon() {
return this.m_icoIcon;
}

/**
* 扩展图标
* @return
*/
public ImageIcon getExpandedIcon() {
return this.m_icoExpandedIcon ;
}

/**
* 图标名字
* @return
*/
public String getIconName() {
if (this.m_szIconName != null) {
return this.m_szIconName;
}
else {
String str = userObject.toString();
int index = str.lastIndexOf(".");
if (index != -1) {
return str.substring(++index);
} else {
return null;
}
}
}

/**
* 设置图标名字
* @param name
*/
public void setIconName(String name) {
this.m_szIconName= name;
}

/**
* 是否扩展
* @return
*/
public boolean isExpanded(){
return this.m_bisExpanded;
}

/**
* 不允许扩展
* @return
*/
public boolean noExpanded(){
this.m_bisExpanded= false;
return this.m_bisExpanded;
}
}

class IconNodeRenderer extends DefaultTreeCellRenderer {

public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf,int row, boolean hasFocus) {

super.getTreeCellRendererComponent(tree, value,sel, expanded, leaf, row, hasFocus);

if (value instanceof Boolean){
setText("Retrieving data...");
//Debug.print("once more!");
}
else if ((value instanceof IconData)||(value instanceof String)){
IconData idata = (IconData)value;
if(!leaf){
if (hasFocus)
setIcon(idata.getExpandedIcon());
else
setIcon(idata.getIcon());
}
}
else
setIcon(null);
this.setPreferredSize(new Dimension(this.getIcon().getIconWidth(),this.getIcon().getIconHeight()));
return this;
}

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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