Application Techniques



Providing Data to JTables in External Java Clients

How to code an external Java client (outside of the SilverStream development environment) that accesses the SilverStream Server to populate a JTable with rows of data.

About this technique

Details

Category

Java Client Techniques> External Java clients

Description

You'll learn about:

Source code

You can get the source code for this technique from:

Related reading

See the chapter on writing external Java clients in the Programmer's Guide

Setting up   Top of page

For this technique, you need:

Coding the client   Top of page

Here's a class that provides a basic implementation of this technique.

  package extjtech; 
   
  import java.awt.*; 
  import java.awt.event.*; 
  import java.io.*; 
  import javax.swing.*; 
  import javax.swing.table.JTableHeader; 
   
  import com.sssw.rt.util.*; 
   
  // The ExtJTable class shows how you can access a SilverStream 
  // server from an external Java client. It includes an example 
  // of populating a JTable with data from a SilverStream server 
  // object. 
  public class ExtJTable { 
   
       // Instance variables for the ExtJTable class. 
       JTable table; 
       JTableHeader thead; 
       JScrollPane scrollpane; 
       JFrame frame; 
   
       // These instance variables are for the SilverStream 
       // objects used by the ExtJTable class. 
       AgrServerSession session; 
       AgrData appdata; 
       AgoRowCursorTableModel agmodel; 
   
    // Constructor for the ExtJTable class. It does the following: 
    // * Connects to a SilverStream server 
    // * Accesses the appropriate data 
    // * Loads the data into a JTable 
    // * Displays the JTable in a window (JFrame) 
    // 
    // It takes 3 arguments: 
    // 1 SilverStream server host 
    //     myserver 
    // 2 SilverStream db & data source object 
    //     mydb:com.myorg.data.dsoMyPassthru 
    // 3 Application title 
    //     My Application Title 
    public ExtJTable(String s3serverhost,   
                     String s3dsolocation, 
                     String apptitle) { 
   
      try  { 
        // Initialize the SilverStream runtime environment. Do 
        // this if you need to specify a login handler for the  
        // SilverStream server to call back when it requires user 
        // authentication. (The login handler is a class that you 
        // code and instantiate.) 
        AgRuntime.init(new LoginHandlerDlg()); 
   
        // Connect to the appropriate SilverStream server. This 
        // establishes a server session (represented by an instance 
        // of AgrServerSession). 
        // 
        // The following example connects via HTTP. In this case,  
        // you wouldn't use RMI-IIOP (the connectRMI method), 
        // because it doesn't support DSO access. 
        session = AgRuntime.connect(s3serverhost); 
   
   
        // Create an instance of AgrData (to represent the cache of 
        // data you want to work with). 
        appdata = new AgrData(); 
   
        // Initialize the AgrData by passing it the AgrServerSession. 
        appdata.init(session); 
   
        // Specify the data source for the AgrData. In this case,  
        // it is a pass-through DSO. 
        appdata.setDataSource(s3dsolocation); 
   
        // Invoke the DSO to get the data for the AgrData. 
        appdata.invokeQuery(null); 
   
   
        // Create a model that a JTable can use to access the  
        // data in the AgrData. You can do this with an instance 
        // of AgoRowCursorTableModel. 
        agmodel = new AgoRowCursorTableModel(appdata); 
   
        // Create a JTable instance and bind it to that model. 
        table = new JTable(agmodel); 
   
   
        // Format the JTable. 
        table.setRowHeight(10); 
        table.setIntercellSpacing(new Dimension(8, 8)); 
        thead = table.getTableHeader(); 
        thead.setFont(new Font("Sans Serif", Font.BOLD, 14)); 
        table.setFont(new Font("Sans Serif", Font.PLAIN, 11)); 
        table.setPreferredScrollableViewportSize(new Dimension(500, 300)); 
   
        // Create a scroll pane and add the JTable to it. 
        scrollpane = new JScrollPane(table); 
   
        // Create a JFrame instance for the application's window. 
        frame = new JFrame(apptitle); 
   
        // Add the scroll pane to the frame. 
        frame.getContentPane().add(scrollpane, BorderLayout.CENTER); 
           
        // Add an event handler to close the SilverStream server 
        // session when a user closes the window. 
        frame.addWindowListener(new WindowAdapter() { 
          public void windowClosing(WindowEvent e) { 
            session.close(); 
            System.exit(0); 
          } 
        }); 
   
        // Finish setting up the frame, then show it. 
        frame.pack(); 
        frame.setVisible(true); 
      } 
      catch (Exception e) { 
        System.out.println("Application error in ExtJTable"); 
        e.printStackTrace(); 
        System.exit(0); 
      } 
    } 
   
   
    // Main method (used for application startup). 
    public static void main(String[] args) { 
   
      if (args.length < 3) { 
        // Make sure all of the required command-line args 
        // have been provided to the application. If not, 
        // display an error message and terminate. 
        JFrame errframe = new JFrame(); 
        JOptionPane.showMessageDialog(errframe,  
          "Required arguments:\n\n" + 
            "1  SilverStream server host\n" + 
            "2  SilverStream db & data source object\n" + 
            "3  Application title\n\n" + 
          "Example:\n\n" + "java extjtech.ExtJTable " + 
            "myserver " +           
            "mydb:com.myorg.data.dsoMyPassthru " + 
            "\"My Application Title\"", 
          "Missing Command-Line Arguments",  
          JOptionPane.ERROR_MESSAGE); 
        System.exit(0); 
      } 
      else { 
        // Get the command-line args so the application can 
        // pass them to the ExtJTable constructor. 
        String s3serverhost = args[0]; 
        String s3dsolocation = args[1]; 
        String apptitle = args[2]; 
   
        // Create an instance of ExtJTable. This executes the  
        // constructor for the class, which then accesses the 
        // SilverStream server and displays the result. 
        ExtJTable extjtable = new ExtJTable(s3serverhost, 
                                            s3dsolocation, 
                                            apptitle); 
      } 
    } 
  } 

Running the client   Top of page

When this class runs, it retrieves rows of data from the DSO, then populates the JTable with them.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.