为Jython中的JavaFx Filechooser添加文件过滤器并对其进行参数化。

huangapple go评论71阅读模式
英文:

Add file filters to JavaFx Filechooser in Jython and parametrize them

问题

我在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("所有图片", '*.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图片", '*.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

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

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

英文:

I created a javaFX chooser file in Jython. It wasn't easy to port from Java to Jython, but in the end some results came. Now I would like to parameterize the obtained class, taking into account the file filters, so as to be able to use the object for browsing different from the type of filtered files.<br>
I tried to insert a fixed filter:

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(&quot;All Images&quot;, &#39;*.jpg&#39;)
        fc.getExtensionFilters().add(
            filter
        )

        f = fc.showOpenDialog(primaryStage)

if __name__ == &#39;__main__&#39;:
    fileBrowser.main(sys.argv)

But I have the following error:

Exception in Application start method
Traceback (most recent call last):
  File &quot;provaFileChooser.py&quot;, line 28, in &lt;module&gt;
    fileBrowser.main(sys.argv)
  File &quot;provaFileChooser.py&quot;, 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 &quot;provaFileChooser.py&quot;, line 19, in start
    filter = FileChooser.ExtensionFilter(&quot;JPG Images&quot;, &#39;*.jpg&#39;)
TypeError: javafx.stage.FileChooser$ExtensionFilter(): 2nd arg can&#39;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

I also tried to cast the filter into a list and to insert the filter into a list but the error persists.<br><br>
What am I doing wrong, and what should I do?<br>
Thanks in advance.

答案1

得分: 5

你可以使用向量解析扩展类型来解决您的StackTrace,就像这样:

    import sys
    
    from java.io import File
    
    from javafx.application import Application
    from javafx.stage import FileChooser, Stage
    
    class FileBrowser(Application):
        
        # 我是一个类属性,可以使用NameClass.me来调用我
        # 你必须记住,这个路径根据操作系统不同而不同
        initalDir = File("/home/miolivc/Documents/") # 你需要处理这个
        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中进行了测试。

英文:

You can solve your StackTrace using vector to parse the extension types, like this:

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(&quot;/home/miolivc/Documents/&quot;) # you have to handle this
    extensions = [&quot;*.jpg&quot;]

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

    def start(self, primaryStage):

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

        choosed = fc.showOpenDialog(primaryStage)

        print(&quot;File: &quot; + str(choosed))

if __name__ == &#39;__main__&#39;:
    FileBrowser.main(sys.argv)

About using FileBrowser in others parts of your code, you have to understand how JavaFX works, you can use file in Controller class constructor and call this view using Scene class. FileBrowser is extending Application class, this mean that is the root of your app, you should call others from this one.

To understand more about this, I suggest you search about Scene and FXMLLoader.

I tested using Zulu Java FX 11 and Jython 2.7.1

huangapple
  • 本文由 发表于 2020年9月14日 17:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63881222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定