0

I made a simple application that automatically uploads/downloads files to and from a server given that there are files to upload or download. I am using a Timer Task to periodically check if there are files locally to upload, or files online to download.

Everything is working fine, and I decided that every time that there is a successful upload/download, I'll show a message dialogue to show the user that a file was successfully uploaded/downloaded.

So far, this is what I have, I am calling this function infoBox. Note that I am doing this in the TimerTask:

public static void infoBox(String infoMessage, String titleBar){
    Toolkit.getDefaultToolkit().beep();
    JOptionPane.showMessageDialog(null, infoMessage, titleBar,  
                                 JOptionPane.INFORMATION_MESSAGE);
}

Then based on whether or not I got a file or I sent a file, I call it as such:

infoBox("Files Sent!", "Files were sent successfully!");

Or

infoBox("Files Received!", "Files were downloaded successfully!");

And it works fine. If I get a file from the server, or upload a file to it, the message pops up fine.

However, it seems that the TimerTask stops whenever it shows the message dialogue. I have to click "Ok" for the dialogue to close and for the TimerTask to do its thing again.

What I want to happen is for the TimerTask to execute over and over again, regardless how many message dialogues it has shown.

I have a feeling I misplaced where I declared and called my infoBox function. Is there a way for a programs routine task to continually execute as I show message dialogues?

halfer
  • 19,824
  • 17
  • 99
  • 186
Razgriz
  • 7,179
  • 17
  • 78
  • 150
  • JOptionPane.showMessageDialog() is used to get user response so your thread has been halted. Consider writing into a log or text area so users can scroll through. – Minh Kieu May 25 '17 at 09:05
  • @MinhKieu I need to show the user visually. I am already writing to the console window if files are getting downloaded/uploaded, which acts like a log. – Razgriz May 25 '17 at 09:29
  • Then use JLabel or another dialouge box that does not require the user to acknowledge. – Minh Kieu May 25 '17 at 10:53
  • @MinhKieu Yeah that's the entire point of this question, to find a way to may JLabel not block my main thread or an alternative library that would show a dialogue that does not block my main thread. – Razgriz May 25 '17 at 12:12

1 Answers1

0

Found an answer here.

Just run the code in a thread, and it won't stop the main thread any more.

public static void infoBox(String infoMessage, String titleBar){
  Thread t = new Thread(new Runnable(){
        public void run(){                  
            Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(null, infoMessage, titleBar, JOptionPane.INFORMATION_MESSAGE);
        }
    });
  t.start();

}

Razgriz
  • 7,179
  • 17
  • 78
  • 150