Handling multiple file selection in the FileDialog class - Java Swing

Java examples for Swing:JFileChooser

Description

Handling multiple file selection in the FileDialog class

Demo Code

import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
    /*w  w  w  .jav  a 2  s .  co m*/
    public Main() {        
        this.setTitle("Example");
        this.setSize(200, 100);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
        this.setLayout(new FlowLayout());
        
        final FileDialog fileDialog = new FileDialog(this, "FileDialog");
        fileDialog.setMultipleMode(true);
        
        JButton fileDialogButton = new JButton("File Dialog");    
        fileDialogButton.addActionListener(event->{
                fileDialog.setVisible(true);
                File files[] = fileDialog.getFiles();
                for (File file : files) {
                    System.out.println("File: " + file.getName());
                }
        });

        this.add(fileDialogButton);
        
        JButton exitButton = new JButton("Exit");    
        exitButton.addActionListener(event->System.exit(0));

        this.add(exitButton);
    }


    public static void main(String[] args) {              
        
        SwingUtilities.invokeLater(() ->{
                Main window = new Main();
                window.setVisible(true);
        });

    }
}

Related Tutorials