2012-07-09 100 views
1

我正在使用FTP4j,我想用FTP服务器的根目录填充JTree。我试过使用FTP4j的currentDirectory()方法,但只返回一个没用的“/”。我也尝试将ftp:// url传递给初始化JTree的方法,该方法也不起作用。这是我的第一个Swing程序,所以我有点不知所措。下面是代码:如何使用FTP目录填充JTree?

package net.emptybox.ui; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import net.miginfocom.swing.MigLayout; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import java.awt.FlowLayout; 
import java.awt.BorderLayout; 
import javax.swing.BoxLayout; 
import javax.swing.JSplitPane; 
import javax.swing.JSeparator; 
import javax.swing.JTree; 
import javax.swing.JTextArea; 
import java.awt.Component; 
import java.io.File; 

import net.emptybox.ui.FTP; 

import javax.swing.Box; 
import javax.swing.event.TreeSelectionEvent; 
import javax.swing.event.TreeSelectionListener; 
import javax.swing.JScrollPane; 
import javax.swing.ScrollPaneConstants; 



public class GUI { 

    static JFrame frame; 
    static private JSplitPane splitPane; 
    static private JLabel lblServer; 
    static private JTextField serverField; 
    static private JLabel lblPort; 
    static private JTextField portField; 
    static private JLabel lblUsername; 
    static private JTextField usernameField; 
    static private JLabel lblPassword; 
    static private JTextField passwordField; 
    static private JButton connectButton; 
    static private JSeparator separator; 
    static private JTextArea detailArea; 
    static private JButton downloadButton; 
    static private JButton uploadButton; 
    static private Component horizontalGlue; 
    static private JTextField fileField; 
    static private JButton goButton; 
    static private Component horizontalGlue_1; 

    static FileSystemModel fileSystemModel; 
    static JLabel consoleLabel; 
    private static Component verticalGlue; 
    private static JScrollPane scrollPane; 
    static JTree fileTree; 

    /** 
    * Launch the application. 
    */ 

    /** 
    * Create the application. 
    */ 
    public GUI() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    public static void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 648, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(new MigLayout("", "[grow]", "[][][][grow][]")); 

     lblServer = new JLabel("Server:"); 
     frame.getContentPane().add(lblServer, "flowx,cell 0 0"); 

     consoleLabel = new JLabel(""); 
     frame.getContentPane().add(consoleLabel, "flowx,cell 0 1"); 

     separator = new JSeparator(); 
     frame.getContentPane().add(separator, "cell 0 2"); 

     splitPane = new JSplitPane(); 
     splitPane.setOneTouchExpandable(true); 
     splitPane.setContinuousLayout(true); 
     frame.getContentPane().add(splitPane, "cell 0 3,grow"); 

     detailArea = new JTextArea(); 
     detailArea.setEditable(false); 
     splitPane.setRightComponent(detailArea); 

     scrollPane = new JScrollPane(); 
     scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     splitPane.setLeftComponent(scrollPane); 

     serverField = new JTextField(); 
     frame.getContentPane().add(serverField, "cell 0 0,growx"); 

     lblPort = new JLabel("Port:"); 
     frame.getContentPane().add(lblPort, "cell 0 0"); 

     portField = new JTextField(); 
     frame.getContentPane().add(portField, "cell 0 0,growx"); 

     lblUsername = new JLabel("Username:"); 
     frame.getContentPane().add(lblUsername, "cell 0 0"); 

     usernameField = new JTextField(); 
     frame.getContentPane().add(usernameField, "cell 0 0,growx"); 

     lblPassword = new JLabel("Password:"); 
     frame.getContentPane().add(lblPassword, "cell 0 0"); 

     passwordField = new JTextField(); 
     frame.getContentPane().add(passwordField, "cell 0 0,growx"); 


     connectButton = new JButton("Connect"); 
     frame.getContentPane().add(connectButton, "cell 0 0"); 

     if (serverField.getText() == null || usernameField.getText() == null || passwordField.getText() == null) { 
      connectButton.disable(); 
     } else { 
      connectButton.enable(); 
     } 

     if (portField.getText() == null) { 
      portField.setText("21"); 
     } 

     connectButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       FTP.connect(serverField.getText(), portField.getText(), usernameField.getText(), passwordField.getText()); 
      } 
     }); 

     downloadButton = new JButton("Download"); 
     frame.getContentPane().add(downloadButton, "flowx,cell 0 4"); 
     downloadButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      } 
     }); 

     uploadButton = new JButton("Upload"); 
     frame.getContentPane().add(uploadButton, "cell 0 4"); 
     uploadButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      } 
     }); 

     horizontalGlue_1 = Box.createHorizontalGlue(); 
     frame.getContentPane().add(horizontalGlue_1, "cell 0 4,growx"); 

     fileField = new JTextField(); 
     frame.getContentPane().add(fileField, "cell 0 4"); 
     fileField.setColumns(200); 

     goButton = new JButton("Go"); 
     goButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      } 
     }); 
     frame.getContentPane().add(goButton, "cell 0 4"); 

     horizontalGlue = Box.createHorizontalGlue(); 
     frame.getContentPane().add(horizontalGlue, "cell 0 4,alignx leading"); 

     verticalGlue = Box.createVerticalGlue(); 
     frame.getContentPane().add(verticalGlue, "cell 0 1"); 
    } 

    private String getFileDetails(File file) { 
     if (file == null) 
      return ""; 
     StringBuffer buffer = new StringBuffer(); 
     buffer.append("Name: " + file.getName() + "\n"); 
     buffer.append("Path: " + file.getPath() + "\n"); 
     buffer.append("Size: " + file.length() + "\n"); 
     return buffer.toString(); 
     } 

    public static void populateTree(String directory) { 
     fileSystemModel = new FileSystemModel(new File(directory)); 

     fileTree = new JTree(fileSystemModel); 
     scrollPane.setViewportView(fileTree); 
    } 
} 

填充树被另一个类调用当连接成功与服务器建立的用户登录

+0

执行'TreeModel',如[Q&A](http://codereview.stackexchange.com/q/4446/6692)中所述。 – trashgod 2012-07-09 20:29:22

回答

0

你会想创建自己的TreeModel 。

一旦用户连接成功,您需要查询站点以获取更多信息,默认实现中没有可用的模型,可以实现您想要的功能,但您必须为其工作;)

让我们从FTP端开始。

String current = ftpSite.getCurrentDirectory(); // Just in case you want to come back 
ftpSite.changeDirectort("/"); // Move to the root directory 
FPTFile[] fileList = ftpSite.list(); // Get the list of files. 

现在,你如何去做这件事取决于你。但基本上,我会构建一个MutableTreeNode作为根节点(用于JTree)。

从那里您可以根据需要向根添加新的(MutableTreeNode)节点。

你可以查看http://www.jroller.com/Thierry/entry/swing_lazy_loading_in_a了解更多关于如何实现所有这些的想法。