Question

Sorry guys maybe it can be a silly question but really i couldn't find any similar situation like this.

Here is my code:

private void startHashingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                   

     consoleArea.setText( myFile.getName() + " started to be hashing! It can take few minutes, please wait..");       //20:05

   try {
        BufferedReader reader = new BufferedReader(new FileReader(myFile));
        myHash = new HashOA(300000);
       try {
           while(reader.readLine() != null){
               myHash.hash(reader.readLine());
           }

           consoleArea.append("\n" + myFile.getName() + " is successfully hashed!!");

       } catch (IOException ex) {
           Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
       }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
    }

}

I expect that in consoleArea(TextArea) there should be "file.txt started to be hashing! It can take few minutes, please wait.." written and after that the hashing process(that while(reader.readLine() != null) loop) should be started. But when i run the program and click on "startHashingButton" it first finishes hashing process and later it writes on console(jTextArea) --> "file.txt started to be hashing! It can take few minutes, please wait..", "file.txt is successfully hashed!!"

I'm working on a large text file and it takes a while to hash it. That's why i want to tell user he/she should wait a bit.

Why the working queue differs from my code order ?

Note: The only thing that came to my mind is to use thread, could it solve the problem?

Was it helpful?

Solution 2

Use SwingWorker to implement a worker thread. Do all the processing in doInBackground method, you can add your following code in doInBackground. Before that you can you will set the text in your console area. And once your file is hashed, you can implement done() method and set the appropriate message in your console area. To give you an idea, it will look something like this

@Override
  protected Integer doInBackground() throws Exception {
    try {
            BufferedReader reader = new BufferedReader(new FileReader(myFile));
            myHash = new HashOA(300000);
           try {
               while(reader.readLine() != null){
                   myHash.hash(reader.readLine());
               }

               return null;

           } catch (IOException ex) {
               Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
           }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void done() {
        consoleArea.append("\n" + myFile.getName() + " is successfully hashed!!");
    }

Refer this for more clarity : How do I make my SwingWorker example work properly?

OTHER TIPS

Note: The only thing that came to my mind is to use thread, could it solve the problem?

Yes, that is correct. Code executed in an event listener is invoked on the Event Dispatch Thread. The GUI can't be repainted until all the code is finished executing. So a long running task prevents the GUI from repainting itself.

Read the section from the Swing tutorial on Concurrency in Swing for more information. Maybe a SwingWorker will be a better solution than creating your own Thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top