Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes JList does not always update/refresh properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JList does not always update/refresh properly" Watch "JList does not always update/refresh properly" New topic
Author

JList does not always update/refresh properly

jeffrey derksen
Greenhorn

Joined: Feb 23, 2013
Posts: 1
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jun 13, 2009
Posts: 2153
    
    7
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

Joined: Jan 26, 2013
Posts: 56
invoke the jlist method - > setmodel()

urjlist.setmodel(modelPlayers);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JList does not always update/refresh properly
 
Similar Threads
ListCellRenderer!!!Help!!
Head First Java Ch. 2 Compile error
I have a problem with a JTable that is driving me mad!
Synchronize the way threads(players) take their turn.
ObjectStream client-server object sharing problem.