将文件过滤器添加到 Jython 中的 JavaFx Filechooser 并对其进行参数化
Add file filters to JavaFx Filechooser in Jython and parametrize them
我在 Jython 中创建了一个 javaFX 选择器文件。从 Java 移植到 Jython 并不容易,但最终还是取得了一些成果。现在我想参数化获得的[=23=],考虑到文件过滤器,以便能够使用对象来浏览不同于过滤文件类型的对象。
我尝试插入固定过滤器:
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(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
我也尝试过将过滤器转换为列表并将过滤器插入列表,但错误仍然存在。
我做错了什么,我该怎么办?
提前致谢。
您可以使用向量解析扩展类型来解决您的 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 class 构造函数中使用文件并使用 Scene class. FileBrowser 正在扩展应用程序 class,这意味着它是您应用程序的根,您应该从这个应用程序调用其他应用程序。
要了解更多相关信息,建议您搜索 Scene 和 FXMLLoader。
我使用 Zulu Java FX 11 和 Jython 2.7.1
进行了测试
我在 Jython 中创建了一个 javaFX 选择器文件。从 Java 移植到 Jython 并不容易,但最终还是取得了一些成果。现在我想参数化获得的[=23=],考虑到文件过滤器,以便能够使用对象来浏览不同于过滤文件类型的对象。
我尝试插入固定过滤器:
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(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
我也尝试过将过滤器转换为列表并将过滤器插入列表,但错误仍然存在。
我做错了什么,我该怎么办?
提前致谢。
您可以使用向量解析扩展类型来解决您的 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 class 构造函数中使用文件并使用 Scene class. FileBrowser 正在扩展应用程序 class,这意味着它是您应用程序的根,您应该从这个应用程序调用其他应用程序。
要了解更多相关信息,建议您搜索 Scene 和 FXMLLoader。
我使用 Zulu Java FX 11 和 Jython 2.7.1
进行了测试