2015-10-18 71 views
0

我想知道在文件上传到FTP服务器后是否有更新JList的方法。Java,上传文件后更新Jlist

例如:当我登录时,它会加载JList并获取文件。当您上传文件时,会上传,但列表不会更新。

这里是我的代码:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.ListSelectionModel; 

import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 

public class MyClass{ 

public static JFrame frame = new JFrame(); 
public static JPanel panel = new JPanel(); 
public static JButton download = new JButton(); 
public static JButton upload = new JButton(); 
public static JButton login = new JButton(); 
public static JButton close = new JButton(); 
public static JLabel label = new JLabel(); 
public static JList list = new JList(); 
public static JFileChooser chooser = new JFileChooser(); 
public static FTPClient ftpClient = new FTPClient(); 
public static JDialog dialog = new JDialog(); 
public static String[] files; 

public static void main(String args[]){ 

    //JFrame, frame 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setSize(500, 500); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 

    //JPanel, panel 
    panel.setLayout(null); 
    frame.add(panel); 

    //JButton, upload 
    upload.setBounds(25, 25, 90, 30); 
    upload.setText("Upload"); 

    //JButton, download 
    download.setBounds(25, 60, 90, 30); 
    download.setText("Download"); 

    //JButton, login 
    login.setBounds(25, 95, 90, 30); 
    login.setText("Login"); 
    panel.add(login); 

    //JButton, close 
    close.setBounds(25, 95, 90, 30); 
    close.setText("Close"); 

    //JLabel, label 
    label.setBounds(25, 435, 500, 20); 
    panel.add(label); 

    //JList, list 


    //Login 
    login.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent arg0) { 
      Login(); 
     } 

    }); 

    //Logout and close 
    close.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) { 
      Close(); 
     } 
    }); 

    //Upload 
    upload.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 

       chooser.setCurrentDirectory(new File(System.getProperty("user.home"))); 
       int result = chooser.showOpenDialog(dialog); 
       if (result == JFileChooser.APPROVE_OPTION) { 
        File selectedFile = chooser.getSelectedFile(); 
        label.setText("Selected file: " + selectedFile.getAbsolutePath()); 
       }else{ 
        label.setText("Cancelled"); 
       } 

       Upload(); 





     } 
    }); 

} 



public static void Login(){ 
     String server = "X"; 
     int port = X;   //Crossed out for 
     String user = "X"; //Purposes 
     String pass = "X"; 



      try{ 
      ftpClient.connect(server, port); 
      ftpClient.login(user, pass); 
      ftpClient.enterLocalPassiveMode(); 

      if(ftpClient.isConnected()){ 
       label.setText("Connected to: " + server); 
       panel.remove(login); 
       panel.add(close); 
       panel.add(upload); 
       panel.add(download); 

       files = ftpClient.listNames(); 

       list = new JList(files); //data has type Object[] 
       list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
       list.setLayoutOrientation(JList.VERTICAL); 
       list.setVisibleRowCount(20); 

       list.setLayout(null); 
       list.setBounds(130, 25, 340, 400); 
       panel.add(list); 
       frame.repaint(); 
      } 

      }catch(IOException e){ 
       System.out.println("ERROR: Can't connect"); 
      } 

} 


public static void Close(){ 

    try{ 
    ftpClient.logout(); 
    ftpClient.disconnect(); 
    System.exit(1); 
    }catch(IOException e){ 
     System.exit(1); 
    } 

} 


public static void Upload(){ 

     try { 

      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

      // APPROACH #1: uploads first file using an InputStream 
      File selectedFile = chooser.getSelectedFile(); 

      String firstRemoteFile = selectedFile.getName(); 
      InputStream inputStream = new FileInputStream(selectedFile); 

      label.setText("Uploading..."); 
      boolean done = ftpClient.storeFile(firstRemoteFile, inputStream); 
      inputStream.close(); 
      if (done) { 
       label.setText("Finished."); 

      } 

     } catch (IOException ex) { 
      System.out.println("Error: " + ex.getMessage()); 
      ex.printStackTrace(); 
     } 

} 

} 
+0

JList不会更新,因为您永远不会更改它所保存的数据。它不会奇迹般地自行改变。 –

回答

2

JList的不更新,因为你永远不会改变它。您需要在文件上传后重新加载更新的数据,这可以类似于最初使用数据加载列表的方式完成,只是您不想创建新的JList,而是创建一个新的DefaultListModel,一个保存新数据,然后通过JList的setModel(...)方法将其添加到JList。

其他方面的问题:

  • 你的代码显示了显著过度使用static修饰符的,实际上最重要的是你的方法和字段应非静态实例字段和方法,因为这将帮助您创建更简单,更可重用的代码。
  • 您将需要学习和遵循Java命名约定,包括给出以小写字母和类名开头且大写字母开头的变量和方法名称。遵循约定将使您的代码对其他人更容易理解,包括我们。
  • 您正在使用null布局和setBounds,并且这会绑定您的代码。 Swing GUI的构建并非像素完美,绝对定位的使用意味着您的GUI几乎可以保证在除少数操作系统和屏幕分辨率之外都很难看,并且会使您的GUI布局非常难以修改或增强。这是一个普通的Swing新手错误,认为使用空布局会使GUI创建变得更容易,因为从长远来看,它恰恰相反。