0

I'm new to posting questions, so help me out if I post this wrong.

I'm a student learning Java and Android. Currently, I'm building a JavaFX application with FXML/Controllers. I haven't hit any roadblocks until now.

I have a section in my code that reads a file, parses it, and puts data into a JavaDB (embedded Derby) database. This part works fine, but if the file is large, it will take about 15 seconds to finish. During this time, the user can click on other things in the GUI. I wanted to alert the user and prevent further menu clicking until it finishes. I made a new pane that becomes visible, overlaying the controls, when the update button is clicked. It works, but the GUI pane does not become visible until the file reading is finished. It does this even though the pane visibility code is before the file reading code. I need it to show before the file is read.

Can anyone help?

@FXML
AnchorPane paneWait;

@FXML
public void btnUpdateNumberSetHandle(ActionEvent event) {

    System.out.println("button: update number set");

    // download the file
    if (numberSetMetaData.getUrlImport().length() > 0) {

        // this turns on the "please wait" pane
        // this does not show while file is read
        this.paneWait.setVisible(true);

        try {
            System.out.println("Attempting to download latest numbers from: "+numberSetMetaData.getUrlImport());
            URL website = new URL(numberSetMetaData.getUrlImport());
            String saveTo = userConfigurations.getTempFilesFolder()+"/numberSet"+numberSetID;
            Path tempFolder = Paths.get(saveTo);
            Files.copy(website.openStream(), tempFolder, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("Successfully saved to: "+saveTo);

            // the file is read here and info is saved into database
            fileToDatabase.readFileToDatabase(saveTo, numberSetID);

        } catch (IOException ex1) {
            System.out.println("Problem downloading updated number set from: "+numberSetMetaData.getUrlImport());
            ex1.printStackTrace();
        }

        // this turns off the "please wait" pane
        this.paneWait.setVisible(false);
    }


}
Nickety
  • 11
  • 4
  • 1
    See if http://stackoverflow.com/questions/30249493/using-threads-to-make-database-requests helps – James_D Oct 04 '15 at 01:11
  • You are reading the file in the UI thread. This is not a good idea. Delegate it to a background thread and do it there as @James_D suggests with his link. – hotzst Oct 04 '15 at 07:30
  • @James_D thank you guys. This is exactly what I was needing. I read up on threads and applied it to the app. Works great. – Nickety Oct 05 '15 at 07:11

0 Answers0