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

时间:2014-09-11 06:39:07

标签: java file button javafx filenames

将一组图像文件添加到File类型的arraylist(filelist2)中。然后将一个imageview和一个按钮添加到vbox中,使用for循环将这些vbox添加到gripane的网格中。(迭代次数)等于文件列表的大小2)按下按钮后,我需要获取该vbox中图像的相应文件名。 假设我按下了(1,1)所包含的按钮{即行no01,col no1}我需要获取图像的文件名(1,1)  这是一个截图:enter image description here

这是我的代码: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++;
                }

            }

4 个答案:

答案 0 :(得分:1)

使用setUserDatagetUserData在节点中存储和检索自定义值!将fileName设置为userdata,然后单击,检索它。

downloadbtn.setUserData(filelist2.get(index).getName());
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent arg0) {
            System.out.println(downloadbtn.getUserData());   
     } 

答案 1 :(得分:1)

为什么不简单

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


            }
        });
    }

答案 2 :(得分:0)

考虑使用java.util.HashMap<Button, File>并调用hashMap.get(actionEvent.getSource()).getName()来获取文件名。

答案 3 :(得分:0)

我创建了一个DataButton,它可以保存一些类型化的数据(与具有Object类型的userData不同)。 您可以指定渲染器来渲染按钮上的数据或渲染替代文本,例如。在你的情况下:“下载”。

EG。你可以使用这样的东西:

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

如您所见,您可以直接使用Path对象(应该优先于旧的File对象)。您不必转换或转换数据。

注意:您也可以省略DownloadRenderer并直接设置text属性:

downloadbtn.setData(pathlist2.get(index));
downloadbtn.setText(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>
相关问题