When I use system.file.openFile() to select a file and I type a in the File Name text field, the file chooser changes directory up one level. This makes it very difficult to use. (I assume openFile() is using a JFileChooser because I get the same behavior if I import javax.swing.JFileChooser and call its showDialog() method.)
Is this a bug in the underlying Jython implementation? Is there a workaround?
I have a workaround that’s quite a bit simpler than the one you referenced, although I’m not sure I understand why it works. I derive from JFileChooser and redefine processKeyBinding() as a do-nothing method. To illustrate, execute the following code in the script playground in version 7.5.10 (if you have it available):
This suffers from the “Backspace invokes ‘Go Up’ action” issue:
from javax.swing import JFileChooser
chooser = JFileChooser(None)
if chooser.showOpenDialog(None) == FileChooser.APPROVE_OPTION:
print chooser.getSelectedFile()
Whereas this does not:
from javax.swing import JFileChooser
class FileChooser(JFileChooser):
def processKeyBinding(self, keyStroke, keyEvent, condition, pressed):
pass
chooser = FileChooser(None)
if chooser.showOpenDialog(None) == FileChooser.APPROVE_OPTION:
print chooser.getSelectedFile()
I assume that the JFileChooser bug is in the processKeyBinding method and by essentially removing it I avoid the problem at the cost of losing whatever other keyboard shortcuts it implements. Character events are still handled correctly by the JTextField containing the selected file, so I’m content with the trade-off.
If someone has more insight into this or has a better solution, I’d like to hear it, but this workaround will do for now.