Reputation:
Recently I had to use a filechooser in one of my projects. I made a frame with 8 buttons, each opening a filechooser to set some Strings.
The buttons have names from "RA1" - "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 choose the files and after that I want the data to be send to another class, for testing puposes I just wanted to print the path, but it won't really work. When clicking on one of the buttons the filechooser pops up but after clicking on "open" it just brings up another one.
This happens 8 times and after that, when I press the button "finish" I get an output like this:
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" too.
I selected "RA8" as the last folder. Now to my questions:
Thanks of your help!
Upvotes: 4
Views: 90
Reputation: 1088
This might help you:
I made one ActionListener
calling the method doSomething()
with its calling JButton
as argument
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
doSomething((JButton)e.getSource());
}
};
The ActionListener
will be added to all JButtons
.
RA1.addActionListener(al);
RA2.addActionListener(al);
...
RA8.addActionListener(al);
finish.addActionListener(al);
doSomething()
looks like this (shortened to 3 buttons to keep it clean):
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);
}
}
Upvotes: 0