Java – error (?) in OS X with Jfilechooser UI

Like many others, I have encountered and seen this problem endlessly in this forum Unfortunately, I have never answered, and generally I have not finished

For example, if I have this Code: I try to read it Txt and put each row into an array (allanamn)

public static void getFile() {
    try {
        System.out.println("1");
        String aktuellMapp = System.getProperty("user.dir");
        JFileChooser fc = new JFileChooser(aktuellMapp);
        System.out.println("2");
        int resultat = fc.showOpenDialog(null);
        System.out.println("3");
        if (resultat != JFileChooser.APPROVE_OPTION) {
            JOptionPane.showMessageDialog(null,"No file choosen.");
            NamnProgram.main(null);
        }

        String fil = fc.getSelectedFile().getAbsolutePath();

        BufferedReader inFil = new BufferedReader(new FileReader(fil));                 
        String rad = inFil.readLine();

        int counter = 0;
        while (rad != null) {
            rad = inFil.readLine();
            counter++;
        }
        if (counter == 0) {
            JOptionPane.showMessageDialog(null,"Textfil is empty");
        }
        BufferedReader skrivFil = new BufferedReader(new FileReader(fil));
        allaNamn = new String[counter]; 
        int antal = 0;        
        String sparaRad = skrivFil.readLine();
        while (antal < counter) {
            allaNamn[antal] = sparaRad;
            sparaRad = skrivFil.readLine();
            antaL++;
        }       
        //Closing
        inFil.close();      
        skrivFil.close();       
    }
    catch (IOException e1) {
        JOptionPane.showMessageDialog (null,"Det misslyckades");
    }

}

I've tried debugging this and some other programmers; Unfortunately, there was no success Some of the methods I print are system out. println():

1
2

It is worth noting that the program does not close, but continues to run without any display - no errors, etc Thank you for your help

Solution

Showopendialog() stops the thread and waits for user input

If so – you can try calling:

SwingUtilities.invokelater(new Runnable() {
     public void run() {
         getFile();
     }
 });

Even better than simply using swingutilities Invokelater is better, using the swingworker API

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>