2014-09-11 35 views
0

将一组图像文件添加到File类型的数组列表(filelist2)中。然后将图像视图和按钮添加到vbox,这样的vbox被添加到使用gripane的网格一个for循环(迭代次数等于filelist2的大小)一旦按下按钮,我需要获取该vbox中图像的相应文件名。 说我压载(1,1)按钮{即行NO01,山坳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++; 
       } 

      } 
+0

的[setOnAction则不会触发(可能重复http://stackoverflow.com/questions/25758255/setonaction-is-not-触发)(你已经接受了一个答案,这就是你在这里所要求的) – 2014-09-11 17:14:26

回答

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


      } 
     }); 
    } 
0

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

1

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

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

我创建了一个DataButton可容纳一些类型的数据(与用户数据具有Object类型不是真正清楚,我。) 您可以指定一个渲染器渲染按钮上的数据或提供替代文本,例如,在你的情况:“下载”

例如,你可以使用这样的:。

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和文本属性直接设置:

downloadbtn.setData(pathlist2.get(index)); 
downloadbtn.setText(myResourceBundle.getString("downloadbtn.text")); 

但你必须确保始终使用setData之后调用的setText。

库是开源的,可以从Maven的中央:

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