-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4834298: JFileChooser.getSelectedFiles() failed with multi-selection and double-click #9996
Conversation
👋 Welcome back tr! A progress list of the required criteria for merging this PR into |
|
@TejeshR13 The following label will be automatically applied to this pull request:
When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command. |
Webrevs
|
//Check if any files are selected before setting multi-Selection. | ||
//If selected, retain them and update the selected files in FileChooser class. | ||
File[] selectedFiles = null; | ||
if(getFileChooser().getSelectedFiles().length != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add space after if
s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
if (getFileChooser().getSelectedFiles().length != 0) { | ||
selectedFiles = getFileChooser().getSelectedFiles(); | ||
} else if (getFileChooser().getSelectedFile() != null) { | ||
File selectedFile = getFileChooser().getSelectedFile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File selectedFile = getFileChooser().getSelectedFile();
selectedFiles = new File[1];
selectedFiles[0] = selectedFile;
You can directly assign getFileChooser().getSelectedFile() value to selectedFiles[0]. I think no need to create extra selectedFile variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
chooser.showOpenDialog(null); | ||
File[] files = chooser.getSelectedFiles(); | ||
|
||
if(files.length <= 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(files.length <= 0) {
Please add space after if.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
I guess copyright year for FilePane.java needs to be updated. |
Updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied the patch and tested, attached test case is passed with your fix.
* returns selectedFiles when Multi-Selection is enabled. | ||
* @run main/manual MultiSelectionEnabledSelectedFilesTest | ||
*/ | ||
public class MultiSelectionEnabledSelectedFilesTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this bug specific to any platform?
Because the test is created to run on all platforms and it doesnt fail in my Mac.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it doesn't fail in mac. It does fail in windows and linux, so didn't make any os specific. Still I have tested in all platforms and it works fine. The bug was raised as linux specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the test doesnt fail in Mac(In my case second dialog doesnt exit and it stays on the screen, which will result in timeout and jtreg error on Mac)?
It looks like fix is not some platform specific change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix handles Windows and linux platforms, since FilePane.class is involved on mode selection. In Mac its doesn't come thought this path, I guess it handles some other way. The issue doesn't occur on key press also (All platforms), since the path of setting selected files are different for each scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the dialogue is not not been removed when exception is thrown. Will take a look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the product fix that is being done will touch only Linux and Windows(And in Mac it looks like it not exiting properly), we can make this regression test to run only on Linux and Windows using jtreg tag:
@requires (os.family == "windows" | os.family == "linux")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
|
||
static void runTest() { | ||
//Initialize the components | ||
String INSTRUCTIONS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instruction should be updated to specify that we need to use double-click.
Also when i select a file using double click in first and then select same file using double-click(without moving mouse) test doesnt exit. JFileChooser second dialog just stays on the screen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
chooser.showOpenDialog(null); | ||
File[] files = chooser.getSelectedFiles(); | ||
|
||
if (files.length <= 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can File[] length be negative? I think just a "==" to "0" check should suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
selectedFiles = new File[1]; | ||
selectedFiles[0] = getFileChooser().getSelectedFile(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this problem seen only when single file is selected? What happens when more than one file is selected?
By default, is MultiSelection enabled on JfileChooser?
In FilePane.java, i see that get/setSelectedFiles() is exclusively used when multi-selection mode is enabled and get/setSelectedFile() is used when multi-selection is not enabled. Using getSelectedFile() to change the state when multi-selection is enabled might introduce state corruption issues in other scenarios.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This problem occurs when user switch the mode from single selection to multi selection, that too on specified scenario. By default multiselection will be disabled. When user selects a file in single selection and then switch to multi selection the selected single file is not retained in selectedFiles (Even though its a single file). So In order to handle this situation, whenever user switch from any state to multiselection enabled state, the previously selected files(if present) is set to selectedFiles().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other state are not altered or modified during the process, just the selected files are set to setSelectedFiles.
|
||
if (selectedFiles != null) { | ||
getFileChooser().setSelectedFiles(selectedFiles); | ||
} | ||
} else { | ||
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); | ||
clearSelection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we move from multi-selection enabled to disable. We are clearing the selection and calling setSelectedFiles(null). Do we need to replicate similar state update when we move from multi-selection disable to enable (clearing selection and calling setSelectedFile(null))?
It looks like somewhere we are not clearing SelectedFile() and it is causing this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we replicate the same then previously selected files will be cleared and user wont be able to retain the selected files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearing the selection from multi to single is fine, but the reverse might not be right I guess..... Since selected file/files can be retained when user switch to multi selection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this scenario we are actually closing the first JFileChooser dialog and then opening another JFileChooser dialog with multi-selection. Do we need to maintain selected file state between these dialogs? If yes, why? Please clarify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In mac, the selection is maintained during this state transition, only in this scenario its not handled. And since same filechooser is used in selecting the files ( Just pop-up dialogue is used to select the files) I think it would be better to retain the selection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In keyboard selection the state is actually not maintained. Then do you think it better to clear the selection and set selected files to null....?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearing the selection and setting it to null also solves the problem though. When we call clear selection, reset will happen and selectedFiles will be set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed on call its better to clear selection when we move between multi-selection enabled/disabled as captured in #9996 (comment).
Because using getSelectedFile() to update setSelectedFiles() might cause state management issues in some other scenarios.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I have updated as discussed on call and tested.
@kumarabhi006 U might have to re-review the PR as we have changed the logic. Previously the selected flies state was retained, now the state is been cleared on MultiSelection enabled/disabled switch. |
@TejeshR13 I see some test failures in latest CI job like javax/swing/JTree/4618767/JTreeSelectedElementTest.java. |
Yeah, those tests are failing without my changes too, so confirmed that it wasn't from my change. Some tests are failing without my fix also, so to ensure that my fix doesn't cause any regression I have tested without my fix and shared the link in JBS. |
Thanks for adding CI run without fix also in JBS. LGTM. |
Applied the patch and tested, attached test case is passed with your recent fix. |
@TejeshR13 This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 378 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@jayathirthrao) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
/integrate |
@TejeshR13 |
/sponsor |
Going to push as commit 9cd3e35.
Your commit was automatically rebased without conflicts. |
@jayathirthrao @TejeshR13 Pushed as commit 9cd3e35. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
JFileChooser.getSelectedFiles() failed to retrieve files with multi-selection and double-click. This occurs when a single file is selected then enable multi-selection then select the same file through mouse double-click(Two file chooser dialogs used before and after multi-selection enabled). SelectedFiles are updated when a change in files/directory selection is done or when selection is made through keyboard selection. In this case since a single file is selected before multi-selection is enabled and same file is selected again without changing the selection index/directory, leaving selected Files un-updated.
Proposed Fix : Whenever Multi-Selection is enabled check if any files are selected, if yes update the selected Files of JFileChoooser.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/9996/head:pull/9996
$ git checkout pull/9996
Update a local copy of the PR:
$ git checkout pull/9996
$ git pull https://git.openjdk.org/jdk pull/9996/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 9996
View PR using the GUI difftool:
$ git pr show -t 9996
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/9996.diff