Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

how to use wait/notify with table model?

Thread view: 
Jeff - 16 Dec 2003 20:43 GMT
My application sends data to the gui frequently, but not constantly.  So
far, my implemenation of wait/notify on a linked list of arriving data has
worked fine.  Now I need to update a JTable based on incoming data.  I'd
like the JTable cell values to change based on incoming data.  However, I'm
uncertain where to put run method with the wait.

Most of my GUI objects have the following code:
public void run() {
      synchronized( myQueue ) {
                       try {
                           myQueue.wait();
                       }
                       catch (InterruptedException e) {
cat.warn("Exception: " + e + " while waiting on myQueue"); }

                       while ( myQueue.size() > 0 ) {

                           object = myQueue.removeLast();
                           // *** update the gui with object ***
                       }
     }
}

Should I create a tableModel that implements runnable and uses setValueAt to
update cells?  For example:

public class myTableModel extends AbstractTableModel implements Runnable {

public void run() {
          synchronized( myQueue ) {
                       try {
                           myQueue.wait();
                       }
                       catch (InterruptedException e) {
cat.warn("Exception: " + e + " while waiting on myQueue"); }

                       while ( myQueue.size() > 0 ) {

                           object = myQueue.removeLast();
                           // **** call setValueAt( object )
                       }
             }
}

Thanks
Jeff
--
Todd Corley - 17 Dec 2003 18:18 GMT
> My application sends data to the gui frequently, but not constantly.  So
> far, my implemenation of wait/notify on a linked list of arriving data has
[quoted text clipped - 43 lines]
> Jeff
> --

Swing is not thread safe.  If you are going to update a swing
component use:

javax.swing.SwingUtilities.invokeLater( Runnable runnable )

Your actual usage will look something like :
while ( myQueue.size() > 0 ) {

                            object = myQueue.removeLast();
                            SwingUtilities.invokeLater(new Runnable()
                            {
                              public void run()
                              {
                                // **** call setValueAt( object )
                              }
                            });

                        }

InvokeLater runs the runnable in the event thread.  Which is where
swing updates are supposed to happen.

Hope this helps,
Todd


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.