Java 按下按钮后获取图像的相应文件名

Java 按下按钮后获取图像的相应文件名,java,file,button,javafx,filenames,Java,File,Button,Javafx,Filenames,将一组ImageFile添加到File类型的arraylist(filelist2)中。然后将imageview和按钮添加到vbox中,这样的vbox使用for循环添加到gripane的网格中。(迭代次数等于filelist2的大小)一旦按下按钮,我需要在该vbox中获取相应的图像文件名。 假设我按下了(1,1)中包含的按钮{e row no01,col no1},我需要获取(1,1)中图像的文件名 以下是一个屏幕截图: 这是我的代码:FXMLController File file = ne

将一组ImageFile添加到File类型的arraylist(filelist2)中。然后将imageview和按钮添加到vbox中,这样的vbox使用for循环添加到gripane的网格中。(迭代次数等于filelist2的大小)一旦按下按钮,我需要在该vbox中获取相应的图像文件名。 假设我按下了(1,1)中包含的按钮{e row no01,col no1},我需要获取(1,1)中图像的文件名 以下是一个屏幕截图:

这是我的代码:FXMLController

 File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
            File[] filelist1 = file.listFiles();
            ArrayList<File> filelist2 = new ArrayList<>();

            for (File file1 : filelist1) {
                filelist2.add(file1);

            }
            btnar = new ArrayList<>();
            for (int i = 0; i < filelist2.size(); 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");                             
                            downloadbtn.getId();
                            //System.out.println(filelist2.get(Integer.valueOf(downloadbtn.getId())).getName());   

                        } catch (Exception ex) {
                            Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
                        }


                    }
                });
            }

            System.out.println(filelist2.size());
            gridpane.setAlignment(Pos.CENTER);
            gridpane.setPadding(new Insets(20, 20, 20, 20));

            gridpane.setHgap(20);
            gridpane.setVgap(20);

            ColumnConstraints columnConstraints = new ColumnConstraints();
            columnConstraints.setFillWidth(true);
            columnConstraints.setHgrow(Priority.ALWAYS);
            gridpane.getColumnConstraints().add(columnConstraints);

            int imageCol = 0;
            int imageRow = 0;

            for (int i = 0; i < filelist2.size(); i++) {
                System.out.println(filelist2.get(i).getName());

                image = new Image(filelist2.get(i).toURI().toString());

                pic = new ImageView();
                pic.setFitWidth(130);
                pic.setFitHeight(130);


                pic.setImage(image);
                vb = new VBox();
                vb.getChildren().addAll(pic, (Button) btnar.get(i));

                gridpane.add(vb, imageCol, imageRow);
                GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
                imageCol++;

                // To check if all the 3 images of a row are completed
                if (imageCol > 2) {
                    // Reset Column
                    imageCol = 0;
                    // Next Row
                    imageRow++;
                }

            }
File File=new文件(“D:\\SERVER\\SERVER Content\\Apps\\icons”);
File[]filelist1=File.listFiles();
ArrayList filelist2=新的ArrayList();
对于(文件file1:filelist1){
filelist2.add(file1);
}
btnar=新的ArrayList();
对于(int i=0;i2){
//重置列
imageCol=0;
//下一排
imageRow++;
}
}

考虑使用
java.util.HashMap
并调用
HashMap.get(actionEvent.getSource()).getName()
来获取文件名。

使用并在节点中存储和检索自定义值!将
文件名
设置为用户数据,然后单击,检索它

downloadbtn.setUserData(filelist2.get(index).getName());
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent arg0) {
            System.out.println(downloadbtn.getUserData());   
     } 
下载btn.setUserData(filelist2.get(index.getName());
downloadbtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent arg0){
System.out.println(downloadbtn.getUserData());
} 
为什么不简单

System.out.println(filelist2.get(index).getName());
?

(实际上,我不太清楚为什么要创建
filelist2
。为什么不呢

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);
                }


            }
        });
    }
btnar=newarraylist();
for(int i=0;i
我创建了一个可以保存一些类型化数据的数据库(不像userData有type对象)。 您可以指定一个渲染器来渲染按钮上的数据或渲染替代文本,例如,在您的示例中:“下载”

你可以用这样的东西:

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");
    }
} 
但是,您必须确保始终在setData之后调用setText

该库是开源的,可从Maven Central获得:

<dependency>
    <groupId>org.drombler.commons</groupId>
    <artifactId>drombler-commons-fx-core</artifactId>
    <version>0.4</version>
</dependency>

org.drombler.commons
drombler commons fx核心
0.4
<dependency>
    <groupId>org.drombler.commons</groupId>
    <artifactId>drombler-commons-fx-core</artifactId>
    <version>0.4</version>
</dependency>