0

I'm using the JFileChooser in the Java Swing library to save a file when user select a directory. It works fine for the purpose. However, after the user has either selected the directory and saved the file or pressed cancel, the icon sticks to my taskbar even if it's closed. This is quite annoying, because if I repeat the process, it does not open freshly again but I have to click on the icon in the taskbar to show the new frame. Is there a way to fix this? I can't seem to find any working solution.

Code:

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.util.*;

public class Client {

    // Main Method, taking args from terminal

    public static void main(String[] args) {
        // parent component of the dialog
        JFileChooser chooser = new JFileChooser();
        String save_location = "";
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("choosertitle");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            save_location = chooser.getSelectedFile().getAbsolutePath();
            System.out.println(save_location);
        } else {
            System.out.println("You must select a destination folder. Please try again");
        }                   
}
7
  • 1
    A JFileChooser is a JDialog. A JDialog does not display on the task bar, only a JFrame is added to the task bar. Post your minimal reproducible example that demonstrates the problem. We should be able to copy/paste/compile and test to verify if we have the same problem. And remove all the logic not directly related with the file chooser to determine if the problem is the file chooser or the other processing of your app.
    – camickr
    Mar 6, 2020 at 21:04
  • @camickr Updated. Thanks
    – nTuply
    Mar 6, 2020 at 21:07
  • The code runs as I suggested. The file chooser is displayed, but there is no icon added to the taskbar. I use JDK 8 on Windows 7. Maybe you have a version/platform issue. Also, as a side note all GUI components should be create on the Event Dispatch Thread (EDT). The code you posted should be wrapped in a SwingUtilities.invokeLater(…). Don't know if it will make a difference for your problem.
    – camickr
    Mar 6, 2020 at 21:12
  • @camickr I use a mac with JDK 8. I'm pretty sure it's probably a mac issue, but it's weird since I can't find a work around to this.
    – nTuply
    Mar 6, 2020 at 21:14
  • @camickr Regarding your edit, can you maybe give an example?
    – nTuply
    Mar 6, 2020 at 21:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.