Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JList does not always update/refresh properly

 
jeffrey derksen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this game using a Client & Server with sockets. When a client starts it requests a Room list of all currently available Rooms which then the client puts in a JList using a DefaultListModel. Then when the client selects one of the Rooms it sends a request to the Server to join this Room. Then the Server replies back with an Object containing all the Players that are currently in this Room. The client then updates the JList like this:



As you can see basically all the current elements currently in the DefaultListModel are removed and then the newly received Players are added to the DefaultListModel.

But sometimes the JFrame does not update the JList properly and it appears empty but it did printout the message "..... added to the Playerlist"..

Example:
Player A connects to the Server and joins Room 0. Now the JList is always gets updated properly!!
Then Player B connects to the server and joins Room 0 as well. Then the server broadcasts the current players in Room 0 back to the Clients(Player A and B).
Player A does receive the update(according to the printed out console messages) but the JList is not updated(this is actually where the problem occurs but it does not always happen!).
Player B also receives the update and the JList is always updated properly.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Player A does receive the update(according to the printed out console messages) but the JList is not updated

possible symptom of blocking Swing's EDT
 
Rob Camick
Ranch Hand
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is actually where the problem occurs but it does not always happen!


Whenever I see random behavour I always guess that you are not updating your Swing component on the EDT.

If you are doing Client/Server processing, then somewhere you need to invoke SwingUtilities.invokeLater(...) to place the list update code on the EDT.
 
kahkean chor
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
invoke the jlist method - > setmodel()

urjlist.setmodel(modelPlayers);
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not necessary because DefaultListModel already triggers an event that should cause a visual update to the GUI. I think Michael and Rob are right. Concurrency in Swing explains it all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic