• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 782
  • Last Modified:

JList wont update no matter what i do.

I currently have an application with a gui (see attached picture) containing some JLists of which the one of them is made for reporting server reports. The Jlists are initialized with 3 DefaultListModels contained by a model class (that keeps the state of the app). The problem is updating the JList with server data gathered in a seperate thread, which simple wont work no matter what i try it seems.

Adding items to the Jlist works fine by manipulating the DefaultListModel instance referenced by the JList. However as soon as I start the server listening thread it doesnt work anymore. I have tried using the SwingUtilities.invokeLater method and using a property change listener approach where I fire a property cahnge event and take care of things in the propertyChanged method, which works for the status panel (which is updated by the listen thread - see picture). I can't figure out why it wont update the gui, I mean the DefaultListModel object is updated with a lot of elements, but nothing shows. I really hope someone can help, this is truly a pain in the ass.
gui.JPG
0
Suppai
Asked:
Suppai
  • 7
  • 5
  • 3
1 Solution
 
CEHJCommented:
Well, at least it looks good ;-) As i mentioned earlier, you could also use a SwingWorker. Perhaps you'd better attach some (compilable )source files
0
 
SuppaiAuthor Commented:
I have sent you the source code as an eclipse project to the email provided in your profile info - a bad thing to publish a school project, although my lecturer encouraged us to use all the sources of info possible like in the real world.
0
 
CEHJCommented:
Make sure you're not ignoring any exceptions
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
SuppaiAuthor Commented:
Yeah I know the exception handling is not great yet, but Ill take care of the polishing when the main functionality is done. I actually made it work just now refering directly to the DefaultListModel from the while loop like this:

link.getReportList().addElement(record);

Why is this different from calling:

link.addReportRecords where the same is done just in the code of the ServerLink class instead of directly in the thread loop? Im new to java so I probably got some object referencing basics wrong, but it still confuses me a bit.
if (report.getType().equals(ServerResponse.RES_REPORT)) {
					link.setStatusText(new Date().toString() + ": " + report.getListRecords().firstElement());	
					for (Iterator<String> iterator = report.getListRecords().iterator(); iterator.hasNext();) {
						String record = iterator.next();
						link.getReportList().addElement(record);
					}				} else if(report.getType().equals(ServerResponse.RES_MSG)){
					link.setStatusText(report.getMsg());
				}

Open in new window

0
 
objectsCommented:
> I have sent you the source code as an eclipse project to the email provided in your profile info

that is not permitted at EE, please post all the source here

0
 
objectsCommented:
Make sure you are updating the same instance of the list as the one you have added to your gui.
0
 
SuppaiAuthor Commented:
theres only one instance set as field in the model object of which theres also only one instance, so that couldnt be it, but i guess i just have to do it directly in the thread code then.

I didnt know it wasnt permitted. I won't happen again.
0
 
objectsCommented:
>  but i guess i just have to do it directly in the thread code then.

what do you mean by that?
can u post your code please

0
 
objectsCommented:
see the following, it discusses updating your gui from a different thread

http://helpdesk.objects.com.au/java/how-do-i-update-my-gui-from-any-thread

0
 
objectsCommented:
assuming that code is not running on the swing thread then the code should be something like the following.

            if (report.getType().equals(ServerResponse.RES_REPORT)) {
                  link.setStatusText(new Date().toString() + ": "
                              + report.getListRecords().firstElement());
                  for (final String record ; report.getListRecords()) {
                        EventQueue.invokAndWait(new Runnable() {
                              public void run() {
                                    link.getReportList().addElement(record);
                              }
                        });
                  }

0
 
SuppaiAuthor Commented:
I found out that the reason it wasnt working was that i was replacing the DafaultListModel with a newly created one in the link.addReportRecords method. When merely manipulating the existing list it worked out fine. Thanks for your input.
0
 
objectsCommented:
which is what I posted earlier??  Why did you accept a comment that had nothing to do with the problem? All that does is confuse anyone who looks at this question in the future with a similar problem.  
You should have accepted the comment that I posted that told you what the problem was :)


0
 
SuppaiAuthor Commented:
Yes you were right I was just trying to get the point allocated as fast as possible as Im in a bit of a hurry and didnt think much about marking the right posts.
0
 
CEHJCommented:
>>I found out that the reason it wasnt working was that i was replacing the DafaultListModel with a newly created one in the link.addReportRecords method.

That actually shouldn't make much difference as long as you set the new one as the model of your JList
0
 
objectsCommented:
the accepted comment should be:
"Make sure you are updating the same instance of the list as the one you have added to your gui."
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now