A Set of imagefiles are added to an arraylist(filelist2) of type File.Then an imageview and a button are affffded to a vbox,such vboxes are added to a grids of a gripane using a
Why not simply
System.out.println(filelist2.get(index).getName());
?
(Actually, it's not really clear to me why you create filelist2
at all. Why not do
btnar = new ArrayList<>();
for (int i=0; i < filelist1.length; i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
System.out.println(filelist1[index].getName());
} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
Consider using a java.util.HashMap<Button, File>
and calling hashMap.get(actionEvent.getSource()).getName()
to get the file name.
I've created a DataButton which can hold some typed data (unlike userData which has the type Object). You can specify a Renderer to render the data on the button or to render an alternative text, eg. in your case: "Download".
Eg. you could use something like this:
List<Path> pathlist2 = new ArrayList<>();
...
// provide language specific text for "Download"
ResourceBundle myResourceBundle = ...;
...
DownloadRenderer downloadRenderer = new DownloadRenderer(myResourceBundle);
...
// the dafault renderer would set the text property to path.toString()
DataButton<Path> downloadbtn = new DataButton<>(downloadRenderer);
downloadbtn.setData(pathlist2.get(index));
downloadbtn.setOnAction((actionEvent) -> {
Path path = downloadbtn.getData();
...
});
...
private static class DownloadRenderer extends AbstractDataRenderer<Object> {
private final ResourceBundle myResourceBundle;
public DownloadRenderer(final ResourceBundle myResourceBundle) {
this.myResourceBundle = myResourceBundle;
}
@Override
public String getText(Object item) {
return myResourceBundle.getString("downloadbtn.text");
}
}
As you can see, you can work directly with Path objects (which should be preferred to the legacy File objects). You don't have to cast or convert the data.
Note: you could also omit the DownloadRenderer and set the text property directly:
downloadbtn.setData(pathlist2.get(index));
downloadbtn.setText(myResourceBundle.getString("downloadbtn.text"));
But then you have to make sure to call setText always after setData.
The library is Open Source and is available from Maven Central:
<dependency>
<groupId>org.drombler.commons</groupId>
<artifactId>drombler-commons-fx-core</artifactId>
<version>0.4</version>
</dependency>
Use setUserData and getUserData to store and retrieve custom values in Nodes ! Set the fileName
as the userdata and on click, retrieve it.
downloadbtn.setUserData(filelist2.get(index).getName());
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println(downloadbtn.getUserData());
}