📄 csdn技术中心 jtree实现的系统文件目录树.htm
字号:
fnode = getFileNode(node);<BR> if (fnode !=
null)<BR> m_display.setText(fnode.getFile().<BR> getAbsolutePath());<BR> else<BR> m_display.setText("");<BR> }<BR> }</P>
<P> public static void main(String argv[])
<BR> {<BR> new FileTree1();<BR> }<BR>}</P>
<P>class IconCellRenderer <BR> extends JLabel
<BR> implements TreeCellRenderer<BR>{<BR> protected Color
m_textSelectionColor;<BR> protected Color
m_textNonSelectionColor;<BR> protected Color
m_bkSelectionColor;<BR> protected Color
m_bkNonSelectionColor;<BR> protected Color
m_borderSelectionColor;</P>
<P> protected boolean m_selected;</P>
<P> public
IconCellRenderer()<BR> {<BR> super();<BR> m_textSelectionColor
=
UIManager.getColor(<BR> "Tree.selectionForeground");<BR> m_textNonSelectionColor
=
UIManager.getColor(<BR> "Tree.textForeground");<BR> m_bkSelectionColor
=
UIManager.getColor(<BR> "Tree.selectionBackground");<BR> m_bkNonSelectionColor
=
UIManager.getColor(<BR> "Tree.textBackground");<BR> m_borderSelectionColor
=
UIManager.getColor(<BR> "Tree.selectionBorderColor");<BR> setOpaque(false);<BR> }</P>
<P> public Component getTreeCellRendererComponent(JTree tree,
<BR> Object value, boolean sel, boolean expanded, boolean
leaf, <BR> int row, boolean hasFocus)
<BR> <BR> {<BR> DefaultMutableTreeNode
node =
<BR> (DefaultMutableTreeNode)value;<BR> Object
obj =
node.getUserObject();<BR> setText(obj.toString());</P>
<P>
if (obj instanceof
Boolean)<BR>
setText("Retrieving data...");</P>
<P> if (obj instanceof
IconData)<BR> {<BR> IconData idata =
(IconData)obj;<BR> if
(expanded)<BR> setIcon(idata.getExpandedIcon());<BR> else<BR> setIcon(idata.getIcon());<BR> }<BR> else<BR> setIcon(null);</P>
<P> setFont(tree.getFont());<BR> setForeground(sel
? m_textSelectionColor :
<BR> m_textNonSelectionColor);<BR> setBackground(sel
? m_bkSelectionColor :
<BR> m_bkNonSelectionColor);<BR> m_selected
= sel;<BR> return this;<BR> }<BR>
<BR> <BR> public void paintComponent(Graphics g)
<BR> {<BR> Color bColor =
getBackground();<BR> Icon icon = getIcon();</P>
<P> g.setColor(bColor);<BR> int offset =
0;<BR> if(icon != null && getText() != null)
<BR> offset = (icon.getIconWidth() +
getIconTextGap());<BR> g.fillRect(offset, 0, getWidth() -
1 - offset,<BR> getHeight() -
1);<BR> <BR> if (m_selected)
<BR> {<BR> g.setColor(m_borderSelectionColor);<BR> g.drawRect(offset,
0, getWidth()-1-offset,
getHeight()-1);<BR> }<BR> super.paintComponent(g);<BR>
}<BR>}</P>
<P>class IconData<BR>{<BR> protected Icon
m_icon;<BR> protected Icon
m_expandedIcon;<BR> protected Object m_data;</P>
<P> public IconData(Icon icon, Object
data)<BR> {<BR> m_icon =
icon;<BR> m_expandedIcon = null;<BR> m_data =
data;<BR> }</P>
<P> public IconData(Icon icon, Icon expandedIcon, Object
data)<BR> {<BR> m_icon =
icon;<BR> m_expandedIcon =
expandedIcon;<BR> m_data = data;<BR> }</P>
<P> public Icon getIcon() <BR> { <BR> return
m_icon;<BR> }</P>
<P> public Icon getExpandedIcon() <BR> {
<BR> return m_expandedIcon!=null ? m_expandedIcon :
m_icon;<BR> }</P>
<P> public Object getObject() <BR> {
<BR> return m_data;<BR> }</P>
<P> public String toString() <BR> { <BR> return
m_data.toString();<BR> }<BR>}</P>
<P>class FileNode<BR>{<BR> protected File m_file;</P>
<P> public FileNode(File file)<BR> {<BR> m_file
= file;<BR> }</P>
<P> public File getFile() <BR> { <BR> return
m_file;<BR> }</P>
<P> public String toString() <BR> { <BR> return
m_file.getName().length() > 0 ? m_file.getName() :
<BR> m_file.getPath();<BR> }</P>
<P> public boolean expand(DefaultMutableTreeNode
parent)<BR> {<BR> DefaultMutableTreeNode flag =
<BR> (DefaultMutableTreeNode)parent.getFirstChild();<BR> if
(flag==null) // No flag<BR> return
false;<BR> Object obj =
flag.getUserObject();<BR> if (!(obj instanceof
Boolean))<BR> return
false; // Already expanded</P>
<P> parent.removeAllChildren(); // Remove Flag</P>
<P> File[] files = listFiles();<BR> if (files
== null)<BR> return true;</P>
<P> Vector v = new Vector();</P>
<P> for (int k=0; k<files.length;
k++)<BR> {<BR> File f =
files[k];<BR> if
(!(f.isDirectory()))<BR> continue;</P>
<P> FileNode newNode = new
FileNode(f);<BR> <BR> boolean
isAdded = false;<BR> for (int i=0; i<v.size();
i++)<BR> {<BR> FileNode nd =
(FileNode)v.elementAt(i);<BR> if
(newNode.compareTo(nd) <
0)<BR> {<BR> v.insertElementAt(newNode,
i);<BR> isAdded =
true;<BR> break;<BR> }<BR> }<BR> if
(!isAdded)<BR> v.addElement(newNode);<BR> }</P>
<P> for (int i=0; i<v.size();
i++)<BR> {<BR> FileNode nd =
(FileNode)v.elementAt(i);<BR> IconData idata = new
IconData(FileTree1.ICON_FOLDER,
<BR> FileTree1.ICON_EXPANDEDFOLDER,
nd);<BR> DefaultMutableTreeNode node = new
<BR> DefaultMutableTreeNode(idata);<BR> parent.add(node);<BR> <BR> if
(nd.hasSubDirs())<BR> node.add(new
DefaultMutableTreeNode( <BR> new
Boolean(true) ));<BR> }</P>
<P> return true;<BR> }</P>
<P> public boolean
hasSubDirs()<BR> {<BR> File[] files =
listFiles();<BR> if (files ==
null)<BR> return false;<BR> for (int
k=0; k<files.length;
k++)<BR> {<BR> if
(files[k].isDirectory())<BR> return
true;<BR> }<BR> return
false;<BR> }<BR> <BR> public int compareTo(FileNode
toCompare)<BR> { <BR> return
m_file.getName().compareToIgnoreCase(<BR> toCompare.m_file.getName()
); <BR> }</P>
<P> protected File[] listFiles()<BR> {<BR> if
(!m_file.isDirectory())<BR> return
null;<BR> try<BR> {<BR> return
m_file.listFiles();<BR> }<BR> catch (Exception
ex)<BR> {<BR> JOptionPane.showMessageDialog(null,
<BR> "Error reading directory
"+m_file.getAbsolutePath(),<BR> "Warning",
JOptionPane.WARNING_MESSAGE);<BR> return
null;<BR> }<BR> }<BR>}<BR></P></SPAN><BR>
<DIV
style="FONT-SIZE: 14px; LINE-HEIGHT: 25px"><STRONG>作者Blog:</STRONG><A
id=ArticleContent1_ArticleContent1_AuthorBlogLink
href="http://blog.csdn.net/jackkui/"
target=_blank>http://blog.csdn.net/jackkui/</A></DIV>
<DIV
style="FONT-SIZE: 14px; COLOR: #900; LINE-HEIGHT: 25px"><STRONG>相关文章</STRONG></DIV>
<TABLE id=ArticleContent1_ArticleContent1_RelatedArticles
style="BORDER-COLLAPSE: collapse" cellSpacing=0 border=0>
<TBODY>
<TR>
<TD><A
href="http://dev.csdn.net/article/33/article/37/37853.shtm">TreeCellRenderer使用方法简介</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/33/article/37/37851.shtm">DOM基础&实例</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/33/article/37/37494.shtm">使用SAX把XML转换成JTree</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/33/article/33/33786.shtm">ResultSet概论</A>
</TD></TR>
<TR>
<TD><A
href="http://dev.csdn.net/article/33/article/33/33541.shtm">JTree实现的系统文件目录树</A>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><A name=#Comment></A>
<TABLE cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=center
bgColor=#006699 border=0>
<TBODY>
<TR bgColor=#006699>
<TD id=white align=middle width=556 bgColor=#006699><FONT
color=#ffffff>对该文的评论</FONT> </TD></TR></TBODY></TABLE>
<DIV align=right><A id=CommnetList1_CommnetList1_Morelink
href="http://comment.csdn.net/Comment.aspx?c=2&s=33541">【评论】</A>
<A id=CommnetList1_CommnetList1_Hyperlink1
href="javascript:window.close();">【关闭】</A>
</DIV><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM><!-- 版权 -->
<HR align=center width=770 noShade SIZE=1>
<TABLE cellSpacing=0 cellPadding=0 width=500 align=center border=0>
<TBODY>
<TR>
<TD vAlign=bottom align=middle height=10><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
height=48 src="CSDN技术中心 JTree实现的系统文件目录树.files/biaoshi.gif" width=40
border=0></A></TD></TR>
<TR>
<TD vAlign=top align=middle>北京百联美达美数码科技有限公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.NET, Inc. All Rights
Reserved</FONT></TD></TR>
<TR>
<TD height=15></TD></TR></TBODY></TABLE><!-- /版权 -->
<SCRIPT>
document.write("<img src=http://count.csdn.net/count/pageview1.asp?columnid=4&itemid=11 border=0 width=0 height=0>");
</SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -