1

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.

1
  • What type of message? Commented Feb 27, 2013 at 18:28

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone, I had not thought of using a thread, I fixed it that way.
1

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(); 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.