David
David

Reputation: 1

JFileChooser opening in background

I have a problem with the JFileChooser. When I call it in the main class, it opens in the foreground without any problems. But if I want to call it via another method, it opens in the background. How can I change this?

My current code looks like this:

public static void readBookCopyCSV() {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Open");
        fileChooser.setFileFilter(new FileNameExtensionFilter("CSV file", "csv"));
        ArrayList<ArrayList<String>> bookData = new ArrayList<>();
        String filePath;

        int userSelection = fileChooser.showOpenDialog(null);

        if (userSelection == JFileChooser.APPROVE_OPTION) {
            filePath = fileChooser.getSelectedFile().getAbsolutePath();

{...}
            
        }
    }

I know it has something to do with a parent component and I tried to change some stuff but somehow it still does not work for me. If i call BookCopy.readBookCopyCSV(); in the main.java it opens in the front. But i want the main class to only contain of a 'MenuHandler' method, which is located in another class. Now if i call the readBookCopyCSV method in the MenuHandler class it opens in the background.

My main class looks like this:

public class Library {

    public static void main(String[] args) {
        
        MenuGetters.getMainMenu();
        MenuHandlers.handleMenuWithNumbers();
        //Book.readBookCSV(); 
    }

}

The commented Book.readBookCSV(); opens the file chooser in the front but that does not help me since the method should not be called in the main class. It should be called in the MenuHandlers class when the user wants to import a csv file for book copies. I have seen in another question that it works with a jframe but my program uses a cli and should not create a jframe.

Upvotes: 0

Views: 55

Answers (0)

Related Questions