Java,在上传文件后更新Jlist

时间:2015-10-18 03:38:50

标签: java swing file-io jlist

我想知道在将文件上传到FTP服务器后是否有更新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();
        } 

}

}

1 个答案:

答案 0 :(得分:2)

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

其他方面问题:

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