r/javahelp•
Shuko

Is there a way to make a JFileChooser maintain focus until it is closed?

Hi there. I'm working on an existing UI that can oftentimes have multiple JFrames open, and in one of these JFrames I have a JFileChooser that opens when a button is pressed. What I want to know is whether or not there's a way for me to limit the focus in such a way that the user has to either select a file from the menu and hit the selection button, hit the cancel button, or close the file chooser window before they can click anywhere else in the UI.

My problem arose when I discovered some wonky behavior that occurred when I tried to minimize (iconify) one of the unrelated JFrames while the JFileChooser dialog was showing. If I try to iconify one of the JFrames that is being visually overlapped by another JFrame, it and its overlapper(s) will become iconified, rather than just the one chosen. I suspect that this may have something to do with the JFileChooser's being open, but I'm not sure. I'd feel safer if I could limit the user's input to just the JFileChooser until such time as it is closed.

Is there any way to accomplish this? I've tried adding a FocusListener to the JFileChooser and fiddling with focus settings in both the focusGained() and focusLost() methods, but I don't think that's the right approach, either. I'm open to suggestions, if you have any.

Posted Apr 13, 2017, 5:39 PM UTC

23
wildjokers•

You want to open a JFileChooser as a modal dialog. There is a method for that:

https://docs.oracle.com/javase/8/docs/api/javax/swing/JFileChooser.html#showOpenDialog-java.awt.Component-

Pass the parent JFrame to the showOpenDialog() method. The JFileChooser will then be modal.

Generally you only have one JFrame per application, then other windows will be JDialog's (both are top-level containers, but JDialogs can have a parent).

1
ShukoOP•

Will it be modal even if I use the "showDialog()" method? I've been passing the parent JFrame into that, along with a text string I want for the text for my file selection button. Does that method result in a non-modal dialog, whereas the showOpenDialog() method results in a modal one?

As far as the multiple JFrames problem goes, I know, and I've been working to convince others that we need to use that paradigm instead. I'm not sure why the rest of the dev team decided to go with JFrames instead of dialogs, but I'll look into it. At any rate, I really appreciate the suggestions, but unless using showOpenDialog() instead of showDialog(parent, String) solves my issue, I'm back where I started from. :(

1
wildjokers•

showDialog() should also open a modal dialog.

I have a feeling that the file chooser is modal in the sense that you can't select its parent JFrame; however, you can still select its non-parent JFrame. Is this true?

Your problem may be solved by using JDialog for all your other dialogs (except for a single parent JFrame). If they all have the same parent JFrame I am fairly confident (but not 100% sure) that modality will work the way you want.

Out of curiosity would your application make more sense as an MDI application using JDesktopPane?

1