import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; import java.io.IOException; import javax.swing.JFileChooser; public class Main { final static JFileChooser fc = new JFileChooser("/"); public static void main(String[] args) throws IOException { fc.setMultiSelectionEnabled(true); fc.addPropertyChangeListener(new ChangeListener()); fc.showOpenDialog(null); } private static class ChangeListener implements PropertyChangeListener { @Override public void propertyChange(PropertyChangeEvent evt) { if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt .getPropertyName())) { File[] selectedFiles = fc.getSelectedFiles(); File[] allowedFiles = new File[2]; if (selectedFiles.length > 2) { allowedFiles[0] = selectedFiles[1]; allowedFiles[1] = selectedFiles[0]; fc.setSelectedFiles(allowedFiles); } } } } }