Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add file filters to JavaFx Filechooser in Jython and parametrize them

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.
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("All Images", '*.jpg')
        fc.getExtensionFilters().add(
            filter
        )

        f = fc.showOpenDialog(primaryStage)

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

But I have the following error:

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

I also tried to cast the filter into a list and to insert the filter into a list but the error persists.

What am I doing wrong, and what should I do?
Thanks in advance.

like image 377
Memmo Avatar asked Sep 14 '20 09:09

Memmo


People also ask

What is filechooser in JavaFX?

In JavaFX, FileChooser is a class that is used to browse the files from the system. Several operations include opening a single file, opening multiple files and saving files in the system. JavaFX file chooser is instantiated from the class javafx.stage.FileChooser.

How do I add a filter to a JFileChooser?

In Swing, we can do that by using methodaddChoosableFileFilter(FileFilter filter) of the class JFileChooser. boolean accept (File f): returns true if the file f satisfies a filter condition. The condition here is the extension of the file.

How to use filefilter in swing with Java?

In Swing, we can do that by using methodaddChoosableFileFilter(FileFilter filter) of the class JFileChooser. Create a class that extends FileFilter abstract class and overrides its two methods: boolean accept (File f): returns true if the file f satisfies a filter condition. The condition here is the extension of the file.

How do I open a file in JavaFX?

JavaFX Object Oriented Programming Programming Using JavaFX file chooser, you can open files browse through them and save the files. The class javafx.stage.FileChooser represents a file chooser, you can open a file dialog open single or multiple files using this. You can create a file chooser in your application by instantiating this class.


1 Answers

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("/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)

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

like image 117
Michelle Oliveira Avatar answered Oct 23 '22 23:10

Michelle Oliveira