JFileChooser does not return control on close in Ubuntu
I'm trying to use JFileChooser to open a file. I find the file I want and click 'OK'. The dialog box goes away and the rest of the code continues to run, but the whole GUI is no longer operational. I can't even close the window, except manually through the IDE I'm programming in. However, if I click 'cancel', everything works normally. This code worked fine when I was developing in Windows 开发者_如何学JAVA7. It does not work in Ubuntu (10.04 Lucid). My code looks thusly:
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("Wav Files", "wav", "WAV"));
int retVal = chooser.showOpenDialog(this);
if(retVal == JFileChooser.APPROVE_OPTION) {
String filePath = chooser.getSelectedFile().getPath();
txfFileSelect.setText(filePath); // txfFileSelect is a JTextField which is supposed to change when I click 'OK'. It doesn't.
}
else {
txfFileSelect.setText("Test"); // this works normally
}
The problem is likely on this line:
txfFileSelect.setText(filePath);
You should use a System.out.println just before the line above to inspect the filePath String.
System.out.println(filePath);
txfFileSelect.setText(filePath);
Then you should try to call your txfFileSelect.setText(...) with this String in your code without using a JFileChooser (comment it out for the time being). I'll bet you'll have the same problem. Are you catching all exceptions? The cause for the freeze is somewhere else in your code I'll bet.
I had left out a part of the code, so it actually looked like this:
if(retVal == JFileChooser.APPROVE_OPTION) {
String filePath = chooser.getSelectedFile().getPath();
txfFileSelect.setText(filePath);
try {
// Some other code where the program was actually hanging
} catch (IOException e) {
e.printStackTrace();
}
else {
txfFileSelect.setText("Test");
}
In case you couldn't tell by the code above, the problem had to do with the fact that the code was hanging inside a function.
精彩评论