我想知道在将文件上传到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();
}
}
}
答案 0 :(得分:2)
JList不会更新,因为您永远不会更改它。您需要在上传文件后重新加载更新的数据,这可以类似于您最初使用数据加载列表的方式,只是您不想创建新的JList,而是新的DefaultListModel ,一个保存新数据,然后通过JList的setModel(...)
方法添加到JList。
其他方面问题: