Like some other people who have asked similar questions, I was going nuts trying to 'fix' my JFileChooser dialog box generation code until I noticed that it is being generated, but it is appearing underneath all other windows and does not have an associated taskbar icon (so there was no clue at all that it existed!).
I am aware of these similar questions:
- Bringing JFileChooser on top of all windows
- JFileChooser from a command line program and popping up Underneath all windows
...but the answers to those questions seem overly complex, involving creating more GUI elements, which I can't believe would be required.
I am also aware of the advice here about not mixing console and Swing interfaces, but I want to keep things as simple as possible.
I would like to know how to generate a JFileChooser (showOpenDialog) dialog box that is above other windows without resorting to creating other GUI elements (JPanel etc.).
Note 1: This site seems to discuss a solution, but is hard to follow.
Note 2: If what I am asking for is impossible, then information about how to at least give the dialog box a taskbar icon (again without requiring it to have a parent) would be great.
My code, which right now creates a buried dialog box, is here:
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
class Client {
String currentDirectoryFolderPath = "H:\\myFolder";
javax.swing.JFileChooser jFileChooser =
new JFileChooser(currentDirectoryFolderPath);
jFileChooser.setVisible(true); //defaults to invisible?!?
javax.swing.filechooser.FileNameExtensionFilter fileExtensionFilter
= new FileNameExtensionFilter(
comma-separated values and text files",
"csv", "txt");
jFileChooser.setFileFilter(fileExtensionFilter);
//int returnVal = jFileChooser.showOpenDialog(jFileChooser);
//jFileChooser.showDialog(null, "testing 1--2--3");
//jFileChooser.requestFocusInWindow();
//jFileChooser.requestFocus();
//jFileChooser.showOpenDialog(null);
//jFileChooser.requestFocus();
int returnVal = jFileChooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
jFileChooser.getSelectedFile().getName());
}
System.out.println(JFileChooser.APPROVE_OPTION);
System.out.println(jFileChooser);
}
The commented code is all of the things I have tried that have not worked, including
- different types of request for focus before and after calling the dialog box, and
- supplying the dialog box object itself as it's own parent instead of passing a null (I thought that was worth a shot.).