1

I am building a chat room in JFrame. I want the JFrame to refresh each 200ms so that whenever a new text is entered, it will appear inside the JFrame.

I tried using while(true) but JFrame freezes.

How to add timer to the code?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Jonathan Lopez
  • 61
  • 1
  • 1
  • 4
  • 2
    `while(true)` is not a good idea, your'e tying up the EDT, that's why it get stuck. – Maroun Jun 11 '13 at 11:00
  • 1) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 11 '13 at 11:05
  • why not just refresh when new message is arrived instead of repeated refreshing? – pinkpanther Jun 11 '13 at 11:10
  • _How to add timer to the code?_ By using a `javax.swing.Timer` – Robin Jun 11 '13 at 11:11
  • How to make it refresh when message arrives? in the example there are multiple jframes, each consists of users online, chat area and message area. – Jonathan Lopez Jun 11 '13 at 11:13
  • You can use observer pattern..search about that or read this http://stackoverflow.com/questions/14623897/using-oo-observer-pattern-without-updating-object-from-which-change-originated – pinkpanther Jun 11 '13 at 11:18
  • 1
    For [example](http://stackoverflow.com/a/3245805/230513). – trashgod Jun 11 '13 at 11:30
  • awful lot of people seem to be writing JFrame chatrooms lately. Is this a homework? – fdreger Jun 11 '13 at 11:56

2 Answers2

3

I am building a chat room in JFrame. I want the JFrame to refresh each 200ms so that whenever a new text is entered, it will appear inside the JFrame.

I tried using while(true) but JFrame freezes.

How to add timer to the code?

  • disagree, JFrame can't be refreshed only JComponent (JTextComponent) and only in the case that on second side are some changes

  • 200milisecond is very short time, you can't wrote text message on this short period, I'd be to set 750milis - one second

  • use util.Timer or to start endless loop from Runnable#Thread,

  • use boolean local variable instead of while(true), e.g while(canRun), then is possible the loop to stop and start if would be need

  • all output to the Swing GUI from util.Timer / Runnable#Thread must be wrapped into invokeLater, only methods with real changes, methods from Swing APIs e.g. setText(), append() not whole method, void which is responsible to create an output, connection, etc.

  • don't to use SwingWorker, isn't proper API for endless loop, is designated to runs only once time,

  • then there is possible concurency with a few instances of SwingWorkers(invoked from any Timer or Executor), because nobody can to guarantee that every ended and aren't (a few instances) live in the same time, then best of choices is loop invoked from Runnable#Thread

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • new point for me, so jdk 7? :) by the way thread in link doesn't seems to be a question... I wonder why was it not closed :)) – pinkpanther Jun 11 '13 at 12:16
1

You can give a try to timer class as well

Timer timer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Some code here

}
});
timer.start();

for more details see How to Use Swing Timers

Hope this will help you.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110