0

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?

user2949556
  • 47
  • 1
  • 7

2 Answers2

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

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?

Community
  • 1
  • 1
rahul pasricha
  • 931
  • 1
  • 14
  • 35
  • that gave a lot idea thanks, but in that done(Void result) method why does that (Void result) stands for? In my code that part didn't worked but with an empty one "done()" everything went fine. – user2949556 May 05 '14 at 21:19
  • i edited my answer, removed the Void result. (Typo) Just an FYI, if you want to fetch the result returned in doInBackground(), you can call get() method inside done() and it will return the object of what you retured. – rahul pasricha May 05 '14 at 21:50
  • @user2949556, `but in that done(Void result) method why does that (Void result) stands for? In my code that part didn't worked but with an empty one "done()" everything went fine` that is why I gave you the tutorial link. The link contains both a working example and an explanation. You learn more by reading the tutorial and writing the code yourself than you do by copying code. – camickr May 06 '14 at 00:40