1

I have looked at this page on how to use the JavaFX FileChooser with Swing, and it works well: JavaFX FileChooser in swing

I have also looked at this page on how to make a stage now show in the taskbar with StageStyle.UTILITY: JavaFX: can you create a stage that doesn't show on the task bar and is undecorated?

However, when I combine the two, the file dialog that I use in the swing application opens a separate window in the taskbar, that does not appear with JFileChooser. I was wondering how to make the JavaFX FileChooser not show in the taskbar when using it in a swing application.

Here is a snippet of code that demonstrates the issue:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;

import javafx.application.Platform;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Test extends JFrame implements ActionListener {

    private JButton button;
    private FileChooser chooser;

    public Test(){
        super();
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        button = new JButton("Test");
        button.addActionListener(this);
        add(button);

        new javafx.embed.swing.JFXPanel();
        Platform.setImplicitExit(false);

        chooser = new FileChooser();

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e){
        Platform.runLater(new Runnable(){
            public void run(){
                Stage stage = new Stage();
                stage.initStyle(StageStyle.UTILITY);
                File testfile = chooser.showOpenDialog(stage);
            }
        });
    }

    public static void main(String[] args){
        new Test();
    }
}
Community
  • 1
  • 1
jmohrmann
  • 21
  • 3
  • Why are you mixing the two? You could have legitimate reasons to do so, but Swing has it's own File Chooser, and JavaFX has it's own "frame". You could code your app entirely in JavaFX and avoid this issue altogether. – SnakeDoc Jun 01 '16 at 15:39
  • True. The reason this is being pursued is because I am not a fan of the design of JFileChooser, but have a fairly large application that was built with Swing, and I am trying to graft a more native-looking file chooser into it. – jmohrmann Jun 01 '16 at 15:53
  • That's fair. I believe it shows a separate entry in task manager because the JavaFX platform is really self contained with it's own threads and what-not. The `Platform.runLater()` is likely what spawns the additional entry. I don't know if you will be able to "hide" this extra process, but my guess is you can't. – SnakeDoc Jun 01 '16 at 16:03

0 Answers0