[英]JFileChooser show multiple selected files in JTextField
final JFileChooser fc = new JFileChooser();
File[] files = fc.getSelectedFiles();
private void showTxtFileFrame() {
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fc.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION) {
textfield1.setText(fc.getSelectedFile().getAbsolutePath());
}
我想選擇多個文件,並在我的文本字段中列出它們。 我可以選擇多個文件,但它只顯示單個文件的絕對路徑。
一方面,我將在GUI組件中顯示多個文件路徑,該路徑更適合於顯示JList等多個對象。 另外, JFileChooser API會告訴您哪個方法僅返回單個File,哪個方法返回File[]
的數組: getSelectedFiles()
。 注意s
就結束。
但是,當然,您不能將數組放入JTextField中,但是我猜測一旦獲得數據,您就知道如何處理數據。
另外,這沒有任何意義:
final JFileChooser fc = new JFileChooser();
File[] files = fc.getSelectedFiles();
由於實際上是在顯示文件選擇器對話框之前調用getSelectedFiles()
。 您想要做的就是在 showTxtFileFrame()
方法中調用該方法,就像您當前正在調用getSelectedFile()
。
例如:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.*;
@SuppressWarnings("serial")
public class JFileChooserExample extends JPanel {
// use a list model and JList that works *directly* with Files
private DefaultListModel<File> fileListModel = new DefaultListModel<>();
private JList<File> fileJList = new JList<>(fileListModel);
public JFileChooserExample() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new SelectFilesAction("Select Files", KeyEvent.VK_S)));
// help set the width and height of the JList
fileJList.setVisibleRowCount(10);
fileJList.setPrototypeCellValue(new File("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
JScrollPane scrollPane = new JScrollPane(fileJList);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(buttonPanel, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
private class SelectFilesAction extends AbstractAction {
public SelectFilesAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fc.showOpenDialog(JFileChooserExample.this);
if(result == JFileChooser.APPROVE_OPTION) {
fileListModel.clear(); // clear the model of prior files
File[] files = fc.getSelectedFiles();
for (File file : files) {
// add all files to the model
fileListModel.addElement(file);
}
}
}
}
private static void createAndShowGui() {
JFileChooserExample mainPanel = new JFileChooserExample();
JFrame frame = new JFrame("JFileChooser Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.