mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 14:00:55 +03:00
System File Chooser: on Linux when JavaFX is used in application, then always use Swing file chooser because system file dialog does work (PR #988)
with Java 8, some GLib related messages are logged to console and system file dialog is not shown e.g.: `GLib-GObject-WARNING **: cannot register existing type 'GdkDisplayManager'` with Java 17, the system file dialog is shown, but then JavaFX no longer works with Java 21, the application quits/crashes immediately when trying to show system file dialog also fixed Error Prone warning in `getFiltersForDialog()`
This commit is contained in:
@@ -22,9 +22,10 @@ import java.awt.SecondaryLoop;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.Window;
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -590,7 +591,7 @@ public class SystemFileChooser
|
||||
if( filters.size() == 1 &&
|
||||
filters.get( 0 ) == getAcceptAllFileFilter() &&
|
||||
(fileFilter == getAcceptAllFileFilter() || fileFilter == null) )
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>();
|
||||
|
||||
// check whether current filter is already in list
|
||||
if( (fileFilter != null && filters.contains( fileFilter )) || fileFilter == null )
|
||||
@@ -1073,6 +1074,15 @@ public class SystemFileChooser
|
||||
private static class LinuxFileChooserProvider
|
||||
extends SystemFileChooserProvider
|
||||
{
|
||||
@Override
|
||||
public File[] showDialog( Window owner, SystemFileChooser fc ) {
|
||||
// fallback to Swing file chooser if JavaFX is initialized
|
||||
if( isFXinitialized() )
|
||||
return new SwingFileChooserProvider().showDialog( owner, fc );
|
||||
|
||||
return super.showDialog( owner, fc );
|
||||
}
|
||||
|
||||
@Override
|
||||
String[] showSystemDialog( Window owner, SystemFileChooser fc ) {
|
||||
boolean open = (fc.getDialogType() == OPEN_DIALOG);
|
||||
@@ -1175,6 +1185,41 @@ public class SystemFileChooser
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
|
||||
private static Boolean fxinitialized;
|
||||
|
||||
boolean isFXinitialized() {
|
||||
if( fxinitialized != null )
|
||||
return fxinitialized;
|
||||
|
||||
// check whether JavaFX is available
|
||||
try {
|
||||
Class.forName( "javafx.application.Platform", false, getClass().getClassLoader() );
|
||||
} catch( ClassNotFoundException ex ) {
|
||||
// JavaFX is not available
|
||||
fxinitialized = false;
|
||||
return fxinitialized;
|
||||
}
|
||||
|
||||
// check whether JavaFX is initialized
|
||||
try {
|
||||
Class<?> cls = Class.forName( "javafx.application.Platform" );
|
||||
Method m = cls.getMethod( "runLater", Runnable.class );
|
||||
m.invoke( null, (Runnable) () -> {} );
|
||||
fxinitialized = true;
|
||||
return fxinitialized;
|
||||
} catch( InvocationTargetException ex ) {
|
||||
if( ex.getCause() instanceof IllegalStateException )
|
||||
return false; // JavaFX is available, but not (yet) initialized
|
||||
} catch( Throwable ex ) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
// other error --> assume that JavaFX is not initialized
|
||||
fxinitialized = false;
|
||||
return fxinitialized;
|
||||
}
|
||||
|
||||
//---- class LinuxApproveContext ----
|
||||
|
||||
private static class LinuxApproveContext
|
||||
|
||||
Reference in New Issue
Block a user