📄 csdn技术中心 jtree实现的系统文件目录树.htm
字号:
<TR>
<TD align=right><A
href="http://dev.csdn.net/articlelist.aspx?c=13">其他</A> (<A
href="http://www.csdn.net/Rss/RssFeed.aspx?rssid=1&bigclassid=13">rss</A>) </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=1 cellPadding=0 width="100%" bgColor=#000000
border=0><TBODY>
<TR>
<TD vAlign=top bgColor=#ededed>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=right
background="CSDN技术中心 JTree实现的系统文件目录树.files/top.gif"
height=27><FONT
class=fonttitle>积极原创作者 </FONT></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width="100%" border=0>
<TBODY>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/kekeblog">kekeblog</A> <FONT
color=#ff0000>(1)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/wti">wti</A> <FONT
color=#ff0000>(4)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/8988">8988</A> <FONT
color=#ff0000>(1)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/Lightest">Lightest</A> <FONT
color=#ff0000>(4)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/inelm">inelm</A> <FONT
color=#ff0000>(7)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/lijiecong">lijiecong</A> <FONT
color=#ff0000>(1)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/yqj2065">yqj2065</A> <FONT
color=#ff0000>(7)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/apexchu">apexchu</A> <FONT
color=#ff0000>(10)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/TechnoFantasy">TechnoFantasy</A> <FONT
color=#ff0000>(42)</FONT></TD></TR>
<TR>
<TD align=right><A
href="http://dev.csdn.net/user/Passants">Passants</A> <FONT
color=#ff0000>(5)</FONT></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD>
<TD vAlign=top width=786>
<TABLE cellPadding=4 width="100%" border=0>
<TBODY>
<TR>
<TD width=10 height=19></TD>
<TD height=19>
<TABLE cellSpacing=3 cellPadding=3 width="100%" border=0>
<TBODY>
<TR>
<TD><A href="http://www.csdn.net/">CSDN</A> - <A
href="http://dev.csdn.net/">文档中心</A> - <FONT color=#003399><A
id=ArticleTitle1_ArticleTitle1_hlClass
href="http://dev.csdn.net/articlelist.aspx?c=6">Java</A></FONT>
</TD>
<TD align=right>阅读:<SPAN
id=ArticleTitle1_ArticleTitle1_lblReadCount>2466</SPAN>
评论: <SPAN
id=ArticleTitle1_ArticleTitle1_lblCommentCount>0</SPAN>
<A
href="http://dev.csdn.net/article/33/33541.shtm#Comment">参与评论</A>
</TD></TR></TBODY></TABLE>
<TABLE cellSpacing=3 cellPadding=3 width="100%" bgColor=#eeeeee
border=0>
<TBODY>
<TR>
<TD noWrap width=60 height=0></TD>
<TD></TD></TR>
<TR>
<TD noWrap align=middle bgColor=#003399 height=16><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> <SPAN
id=ArticleTitle1_ArticleTitle1_lblTitle>JTree实现的系统文件目录树</SPAN></B>
选择自 <A id=ArticleTitle1_ArticleTitle1_AuthorLink
href="http://dev.csdn.net/user/jackkui">jackkui</A> 的 Blog
</TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=16><FONT
color=#ffffff>关键字</FONT></TD>
<TD width=500> <SPAN
id=ArticleTitle1_ArticleTitle1_lblKeywords>JTree实现的系统文件目录树</SPAN></TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=16><FONT
color=#ffffff>出处</FONT></TD>
<TD> <SPAN
id=ArticleTitle1_ArticleTitle1_lblSource></SPAN></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD width=10></TD>
<TD><SPAN id=ArticleContent1_ArticleContent1_lblContent>
<P>看到很多人问关于用JTree实现资源管理器的方法,在这里我提供一个例子共大家参考,这个例子虽然也是转自其他书,但是JTree的用法,我掌握的差不多了,如果哪位朋友,对此例子有问题,我愿意与他交流。我的email是:<A
href="mailto:jack_kui@126.com">jack_kui@126.com</A>这个例子涵盖了JTree的大部分属性,希望对大家能够有所帮助。需要一些图标文件,请大家自己复制好相同名字的gif图片放到目录下。</P>
<P>下面是源代码:</P>
<P>import java.awt.*;<BR>import java.awt.event.*;<BR>import
java.io.*;<BR>import java.util.*;</P>
<P>import javax.swing.*;<BR>import javax.swing.tree.*;<BR>import
javax.swing.event.*;</P>
<P>public class FileTree1 <BR> extends JFrame
<BR>{<BR> public static final ImageIcon ICON_COMPUTER =
<BR> new ImageIcon("computer.gif");<BR> public
static final ImageIcon ICON_DISK = <BR> new
ImageIcon("disk.gif");<BR> public static final ImageIcon
ICON_FOLDER = <BR> new
ImageIcon("folder.gif");<BR> public static final ImageIcon
ICON_EXPANDEDFOLDER = <BR> new
ImageIcon("expandedfolder.gif");</P>
<P> protected JTree m_tree;<BR> protected
DefaultTreeModel m_model;<BR> protected JTextField
m_display;</P>
<P> public
FileTree1()<BR> {<BR> super("Directories
Tree");<BR> setSize(400, 300);</P>
<P> DefaultMutableTreeNode top = new
DefaultMutableTreeNode(<BR> new
IconData(ICON_COMPUTER, null, "Computer"));</P>
<P> DefaultMutableTreeNode node;<BR> File[]
roots = File.listRoots();<BR> for (int k=0;
k<roots.length; k++)<BR> {<BR> node =
new DefaultMutableTreeNode(new IconData(ICON_DISK,
<BR> null, new
FileNode(roots[k])));<BR> top.add(node);<BR>
node.add( new DefaultMutableTreeNode(new
Boolean(true)));<BR> }</P>
<P> m_model = new
DefaultTreeModel(top);<BR> m_tree = new
JTree(m_model);</P>
<P>
m_tree.putClientProperty("JTree.lineStyle", "Angled");</P>
<P> TreeCellRenderer renderer = new
<BR> IconCellRenderer();<BR> m_tree.setCellRenderer(renderer);</P>
<P> m_tree.addTreeExpansionListener(new
<BR> DirExpansionListener());</P>
<P> m_tree.addTreeSelectionListener(new
<BR> DirSelectionListener());</P>
<P> m_tree.getSelectionModel().setSelectionMode(<BR> TreeSelectionModel.SINGLE_TREE_SELECTION);
<BR> m_tree.setShowsRootHandles(true);
<BR> m_tree.setEditable(false);</P>
<P> JScrollPane s = new
JScrollPane();<BR> s.getViewport().add(m_tree);<BR> getContentPane().add(s,
BorderLayout.CENTER);</P>
<P> m_display = new
JTextField();<BR> m_display.setEditable(false);<BR> getContentPane().add(m_display,
BorderLayout.NORTH);</P>
<P> WindowListener wndCloser = new
WindowAdapter()<BR> {<BR> public void
windowClosing(WindowEvent e)
<BR> {<BR> System.exit(0);<BR> }<BR> };<BR> addWindowListener(wndCloser);<BR> <BR> setVisible(true);<BR> }</P>
<P> DefaultMutableTreeNode getTreeNode(TreePath
path)<BR> {<BR> return
(DefaultMutableTreeNode)(path.getLastPathComponent());<BR> }</P>
<P> FileNode getFileNode(DefaultMutableTreeNode
node)<BR> {<BR> if (node ==
null)<BR> return null;<BR> Object obj =
node.getUserObject();<BR> if (obj instanceof
IconData)<BR> obj =
((IconData)obj).getObject();<BR> if (obj instanceof
FileNode)<BR> return
(FileNode)obj;<BR> else<BR> return
null;<BR> }</P>
<P> // Make sure expansion is threaded and
updating the tree model<BR> // only occurs within
the event dispatching thread.<BR> class
DirExpansionListener implements
TreeExpansionListener<BR>
{<BR> public void
treeExpanded(TreeExpansionEvent
event)<BR>
{<BR>
final DefaultMutableTreeNode node =
getTreeNode(<BR>
event.getPath());<BR>
final FileNode fnode = getFileNode(node);</P>
<P>
Thread runner = new Thread()
<BR>
{<BR>
public void run()
<BR>
{<BR>
if (fnode != null && fnode.expand(node))
<BR>
{<BR>
Runnable runnable = new Runnable()
<BR>
{<BR>
public void run()
<BR>
{<BR>
m_model.reload(node);<BR>
}<BR>
};<BR>
SwingUtilities.invokeLater(runnable);<BR>
}<BR>
}<BR>
};<BR>
runner.start();<BR> }</P>
<P> public void
treeCollapsed(TreeExpansionEvent event) {}<BR>
}</P>
<P><BR> class DirSelectionListener <BR> implements
TreeSelectionListener <BR> {<BR> public void
valueChanged(TreeSelectionEvent
event)<BR> {<BR> DefaultMutableTreeNode
node =
getTreeNode(<BR> event.getPath());<BR> FileNode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -