JFileChooser issues with selecting folders

I recently had to use filechooser in one of my projects. I took a frame with 8 buttons, each of which opened a file file to set some lines.

The buttons have names from "RA1" to "RA8".

So this is what I have:

FileChooser Method:

public File openDataBrowser() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));

    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int result = fileChooser.showOpenDialog(fileChooser);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        return selectedFile;
    }

    return new File("");
}

ActionListener:

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(RA1)) path1 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA2)) path2 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA3)) path3 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA4)) path4 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA5)) path5 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA6)) path6 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA7)) path7 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(RA8)) path8 = openDataBrowser().getAbsolutePath().replace("\\", "/");
    else if (e.getSource().equals(finish)) {
        System.out.println(path1);
    }
}

So, first I want to select the files, and after that I want the data to be sent to another class, for testing the dolls, I just wanted to print the path, but it will not work. When you click on one of the buttons, the file file appears, but after you click "open," it simply opens another one.

This happens 8 times, and after that, when I click the "finish" button, I get this output:

C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8
C:/Users/edv.BORBET/Desktop/Roentgen Auswertung neu/RA8

My folders have names from "RA1" - "RA8".

"RA8" . :

  • ?
  • 8 ?
  • ?

!

+4
1

:

ActionListener doSomething() JButton

ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        doSomething((JButton)e.getSource());
    }
};

ActionListener JButtons.

RA1.addActionListener(al);
RA2.addActionListener(al);
...
RA8.addActionListener(al);
finish.addActionListener(al);

doSomething() ( 3 , ):

protected void doSomething(JButton src) {
        if (src.equals(RA1)) path1 = openDataBrowser().getAbsolutePath().replace("\\", "/");
        else if (src.equals(RA2)) path2 = openDataBrowser().getAbsolutePath().replace("\\", "/");
        else if (src.equals(RA3)) path3 = openDataBrowser().getAbsolutePath().replace("\\", "/");
        else if (src.equals(finish)) {
             System.out.println(path1);
             System.out.println(path2);
             System.out.println(path3);
      }
}
+1

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


All Articles