如何 select JFileChooser 中的多个文件 window
How to select multiple files in JFileChooser window
我不知道如何在 JFileChooser window 中一次 select 多个文件。我认为它已经启用,因为我在 JFileChooser 对象上使用了 setMultiSelectionEnabled(true)
方法,但是当我尝试实际 select 多个时,我做不到。我尝试了单击并拖动、CTRL 和箭头 keys/click、Alt 和箭头 Keys/click、Shift 和箭头 keys/click,但仍然没有成功。我该怎么做?
我创建 JFileChooser 的代码:
仅使用一个文件时,解析方法工作正常。
此 class 在 JFrame 中按下按钮时调用。
public class FileChooser implements ActionListener, Runnable
{
private Parser parser = new Parser();
private static File[] selectedFiles;
private static File currentSelected;
private JFileChooser jfc;
public static File getSelectedFile()
{
return currentSelected;
}
public void actionPerformed(ActionEvent actionEvent)
{
new Thread(this).start();
}
public void run()
{
if ( Window.bFG5IsPressed() && Window.bFGAIsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setMultiSelectionEnabled(true);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseAll(e.getAbsolutePath());
}
}
}
else if ( Window.bFG5IsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseFG5(e.getAbsolutePath());
}
}
}
else if ( Window.bFGAIsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseFGA(e.getAbsolutePath());
}
}
}
else
{
JOptionPane x = new JOptionPane();
x.showMessageDialog(x, "Escolher tipo de arquivo");
x.setLocation(300,300);
x.setVisible(true);
}
}
}
调用 setMultiSelectionEnabled(true)。这是一个 运行 示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
public class MultipleFileChooser extends JFrame {
public static void main(String[] args) {
MultipleFileChooser multipleFileChooser = new MultipleFileChooser();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
JButton button = new JButton("Open Files");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int choice = fileChooser.showOpenDialog(multipleFileChooser);
if (choice == JFileChooser.APPROVE_OPTION) {
File[] openFiles = fileChooser.getSelectedFiles();
System.out.println("Files: " + Arrays.toString(openFiles));
}
}
});
JPanel panel = new JPanel();
panel.add(button);
multipleFileChooser.add(panel);
multipleFileChooser.setSize(new Dimension(400, 400));
multipleFileChooser.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
multipleFileChooser.setVisible(true);
}
}
附上供OP参考的截图:
我不知道如何在 JFileChooser window 中一次 select 多个文件。我认为它已经启用,因为我在 JFileChooser 对象上使用了 setMultiSelectionEnabled(true)
方法,但是当我尝试实际 select 多个时,我做不到。我尝试了单击并拖动、CTRL 和箭头 keys/click、Alt 和箭头 Keys/click、Shift 和箭头 keys/click,但仍然没有成功。我该怎么做?
我创建 JFileChooser 的代码: 仅使用一个文件时,解析方法工作正常。 此 class 在 JFrame 中按下按钮时调用。
public class FileChooser implements ActionListener, Runnable
{
private Parser parser = new Parser();
private static File[] selectedFiles;
private static File currentSelected;
private JFileChooser jfc;
public static File getSelectedFile()
{
return currentSelected;
}
public void actionPerformed(ActionEvent actionEvent)
{
new Thread(this).start();
}
public void run()
{
if ( Window.bFG5IsPressed() && Window.bFGAIsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setMultiSelectionEnabled(true);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseAll(e.getAbsolutePath());
}
}
}
else if ( Window.bFG5IsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseFG5(e.getAbsolutePath());
}
}
}
else if ( Window.bFGAIsPressed() )
{
jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION)
{
selectedFiles = jfc.getSelectedFiles();
for (File e : selectedFiles) {
currentSelected = e;
parser.parseFGA(e.getAbsolutePath());
}
}
}
else
{
JOptionPane x = new JOptionPane();
x.showMessageDialog(x, "Escolher tipo de arquivo");
x.setLocation(300,300);
x.setVisible(true);
}
}
}
调用 setMultiSelectionEnabled(true)。这是一个 运行 示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
public class MultipleFileChooser extends JFrame {
public static void main(String[] args) {
MultipleFileChooser multipleFileChooser = new MultipleFileChooser();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
JButton button = new JButton("Open Files");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int choice = fileChooser.showOpenDialog(multipleFileChooser);
if (choice == JFileChooser.APPROVE_OPTION) {
File[] openFiles = fileChooser.getSelectedFiles();
System.out.println("Files: " + Arrays.toString(openFiles));
}
}
});
JPanel panel = new JPanel();
panel.add(button);
multipleFileChooser.add(panel);
multipleFileChooser.setSize(new Dimension(400, 400));
multipleFileChooser.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
multipleFileChooser.setVisible(true);
}
}
附上供OP参考的截图: