-1

I need to get filename of the imageview within a vbox upon pressing a button?? Here file arraylist has been to added to vbox, the vbox to gridpane Is it possible using vbox.getChildren()?? or else how should i proceed. here's a screenshot for better understanding:

enter image description here

here's the code:

private ArrayList<Button> btnar;
private VBox vb;
private Button downloadbtn;
@FXML
private ScrollPane displayscroll;
private GridPane gridpane;

public HomeUI_2Controller() {
   Platform.runLater(new Runnable() {

     @Override
     public void run() {
        gridpane = new GridPane();
        displayscroll.setContent(gridpane);

        btnar = new ArrayList<>();
        for (int i = 0; i < filelist2.size(); i++) {
            downloadbtn = new Button("Download");
            btnar.add(downloadbtn);
        }
        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++;
            }

        }
         **downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                System.out.println("sssss");
                System.out.println( vb.getChildren().contains(pic.getId()));
            }
        });**
   }
   });
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • You code snippet doesn't follow the JavaFX pattern. Read the [tutorial](http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm) on how to code using FXML in JavaFX ! As already pointed by @James_D, you have a controller and using `Platform.runLater()` within its constructor is really confusing. To learn more about FXML and Controllers, read through the examples of [Mastering FXML](http://docs.oracle.com/javafx/2/fxml_get_started/jfxpub-fxml_get_started.htm) – ItachiUchiha Sep 11 '14 at 06:15

1 Answers1

1

This code is really unclear, and I'm not sure I fully understand what you're doing here. (Is this a controller? If so, why are you initializing the UI in a Platform.runLater(), inside the constructor, instead of in the standard initialize() method? Why are some of your UI components apparently defined in the FXML, and others defined in the Java code in the controller? This is very hard to follow.)

But, it seems like you have a List called filelist2 (you don't show the declaration) which I'm guessing contains filenames. You create a list of Buttons of the same size. I'm guessing the idea is to add an action event handler to each button which does something with the corresponding element of the filelist2 list.

In order to do that you can add the event handler to each button inside the first for loop:

    for (int i = 0; i < filelist2.size(); i++) {
        downloadbtn = new Button("Download");
        btnar.add(downloadbtn);
        final int index = i ;
        downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // do something with filelist2.get(index);
                // ...
            }
        });
    }
James_D
  • 201,275
  • 16
  • 291
  • 322