在Jython中为JavaFx文件选择器添加文件过滤器并对其进行参数化

5

我在Jython中创建了一个JavaFX选择器文件。从Java移植到Jython并不容易,但最终还是有一些结果的。现在我想对获取的类进行参数化,考虑文件过滤器,以便能够使用该对象浏览与过滤文件类型不同的文件。
我尝试插入了一个固定的过滤器:

import sys

from javafx.application import Application
from javafx.stage import FileChooser, Stage

class fileBrowser(Application):

    @classmethod
    def main(cls, args):
        fileBrowser.launch(cls, args)

    def start(self, primaryStage):
        fc = FileChooser()
        filter = FileChooser.ExtensionFilter("All Images", '*.jpg')
        fc.getExtensionFilters().add(
            filter
        )

        f = fc.showOpenDialog(primaryStage)

if __name__ == '__main__':
    fileBrowser.main(sys.argv)

但我遇到了以下错误:
Exception in Application start method
Traceback (most recent call last):
  File "provaFileChooser.py", line 28, in <module>
    fileBrowser.main(sys.argv)
  File "provaFileChooser.py", line 15, in main
    fileBrowser.launch(cls, args)
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
        at java.lang.Thread.run(Unknown Source)
Caused by: Traceback (most recent call last):
  File "provaFileChooser.py", line 19, in start
    filter = FileChooser.ExtensionFilter("JPG Images", '*.jpg')
TypeError: javafx.stage.FileChooser$ExtensionFilter(): 2nd arg can't be coerced to java.util.List, String[]

        at org.python.core.Py.TypeError(Py.java:236)
        at org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:213)
        at org.python.core.PyReflectedFunction.throwBadArgError(PyReflectedFunction.java:316)
        at org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:325)
java.lang.RuntimeException: java.lang.RuntimeException: Exception in Application start method

我也尝试将过滤器转换为列表并将其插入到列表中,但错误仍然存在。

我做错了什么,应该怎么做?
提前感谢。

1个回答

5
您可以使用向量解析扩展类型来解决您的StackTrace问题,例如:
import sys

from java.io import File

from javafx.application import Application
from javafx.stage import FileChooser, Stage

class FileBrowser(Application):
    
    # I am a class attribute, Im called using NameClass.me
    # You have to remember this path is different according to SO
    initalDir = File("/home/miolivc/Documents/") # you have to handle this
    extensions = ["*.jpg"]

    @classmethod
    def main(cls, args):
        FileBrowser.launch(cls, args)

    def start(self, primaryStage):

        fc = FileChooser()
        filter = FileChooser.ExtensionFilter("All Images", FileBrowser.extensions)
        fc.getExtensionFilters().add(filter)
        fc.setInitialDirectory(FileBrowser.initalDir)

        choosed = fc.showOpenDialog(primaryStage)

        print("File: " + str(choosed))

if __name__ == '__main__':
    FileBrowser.main(sys.argv)

关于在代码的其他部分中使用FileBrowser,您需要了解JavaFX的工作原理,您可以在Controller类构造函数中使用文件,并使用Scene类调用此视图。 FileBrowser扩展了Application类,这意味着它是您应用程序的根,您应该从这个类中调用其他类。

为了更好地理解,建议您搜索有关Scene和FXMLLoader的信息。

我测试了使用Zulu Java FX 11和Jython 2.7.1。


1
早上好,米歇尔!很抱歉回复晚了,但你知道程序员的工作相当乏味!感谢你的回答,非常详细,但我想再知道三件事情,如果你至少告诉我前两个,我会给你剩下的奖励:1)如何在特定文件夹中打开浏览器;2)如何返回浏览文件以在代码的其他部分中使用;3)如何将过滤列表作为类/方法的参数。提前致谢! - Memmo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接