System file directory tree implemented by JTree

xiaoxiao2021-03-06  126

Seeing a lot of people asking a way to implement the resource manager with JTREE, here I provide an example of a total of everyone's reference, this example although it is also from other books, but JTree's usage, I have the same thing, if a friend, right This example has a problem, I am willing to communicate with him. My email is: jack_kui@126.com This example covers most of the attributes of JTree, I hope to help everyone. I need some icon files, please copy the GIF image of the same name to the directory.

Here is the source code:

Import java.awt. *; import java.awt.event. *; import java.io. *; import java.util. *;

Import javax.swing. *; import javax.swing.tree. *; import javax.swing.event. *;

public class FileTree1 extends JFrame {public static final ImageIcon ICON_COMPUTER = new ImageIcon ( "computer.gif"); public static final ImageIcon ICON_DISK = new ImageIcon ( "disk.gif"); public static final ImageIcon ICON_FOLDER = new ImageIcon ( "folder. GIF "); public static final imageicon icon_expandedfolder = new imageicon (" expandedfolder.gif ");

Protected Jtree M_Tree; Protected DefaultTreeModel M_Model; protected jtextfield m_display;

Public filetree1 () {Super ("Directories Tree"); Setsize (400, 300);

DEFAULTMUTABLETREENODE TOP = New DefaultmutableTreenode (new icondata (icon_computer, null, "computer");

DefaultMutableTreeNode node; File [] roots = File.listRoots (); for (int k = 0; k

M_Model = New DefaultTreeModel (TOP); m_tree = new jtree (m_model);

M_Tree.PutClientProperty ("JTree.LineStyle", "Angled");

Treecellrenderer renderer = new iconcellrenderer (); m_tree.setcellrenderer (rendere);

m_tree.addtreeExpansionListener (New DirexPansionListener ()); M_Tree.AddtreeseLectionListener (New DiRSELECTIONLISTENER ());

m_tree.getSelectionModel (). setSelectionMode (TreeSelectionModel.single_tree_selection); m_tree.setshowsroothandles (true); m_tree.seteditable (false);

JscrollPane S = new jscrollpane (); s.getViewPort (); add (m_tree); getContentPane (). Add (s, borderlayout.center);

m_display = new jtextfield (); m_display.setedItTable (false); getContentPane (). add (m_display, borderlayout.north);

Windowlistener Wndcloser = new windowadapter () {public void window {system.exit (0);}}; addwindowlistener (wndcloser); setVisible (TRUE);

Defaultmutabletreenode gettmutable (TreePath Path) {return (Path.getLastPathComponent ());}

FileNode getFileNode (DefaultMutableTreeNode node) {if (node ​​== null) return null; Object obj = node.getUserObject (); if (obj instanceof IconData) obj = ((IconData) obj) .getObject (); if (obj instanceof FileNode ) Rturn (filenode) Obj; else return null;}

// Make sure expansion is threaded and updating the tree model // only occurs within the event dispatching thread class DirExpansionListener implements TreeExpansionListener {public void treeExpanded (TreeExpansionEvent event) {final DefaultMutableTreeNode node = getTreeNode (event.getPath ());. Final FileNode FNode = getfilenode (node);

THREAD Runner = new thread () {public void Run () {if (fnode! = Null && fnode.expand (node)) {runnable runnable = new runnable () {public void run () {m_model.reload (node); }}; Swingutilities.invokelater (Runnable);}}}; runner.start ();} public void treecollapsed (treeexpansionevent eve) {}}

class DirSelectionListener implements TreeSelectionListener {public void valueChanged (TreeSelectionEvent event) {DefaultMutableTreeNode node = getTreeNode (event.getPath ()); FileNode fnode = getFileNode (node); if (fnode = null!) m_display.setText (fnode.getFile (). GetabsolutePath ()); else m_display.settext ("");}}

Public static void main (string argv []) {new filetree1 ();}}

class IconCellRenderer extends JLabel implements TreeCellRenderer {protected Color m_textSelectionColor; protected Color m_textNonSelectionColor; protected Color m_bkSelectionColor; protected Color m_bkNonSelectionColor; protected Color m_borderSelectionColor;

Protected boilean m_selected;

public IconCellRenderer () {super (); m_textSelectionColor = UIManager.getColor ( "Tree.selectionForeground"); m_textNonSelectionColor = UIManager.getColor ( "Tree.textForeground"); m_bkSelectionColor = UIManager.getColor ( "Tree.selectionBackground"); m_bkNonSelectionColor = UIManager.getColor ( "Tree.textBackground"); m_borderSelectionColor = UIManager.getColor ( "Tree.selectionBorderColor"); setOpaque (false);} public Component getTreeCellRendererComponent (JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int Row, Boolean Hasfocus) {DefaultmutableTreenode Node = (DefaultmutableTreenode) Value; Object Obj = node.getuserObject (); setText (Obj.toString ());

IF (Obj InstanceOf Boolean) Settext ("Retrieving Data ...");

IF (Obj InstanceOf IconData) {icondata Idata = (ICONDATA) OBJ; Idata.Getexpandedicon ()); Else Seticon ()); Else Seticon (NULL);

setFont (tree.getFont ()); setForeground (sel m_textSelectionColor: m_textNonSelectionColor?); setBackground (sel m_bkSelectionColor:? m_bkNonSelectionColor); m_selected = sel; return this;} public void paintComponent (Graphics g) {Color bColor = getBackground (); Icon icon = geticon ();

G.SetColor; int offset = 0; if (icon! = null && gettext ()! = null) Offset = (icon.geticonwidth () geticontextGAP ()); g.FillRect (Offset, 0, getWidth ) - 1 - offset, getHeight () - 1); if (m_selected) {g.setcolor; g.drawRect (Offset, 0, getWidth () - 1-offset, GetHeight () - 1);} Super .paintcomponent (g);}}

Class iconData {protected icon m_ICON; protected icon m_expandedicon; protected object m_data;

Public iconData (icon icon, object data) {m_icon = icon; m_expandedicon = null; m_data = data;}

Public iconData (icon icon, icon, icon, icon, ibject data) {m_icon = icon; m_expandedicon = expandedicon; m_data = data;

Public icon geticon () {return m_ICON;}

Public icon getExpandedicon () {return m_expandedicon! = null? m_expandedicon: m_icon;

Public Object getObject () {return m_data;}

Public string toString () {return m_data.tostring ();}}

Class Filenode {Protected File M_File;

Public Filenode (file file) {m_file = file;}

Public File getFile () {return m_file;}

Public string toString () {return m_file.getname (). Length ()> 0? m_file.getname (): m_file.getpath ();}

public boolean expand (DefaultMutableTreeNode parent) {DefaultMutableTreeNode flag = (DefaultMutableTreeNode) parent.getFirstChild (); if (flag == null) // No flag return false; Object obj = flag.getUserObject (); if ((obj instanceof Boolean! )) Return False; // already exceeded

Parent.removeAllChildren (); // Remove Flag

File [] files = listfiles (); if (files == null) Return TRUE;

Vector v = new vector ();

For (int K = 0; k

Filenode new = new filenode (f); boolean isadded = false; for (int i = 0; i

Return True;}

Public boolean hasubdirs () {file [] files = listfiles (); if (files == null) Return False; for (int K = 0; K

protected File [] listFiles () {if (! m_file.isDirectory ()) return null; try {return m_file.listFiles ();} catch (Exception ex) {JOptionPane.showMessageDialog (null, "Error reading directory" m_file. GetabsolutePath (), "Warning", JOPANE.WARNING_MESSAGE); RETURN NULL;}}}}

转载请注明原文地址:https://www.9cbs.com/read-126173.html

New Post(0)