2015-12-11 92 views
1

的文件夾下選擇它。我設法使用stackoverflow示例,JTree顯示所有系統驅動器和文件夾,希望顯示文件夾中的所有相應文件以及,所有的文件名都在一個循環中需要添加它們,這就是我卡住的地方!顯示文件的名稱,並在JTree

請給我指示,在文件夾下添加文件,謝謝!

CODE:

public class viewGui extends JFrame { 

    private FileSystemView fileSystemView; 
    private Desktop desktop; 
    private static final long serialVersionUID = 1083130296343096642L; 
    public static JTree tree; 
    private DefaultTreeModel treeModel; 
    private JTable table; 
    private ListSelectionListener listSelectionListener; 

    private static final LayoutManager H = new GridLayout(1, 0); 
    private static final LayoutManager V = new GridLayout(0, 1); 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        viewGui mainWindow = new viewGui(); 
        mainWindow.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public viewGui() { 

     fileSystemView = FileSystemView.getFileSystemView(); 
     desktop = Desktop.getDesktop(); 

     this.setTitle("Student Record Book"); 
     getContentPane().setLayout(H); 
     getContentPane().setLayout(V); 
     this.setPreferredSize(new Dimension(1200, 800)); 
     this.setExtendedState(NORMAL); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.getContentPane().setLayout(null); 
     this.setResizable(true); 

     DefaultMutableTreeNode root = new DefaultMutableTreeNode(); 
     treeModel = new DefaultTreeModel(root); 

     TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { 
      public void valueChanged(TreeSelectionEvent tse){ 
       DefaultMutableTreeNode node = (DefaultMutableTreeNode)tse.getPath().getLastPathComponent(); 
       System.out.println("Node: "+node); 
       showChildren(node); 
      } 
     }; 

     File[] roots = fileSystemView.getRoots(); 
     for (File fileSystemRoot : roots) { 
      DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot); 
      root.add(node); 
      File[] files = fileSystemView.getFiles(fileSystemRoot, true); 
      for (File file : files) { 
       if (file.isDirectory()) { 
        node.add(new DefaultMutableTreeNode(file)); 
       } 
      } 
     } 

     tree = new JTree(treeModel); 
     tree.setBounds(10, 11, 387, 740); 
     tree.setRootVisible(false); 
     tree.addTreeSelectionListener(treeSelectionListener); 
     tree.expandRow(0); 
     JScrollPane treeScroll = new JScrollPane(tree); 

     tree.setVisibleRowCount(15); 

     Dimension preferredSize = treeScroll.getPreferredSize(); 
     Dimension widePreferred = new Dimension(200,(int)preferredSize.getHeight()); 
     treeScroll.setPreferredSize(widePreferred); 

     this.setLayout(H); 
     this.validate(); 
     this.add(treeScroll, BorderLayout.WEST); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 

     table = new JTable(); 
     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     table.setAutoCreateRowSorter(true); 
     table.setShowVerticalLines(false); 

     listSelectionListener = new ListSelectionListener() { 
      @Override 
      public void valueChanged(ListSelectionEvent lse) { 
       int row = table.getSelectionModel().getLeadSelectionIndex(); 
      } 
     }; 

     table.getSelectionModel().addListSelectionListener(listSelectionListener); 
     JScrollPane tableScroll = new JScrollPane(table); 
     Dimension d = tableScroll.getPreferredSize(); 
     tableScroll.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()/2)); 
     getContentPane().add(tableScroll, BorderLayout.CENTER); 


    } 

    private void showChildren(final DefaultMutableTreeNode node) { 
     tree.setEnabled(false); 

     SwingWorker<Void, File> worker = new SwingWorker<Void, File>() { 
      @Override 
      public Void doInBackground() { 
       File file = (File) node.getUserObject(); 
       if (file.isDirectory()) { 
        File[] files = fileSystemView.getFiles(file, true); //!! 
        if (node.isLeaf()) { 
         for (File child : files) { 

          System.out.println("child:"+child); 
          if (child.isDirectory()) { 
           publish(child); 
           //Need to add the file names under the folder 
          } 
         } 
        } 
       } 
       return null; 
      } 

      @Override 
      protected void process(List<File> chunks) { 
       for (File child : chunks) { 
        node.add(new DefaultMutableTreeNode(child)); 
        if (child.isDirectory()){ 

        } 
       } 
      } 

      @Override 
      protected void done() { 
       tree.setEnabled(true); 
      } 
     }; 
     worker.execute(); 
    } 
} 
+0

我不認爲我正確理解了這個問題,但是如果我這樣做:只要刪除包含'.isDirectory();'的if語句。 –

+0

Thanks @LuxxMiner,如果我用.isDirectory()刪除if語句,JTree只顯示c:\驅動器,但我希望所有系統驅動器,文件夾和文件都顯示在JTree上, t正確地構建問題 –

+0

爲什麼只包含此[動態佈局代碼](http://stackoverflow.com/a/5751044/230513)以後'setLayout(null)'?如[必填](http://stackexchange.com/legal),請引用其他使用的示例。 – trashgod

回答

7

不要在一個循環中添加的所有文件名。相反,創建一個FileTreeModel,實現TreeModel,如here所示。該實現只需調用getChild()getIndexOfChild()中的File方法listFiles()。然後,您可以創建一棵樹並展開任何所需的行;如here所示使用setSelectionPath()

TreeModel model = new FileTreeModel(new File(System.getProperty("user.dir"))); 
JTree tree = new JTree(model); 
tree.expandRow(0); 

image

我只得到了c:\;請告訴我怎麼把所有的系統驅動器等

你可以得到文件系統的根目錄的列表與File.listRoots(),如圖Find all drive letters in Java,或FileSystemView#getRoots(),如圖File Browser GUI

+0

Thanks @trashgod,我嘗試了一個[簡單示例](http://pastebin.com/9kFfhXQY)根據您的建議,但我只得到c:\,請給我指示以獲取所有系統驅動器,文件夾和文件請! –

+0

我已經詳細闡述過了。 – trashgod

+0

謝謝@trashgod,將在此工作。謝謝 –

相關問題