user7128116
user7128116

Reputation:

JFileChooser doesn't stop running

I've been trying to use JFileChooser but I have the problem that the program doesn't stop running, here's my code:

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class copiarArcivos {

    public static void main(String[] args) {
       JFileChooser();
    }

    public static void JFileChooser(){
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        int result = fileChooser.showOpenDialog(new JFrame());
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
        }
    }
}

Should I simply put a break at the end of the if?

Upvotes: 1

Views: 294

Answers (2)

Amit
Amit

Reputation: 31

You have to change the method name of JFileChooser in main method. and also in the declaration of this method. You can use JFileChooser2 instid of JFileChooser on both.

Upvotes: 1

camickr
camickr

Reputation: 324108

Don't create an empty JFrame. You can just use null:

//int result = fileChooser.showOpenDialog(new JFrame());
int result = fileChooser.showOpenDialog(null);

Upvotes: 2

Related Questions