Reputation: 92
I can't figure out how to select more than one file at a time in the JFileChooser window. I think it is already enabled because I used the setMultiSelectionEnabled(true)
method on the JFileChooser object, but when I try to actually select more than one, I can't do it. I tried click and drag, CTRL and arrow keys/click, Alt and arrow Keys/click, Shift and arrow keys/click but still no luck. How can I do this?
Code where I create the JFileChooser: The parse methods are working fine when using only one file. This class is called when a button is presses in the 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);
}
}
}
Upvotes: 0
Views: 984
Reputation: 4259
Call setMultiSelectionEnabled(true). Here is a running sample:
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);
}
}
Attaching screenshot for OP's reference:
Upvotes: 1