Giter Club home page Giter Club logo

nativejfilechooser's Introduction

NativeJFileChooser

This is a drop-in replacement for Swing's file chooser. Instead of displaying the Swing file chooser, it makes use of the JavaFX file chooser if available. It still uses Swing's file chooser as a fallback.

Usage

NativeJFileChooser is available via Maven:

<dependency>
    <groupId>li.flor</groupId>
    <artifactId>native-j-file-chooser</artifactId>
    <version>1.6.4</version>
</dependency>

Simply replace

JFileChooser fileChooser = new JFileChooser();

with

JFileChooser fileChooser = new NativeJFileChooser();

Example

EventQueue.invokeLater(() -> {
    JFrame frame = new JFrame("Test");
    JButton button = new JButton("Click me!");
    JFileChooser fileChooser = new NativeJFileChooser();
    fileChooser.setFileFilter(new FileNameExtensionFilter("JPG", "*.jpg"));
    button.addActionListener((ActionEvent e) -> {
        fileChooser.showDialog(frame, "Open");
    });
    frame.add(button);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
});

nativejfilechooser's People

Contributors

steffen678 avatar veluria avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nativejfilechooser's Issues

Not working without JavaFX in classpath

Check of presence of JavaFX is useless because you directly depend on the JavaFX classes. NativeJFileChooser won't be loaded at runtime. Fox example, in Java 14 environment:

--- exec-maven-plugin:3.0.0:exec (default-cli) @ Test ---
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/stage/DirectoryChooser
	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3244)
	at java.base/java.lang.Class.getDeclaredMethod(Class.java:2551)
	at java.desktop/java.awt.Component.isCoalesceEventsOverriden(Component.java:6321)
	at java.desktop/java.awt.Component$3.run(Component.java:6275)
	at java.desktop/java.awt.Component$3.run(Component.java:6273)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
	at java.desktop/java.awt.Component.checkCoalescing(Component.java:6272)
	at java.desktop/java.awt.Component.<init>(Component.java:6241)
	at java.desktop/java.awt.Container.<init>(Container.java:296)
	at java.desktop/javax.swing.JComponent.<init>(JComponent.java:598)
	at java.desktop/javax.swing.JFileChooser.<init>(JFileChooser.java:347)
	at java.desktop/javax.swing.JFileChooser.<init>(JFileChooser.java:295)
	at li.flor.nativejfilechooser.NativeJFileChooser.<init>(NativeJFileChooser.java:86)
	at test.JFileChooserWithProps.main(JFileChooserWithProps.java:89)
Caused by: java.lang.ClassNotFoundException: javafx.stage.DirectoryChooser
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 15 more

DirectoryChooser error

Here is the log, im on a macOS 11.6.4:

	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166)
	at java.base/java.lang.Class.getDeclaredMethod(Class.java:2473)
	at java.desktop/java.awt.Component.isCoalesceEventsOverriden(Component.java:6339)
	at java.desktop/java.awt.Component$3.run(Component.java:6293)
	at java.desktop/java.awt.Component$3.run(Component.java:6291)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.desktop/java.awt.Component.checkCoalescing(Component.java:6290)
	at java.desktop/java.awt.Component.<init>(Component.java:6259)
	at java.desktop/java.awt.Container.<init>(Container.java:296)
	at java.desktop/javax.swing.JComponent.<init>(JComponent.java:599)
	at java.desktop/javax.swing.JFileChooser.<init>(JFileChooser.java:360)
	at java.desktop/javax.swing.JFileChooser.<init>(JFileChooser.java:338)
	at li.flor.nativejfilechooser.NativeJFileChooser.<init>(NativeJFileChooser.java:96)
	at MainClass.main(LambdaExpressions.java:23)
Caused by: java.lang.ClassNotFoundException: javafx.stage.DirectoryChooser
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 15 more```

Event Dispatch Thread blocked

It looks like NativeJFileChooser blocks and the current thread until the file dialog is closed (due to the latch in showOpenDialog()), but if this method is invoked from the EDT it blocks the whole application.

Modality

Hi there, I tried somethings similar in the past but I was missing modality support. Any ideas on how to achieve this?

Select Multiple Folders

Hi,

I know the JavaFx FileChooser doesn't support multiple folder selection, but do you have any idea on how to get around that issue? I dont really want to miss the native filechooser, but selecting multiple folders is somewhat important for my application.

Intermittent deadlock (?) on macOS - getFocusOwner

Hi, I've been trying to use this as part of a Java Swing app my class uses. It seems to work fine most of the time, but on macOS, it occasionally hangs when it tries to show the dialog, and I can't figure out why. I did not write the app, so I can't really re-architect it, and maybe this issue is just because of how the code is written...

In an event handler, the app calls showOpenDialog. The Runnable does run, but on the fileChooser.showOpenDialog(null); line, the program hangs, eats a bunch of CPU, and never shows the dialog.

I've tried using jdb to debug, and found that that method is getting hung up in a call to getFocusOwner within the macOS support. It starts some kind of native AWT loop and never stops.

It seems to be a deadlock - if I interrupt the AWT Event Queue thread (the one waiting on the CountdownLatch), everything reappears, though it causes an InterruptedException so the dialog doesn't actually work. It's like the Event Queue thread is waiting on the JFX App thread, which is waiting on the Event Queue thread...

It feels like I shouldn't be calling showOpenDialog from the AWT Event Queue thread, but don't most applications do so? I mean, you handle a menu event or button by opening the dialog.

Is this an architectural issue? Or is something else going wrong?


Edit: Creating a dummy thread to call showOpenDialog does seem to work. I think this is architectural.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.