Skip to content

Commit 9cd3e35

Browse files
Tejesh Rjayathirthrao
Tejesh R
authored andcommitted
4834298: JFileChooser.getSelectedFiles() failed with multi-selection and double-click
Reviewed-by: jdv
1 parent ec2629c commit 9cd3e35

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

src/java.desktop/share/classes/sun/swing/FilePane.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1751,11 +1751,12 @@ private void doFileSelectionModeChanged(PropertyChangeEvent e) {
17511751
}
17521752

17531753
private void doMultiSelectionChanged(PropertyChangeEvent e) {
1754+
clearSelection();
17541755
if (getFileChooser().isMultiSelectionEnabled()) {
17551756
listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
1757+
getFileChooser().setSelectedFile(null);
17561758
} else {
17571759
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1758-
clearSelection();
17591760
getFileChooser().setSelectedFiles(null);
17601761
}
17611762
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.Color;
25+
import java.io.File;
26+
import javax.swing.JFileChooser;
27+
import javax.swing.JFrame;
28+
import javax.swing.JTextArea;
29+
import javax.swing.SwingUtilities;
30+
31+
/*
32+
* @test
33+
* @bug 4834298
34+
* @key headful
35+
* @requires (os.family == "windows" | os.family == "linux")
36+
* @summary Test to check if the getSelectedFiles of JFilesChooser
37+
* returns selectedFiles when Multi-Selection is enabled.
38+
* @run main/manual MultiSelectionEnabledSelectedFilesTest
39+
*/
40+
public class MultiSelectionEnabledSelectedFilesTest {
41+
private static JFrame frame;
42+
public static void main(String[] args) throws Exception {
43+
SwingUtilities.invokeAndWait(new Runnable() {
44+
public void run() {
45+
try {
46+
runTest();
47+
} finally {
48+
frame.dispose();
49+
}
50+
}
51+
});
52+
System.out.println("Test Pass!");
53+
}
54+
55+
static void runTest() {
56+
//Initialize the components
57+
String INSTRUCTIONS
58+
= "Instructions to Test:"+
59+
"\n1. Select a valid file using mouse double-click on first "+
60+
"dialog."+
61+
"\n2. After Selection, first dialog will close and second " +
62+
"dialog opens with multi-Selection enabled."+
63+
"\n3. Select the same file using mouse double-click without " +
64+
"moving mouse position or selection."+
65+
"\n4. If the selected file is updated then getSelectedFiles "+
66+
"returns the file selected and test will PASS otherwise test "+
67+
"will FAIL";
68+
69+
JFileChooser chooser = new JFileChooser();
70+
JTextArea textArea = new JTextArea();
71+
frame = new JFrame("Test Instructions");
72+
73+
textArea.setText(INSTRUCTIONS);
74+
textArea.setEnabled(false);
75+
textArea.setDisabledTextColor(Color.black);
76+
textArea.setBackground(Color.white);
77+
78+
frame.add(textArea);
79+
frame.pack();
80+
frame.setVisible(true);
81+
82+
chooser.showOpenDialog(null);
83+
chooser.setMultiSelectionEnabled(true);
84+
chooser.showOpenDialog(null);
85+
File[] files = chooser.getSelectedFiles();
86+
87+
if (files.length == 0) {
88+
throw new RuntimeException("Test Failed since selected files are empty!!");
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)