Search code examples
databasejavafxblobprogress-indicator

JavaFX - "Progress Window"


I would like to show a little "Progress Window", contained in a FXML while i put some big BLOBs in a Database.

The Problem is: The Window won't appear till the Insert is executed.

I thought maybe it would be the problem that the FXML loader is too "slow"? And can't display the window because of that.

I also tried adding a ProgressIndicator directly into the FXML of my form and make it just visible when the action button is pressed.

Same thing - the .setVisible Method won't work until the Insert is done.

This is very very very hair-raising Problem for me.

I'm developing a Management Tool and I can't let the user insert a big File and give him no response if something is in Progress or not!?

Would be fine if anyone has a solution for this problem!

Best Thanks!


Solution

  • Well... after the hint from Uluk Biy I started trying around. After 6 hours I did the trick like this:

    This is my Method, that returns the needed Task.

    public static Task saveNewAccoounting(final Accounting a){
        Task<Integer> saveNewAccountingTask = new Task<Integer>() {
            @Override protected Integer call() throws Exception {
                try {
                    run.logger.log(Level.FINE, "Started saveNewAccounting Task!");
                    PreparedStatement newAcc;
                          newAcc = Connect.con.prepareStatement("INSERT INTO `ACCOUNTING`(`ACCNUMBER`, `ACCDATE`, `SHOP`, `CUSTOMER`, `CATEGORIES`, `WRITTENBY`, `DATE`, `DESCRIPTION`, `NOTE`, `PRICE`, `FILEORIGINAL`, `FILEBYTE`) VALUES (null,null,?,?,?,?,?,?,?,?,?,?)");
                          newAcc.setInt(1, a.getShop());
                          newAcc.setInt(2, a.getCustomer());
                          newAcc.setInt(3, a.getCategories());
                          newAcc.setInt(4, Credentials.getUserid());
                          newAcc.setDate(5, a.getDate());
                          newAcc.setString(6, a.getDescription());
                          if (a.getNote().equals("")){
                            newAcc.setNull(7, java.sql.Types.VARCHAR);
                          } else {
                            newAcc.setString(7, a.getNote());    
                          }
                          newAcc.setDouble(8, a.getPrice());
                          if(a.getAttachment() != null){
                              newAcc.setString(9, a.getAttachment().getName());
                              newAcc.setBlob(10, new FileInputStream(a.getAttachment()));
                          } else {
                              newAcc.setNull(9, java.sql.Types.VARCHAR);
                              newAcc.setNull(10, java.sql.Types.BLOB);
                          }
                          if(!newAcc.execute()){
                              newAcc.close();
                              run.logger.log(Level.FINE, "Successfully completed saveNewAccounting Task!");
                              return 1;
                          } else {
                              newAcc.close();
                              run.logger.log(Level.FINE, "Completed saveNewAccounting Task with Failures!");
                              return 0;
                          }
                } catch (SQLException ex) {
                    run.dblogger.log(Level.SEVERE, ex.getMessage());
                    run.logger.log(Level.FINE, "Completed saveNewAccounting Task with Failures!");
                    return 0;
                } catch (FileNotFoundException f){
                    run.iologger.log(Level.SEVERE, f.getMessage());
                    run.logger.log(Level.FINE, "Completed saveNewAccounting Task with Failures!");
                    return 0;
                }
            }
        };
       return saveNewAccountingTask;
    }
    

    All you have to do then is instantiate a Thread with the returned Task and execute it.

    //Make your Progress visible here
    final Task saveTask = SaveTasks.saveNewAccoounting(acc);
    Thread saveThread = new Thread(saveTask);
    saveThread.setDaemon(true);
    saveThread.start();
    

    Now add a Listener to the Task and stuff is done.

    saveTask.setOnSucceeded(new EventHandler() {
                       @Override
                        public void handle(Event t) {
                            Progress.close();
                            if (saveTask.getValue() == 1){
                                //SUCCESS
                            } else {
                                //FAIL
                            }
                        }
                    });
    

    Hope this will help somebody in future.