• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JList does not always update/refresh properly

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
invoke the jlist method - > setmodel()

urjlist.setmodel(modelPlayers);
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic