Question

I'm making a chat program with Java, and I'm doing the communication through TCP and serializable objects. The chat can exchange files, too, but when the files are too big (video files) the interface freezes until it completes the file loading and object transmition process. I need to show a message while the file is being loaded. This is my code:

File file = jFileChooser1.getSelectedFile();
String name= fichero.getName();
int size = (int)fichero.length();
byte[] byteArray = new byte[size];
try {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    bis.read(byteArray);               
} catch (IOException ex) {
    Logger.getLogger(Explorador.class.getName()).log(Level.SEVERE, null, ex);
}

I need to show a message while this line "bis.read(byteArray);" is reading, since I believe that line is the reason the interface freezes. After the byteArray is loaded I put it on a object and send it through the socket connection.

I also need a video streaming, not live, just to open a video file on the server and send it by pieces to the connected clients and play it. Can anyone give me tutorials or tips? thanks.

Was it helpful?

Solution

That UI freezing indicates that you are blocking Swing's Event Dispatch Thread. You want to perform all time-consuming tasks explicitly in other threads instead. One simpler way is to use SwingWorker.

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

OTHER TIPS

You can put your code in a Thread and code a method waiting for it to complete.

new Thread(new Runnable() {

        @Override
        public void run() {
             //Code here
        }
    }).start();

You can perform your tasks in a thread:

new Thread(new Runnable() {

        @Override
        public void run() {
             //Code here
        }
    }).start();

And use runlater() to update your view.

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