Error in OS X with JFileChooser UI (?)

I, like many others, came across and saw this question posted on this forum endlessly; Unfortunately, he never answered at all or completely correctly. In the presence of JFileChooserUI sometimes appears irregularly. This applies to both Opening, and in the Savingfile.

For example, if I have this code: I try to read .txt and put each line in 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 tried to debug this, as well as several other programmers; unfortunately without success. I have several System.out.println()in a method that prints:

1
2

"3" is not displayed, so the problem is most likely in:

int resultat = fc.showOpenDialog(null);

, , - - .. .

+4
2

ShowOpenDialog() .
, ShowOpenDialog() AWT.
( )

, :

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

, SwingUtilities.invokeLater, SwingWorker API.

0

:

    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    try {
         File f = fc.getSelectedFile();
         gestorfile = new GestorFile(cadena, f.getCanonicalPath());
        } catch (IOException ex) {
         System.out.println("ERROR i/o");
          }
    }
0
source

Source: https://habr.com/ru/post/1615108/


All Articles