0

I have the following UI

enter image description here

So , you insert a keyword and then press "Cerca" (it's like Search in Italian) , then the Google Custom Search Api show the first 10 pictures. During the Custom search API are processing the results (the pictures) I want to show an other picture like this enter image description here

(I know it's big but the dimension is not the main point now). My idea is simple, I want to put the picture one "level"(don't know exactly how to call ) over the UI, then the picture will be not visible in 3 case: 1) When the API will end their job 2)If I don't have results 3) If I get an exception. My question is, which is the best approach to do this ? And then, Should I use Threads?

I hope I was clear

UPDATE:

This is the code of "cerca" button

cerca.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {

//                      Task<Boolean> task = new Task <Boolean>(){
//
//                      @Override
//                      protected Boolean call() throws Exception {
//                          // TODO Auto-generated method stub
//                          return null;
//                      }
//                      
//                      
//                  };




                    String searchKeyWord = userTextField.getText();
                     result = getSearchResult(searchKeyWord);


                    for ( i=0; i<result.size(); i++)

                {
                        System.out.println("" +result.get(i)); 
                        ImageView resultview;
                        resultview = new ImageView(result.get(i)); 
                        resultview.setFitWidth(130);
                        resultview.setFitHeight(130);
//                      resultview.setStyle("-fx-border:6; -fx-border-color: green;");   

                          if(j==4)
                                  {
                                    j=0;
                                    k++;
                                  }



                           resultgrid.add(resultview, j,k );
                           j++;





                    VBox vbox = new VBox();
                    resultgrid.setHgap(50);
                    resultgrid.setVgap(50);
//                  resultgrid.setStyle("-fx-border:1; -fx-border-color: red;");
                    vbox.getChildren().add(resultgrid);
                    vbox.setSpacing(10);
                    vbox.setPadding(new Insets(90, 0, 10, 220)); //TOP RIGHT BOTTOM LEFT
//                  content.setAlignment(resultgrid, Pos.TOP_RIGHT);
                    getChildren().add(vbox);


                    final int ind = i;
                    resultview.setOnMouseClicked((ev) ->{


                         if (ev.getClickCount()==2)
                         {





                             image = SwingFXUtils.fromFXImage(resultview.getImage(), null);
                            parent.setCrop(image);



                }


                });


                }

                }
            });
user2556079
  • 624
  • 3
  • 9
  • 23

1 Answers1

2

Have a look at Task. It has setOnSucceeded() and setOnFailed() which will come handy in your case.

You can basically create a new Task when the search takes place. The search will run in background and you can show the loading UI in the screen.

  • If the task completes successfully, you can load the new screen with the results.

  • If the task fails, you can show an error message near the search TextField.

  • In case of exception, you can catch it and write the necessary code.

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • Hi, nice explanation but I need an example to understand. At the moment I just create the task, like this `Task task = new Task (){ @Override protected Boolean call() throws Exception { // TODO Auto-generated method stub return null; } };` – user2556079 Dec 04 '15 at 11:11
  • @IItachiUchiha I just put the code about the "cerca" button, so in this way we can better understand each other. I have commented the code that I wrote you above – user2556079 Dec 04 '15 at 11:18
  • 1
    @user2556079 Creating a task inside the buttons action event is not a very good idea IMO since you are pushing a lot of code inside it. You can create a task and just start it inside the action event. Moreover, you also need to override the `setOnSucceeded()` and `setOnFailed()` which makes your event clumsy. – ItachiUchiha Dec 05 '15 at 08:52
  • You can see the use of both the said methods in [this answer](http://stackoverflow.com/questions/33658082/javafx-8-dialog-and-concurrency/33658178#33658178) . – ItachiUchiha Dec 05 '15 at 08:55