I have a method which loads data from db using Hibernate connection. I want to add to this method - progress bar - for showing when loading data will end.
this is my method for loading data:
@FXML
private JFXProgressBar progressBar;
@FXML
private JFXTextArea textArea;
@FXML
void executeQuery(MouseEvent event) {
//LOAD DATA FROM DB USING HIBERNATE AND SET TEXT TO TEXTAREA
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("firebird_config_file");
EntityManager entityManager = entityManagerFactory.createEntityManager();
TypedQuery<Programy> query1 = entityManager.createQuery("select p from Programy p", Programy.class);
String resultOfTheQuery="";
for (Programy programy : query1.getResultList() ) {
String tmp= "Nazwa programu: " + programy.getExeNazwa() + " Kod programu: " + programy.getKodProg()+" Wersja: "+programy.getWersja()+"\n";
resultOfTheQuery+=tmp;
}
textArea.setText(resultOfTheQuery);
}
I think this is difficult (if not impossible) to achieve.
If you think of an SQL query in the traditional sense, it does not know how many data sets there are and how long it will take. I don't know any SQL tool (like DB Visualizer, DataGrip, etc.) that shows you a progress bar and a correct estimation on how long it will take because it will always depend on the amount of data that is in the table.
There would need to be a way of measuring when the data is 25%/50%/75%/... loaded. And the only thing, that could take a lot of time in your code is the
entityManager.createQuery(...)
call.
I would suggest to remove the progress bar and add something like a spinner to show that the data loading is in progress. JFoenix has a nice material design spinner.
You could also add a simple "loading data ..." text.
Edit (to make it more visible):
As @Slaw correctly suggested you would also need to put your entityManager.createQuery(...)
call into a new thread. Otherwise the UI thread may freeze, when the query takes more time.