简体   繁体   中英

How do I select a folder to save a file to in Jython using JFileChooser

I'm trying to use JFileChooser to select a folder to save a file to in Jython, but I'm not quite sure how to go about it.

The only example I've found so far was at this site: http://zetcode.com/gui/jythonswing/dialogs/ , but its not really what I'm looking for.

I'm hoping to have just the file selection dialog open (the examples requires an awkward button press to activate the file dialog). I would also like to get the current directory of the selected folder instead of the filepath of a file.

Well I'm not going to claim this is the cleanest solution in the world, but it does work and thats close enough for me.

from java.awt import BorderLayout
from javax.swing import JFileChooser, JFrame, JPanel 

class DropDown(JFrame):

    def __init__(self):
        super(DropDown, self).__init__()

        self.initUI()

    def initUI(self):


        self.panel = JPanel()
        self.panel.setLayout(BorderLayout())

        choseFile = JFileChooser()

        choseFile.setDialogTitle('Select Export Location')
        choseFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY)

        ret = choseFile.showSaveDialog(self.panel)

        if ret == JFileChooser.APPROVE_OPTION:
            if choseFile.getSelectedFile().isDirectory():
                self.file_name = str(choseFile.getSelectedFile())

    def get_file_name(self):
        return self.file_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM