Consultor Eletrônico



Kbase 19719: OPEN CLIENT: Java Code to Update JList Via Thread (SDO)
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   5/20/2006
Solution ID: 19719

GOAL:

How to update the data in a Jlist with data from a SmartDataObject (SDO) using the Java SDOAdapter through the Progress AppServer.

FACT(s) (Environment):

Progress 9.1x

SYMPTOM(s):

AppServer

Java

Open Client

FIX:

The code assumes you have a Swing based application called SampleApplication (a Java-based graphical user interface) and that the application makes a connection to the Progress AppServer using the SDOAdapter class.

This code uses a self-contained background (daemon) thread to access
the AppServer and retrieve the data from the SDO. The code kicks off
a second thread that is scheduled for execution on the AWT Event
thread (this is the proper way to update Swing UIs from threads other than the event thread).

The code is:

import javax.swing.*;
import java.util.Vector;
import com.progress.open4gl.*;

public class FetchCustomerList
{
   private Thread            theThread = null;
   private SampleApplication theApp    = null;
   private Vector            theData   = new Vector();

   public FetchCustomerList(SampleApplication app)
   {
       super();

       // Store reference to Swing app (JFrame) that we will
       // need to update (via SwingUtilities.invokeLater) and
       // that has a method (getAppServer) that gives us the
       // AppServer connection needed to work with the SDO

       theApp = app;

       // Private runnable object that does the actual background
       // fetching of data from the SmartDataObject (SDO)

       Runnable runFetch = new Runnable()
       {
           public void run()
           {
               try
               {
                   fetchDataFromAdapter();
               }
               catch (Exception e)
               {
                   System.out.println(e);
               }
           }
       };

       // Instantiate and start the daemon thread

       theThread = new Thread(runFetch);
       theThread.setDaemon(true);
       theThread.start();
   }

   private void fetchDataFromAdapter()
   {
       SDOParameters params   = new SDOParameters();
       SDOResultSet  rs       = null;
       int           position = 0;

       try
       {
           // Configure the SDO for stateless AppServer access

           params.setStateless(true);

           // Instantiate the SDO (in stateless mode this will also
           // retrieve all data)

           rs = theApp.getAppServer()._createSDOResultSet("sdo.w",
                                                          null,
                                                          null,
                                                          params);

           // Efficiency stuff

           // Get the integer position of the field we want so the
           // Adapter doesn't have to do it every time

           position = rs.findColumn("Name");

           // Figure out how many rows we have and size the vector
           // accordingly then reset pointer back to the beginning
           // of the resultset

           rs.last();
           theData.ensureCapacity(rs.getRow());
           rs.beforeFirst();

           // Iterate thru each record and add the field to the
           // vector

           while (rs.next())
           {
               theData.addElement(rs.getObject(position));
           }

           // Close the resultset (i.e. be a good programmer)

           rs.close();

           // Kick off new thread to properly update the list with
           // the data

           SwingUtilities.invokeLater(new Runnable()
           {
               public void run()
               {
                   theApp.getCustomerList().setListData(theData);
               }
           });
       }
       catch (Throwable t)
       {
           System.out.println(t);
       }
   }

   protected void finalize() throws Throwable
   {
       super.finalize();

       // Technically, this is not needed but when dealing
       // with Swing it is best to be conservative

       theApp  = null;
   }
}