I seem to have a problem with my very simple implementation of a file chooser dialogue that requires me to minimize Netbeans each time in order to get to it, and it gets pretty frustrating specially now with testing.
I have seen a few solutions online including SO yet none seem to do the trick, while some other seem very lengthy and complicated for my current level.
private void fileSearch() {
JFileChooser fileSelect = new JFileChooser();
int returnVal = fileSelect.showOpenDialog(null);
String pathToFile;
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileSelect.getSelectedFile();
pathToFile = file.getAbsolutePath();
try {
P.binaryFileToHexString(pathToFile);
} catch (Exception e) {
System.out.print("Oops! there was an error there..." + e);
}
System.out.println("\nYou chose to open this file: " + file.getName());
}
}
Some of my try's include using;
.requestFocus();
.requestFocusInWindow();
.setVisible();
Is there a particular attribute/method I can set in order to solve the problem?
Of course,
this
must be a Component of some sort (the JFrame or JPanel of your main interface). All dialogs need to have a parent component if you wish them to come to the front.I'm not sure what your problem actually is (it's probably your Netbeans.... who knows), but have you tried overriding the
createDialog
method?Example:
This is merely a hack solution, you should not need to do that ordinarily.
The API for
showOpenDialog()
refers toshowDialog()
, which says, "If the parent isnull
, then the dialog depends on no visible window, and it's placed in a look-and-feel-dependent position such as the center of the screen."The example below positions the chooser in the center of the screen on my L&F. You might see how it compares to yours.