Application Techniques



Adding and Deleting Rows with Row Cursors

How to add and delete rows in a view by using row cursors.

About this technique

Details

Category

Java Client Techniques> Java-based views

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on using Java-based views in the Programmer's Guide

The example shows how to associate a vector (which contains the data) with a row cursor (which is the navigation object). Then by using the row cursor, you can find out which row the user wants to delete and proceed to delete it by removing an element of the vector. This example also shows how to add an element to a vector, where the element's data is user input obtained from text fields.

Associating a vector and a row cursor   Top of page

The following code runs when the form is loaded. It creates a dynamic view with rows of data that the user can add to or delete from. The code uses a Vector to contain the data of all rows; each row has two columns of values. It then associates the Vector with an AgoTreeDataManager, which permits navigation among and the manipulation of rows in the data set. Finally, it creates a dynamic view and initializes it with the row cursor and the format for displaying it.

  protected void formActivate() 
  { 
        // Set property names 
        String sColumnNames [] = {"column1", "column2"}; 
               
        // Set values in the vector 
        for (int i = 0; i < 10; i++) 
        { 
           String sValuesArray [] = new String [2]; 
           sValuesArray [0] = "Column" +  
              Integer.toString (i); 
           sValuesArray [1] = Integer.toString (i); 
           m_vectorData.addElement (sValuesArray); 
        } 
        m_treeDataManager = new AgoTreeDataManager( 
            m_vectorData, sColumnNames); 
   
        // Create the dynamic view and initialize it 
        AgiRowCursor rowCursor = 
            m_treeDataManager.getRootRowCursor(); 
        m_viewFormat = createView(vwVector, rowCursor, 
          m_viewFormat); 
  } 

Notes about the code

Adding a row to a vector   Top of page

The following code creates an element to add to the vector in the dynamic view. The user specifies the data by entering them in text fields.

  private void handle_btnAddRow_actionPerformed(ActionEvent evt) 
    { 
     // Add a vector element to the view from the text fields  
      String [] sValuesArray = new String [2]; 
      sValuesArray [0] = fldColumn1.getText(); 
      sValuesArray [1] = fldColumn2.getText(); 
      m_vectorData.addElement (sValuesArray); 
     
      updateView(); 
    } 
   

Notes about the code

Deleting a row from a vector   Top of page

The following code uses getSelectedRowCursor() to determine the row that the user has selected for deletion. It then compares the column values of the selected row with each of the column values in the elements of the vector. When it finds the element whose column values match, it deletes that element of the vector.

  private void handle_btnDeleteRow_actionPerformed(ActionEvent evt) 
    { 
      String sValuesArray []; 
     
      // Get the values of the currently selected row 
      AgiRowCursor rowCursor = vwVector.getSelectedRowCursor(); 
      String sCol1 = (String) rowCursor.getProperty ("column1"); 
      String sCol2 = (String) rowCursor.getProperty ("column2"); 
     
      // Search the vector for those values and remove that element 
      for (int i = 0; i < m_vectorData.size(); i++) 
      { 
        sValuesArray = (String[])m_vectorData.elementAt (i); 
        if (sCol1.equals (sValuesArray[0]) && sCol2.equals 
         (sValuesArray[1])) 
        { 
          m_vectorData.removeElementAt(i); 
          break; 
        } 
      } 
       
      updateView(); 
    } 

Notes about the code

Updating the view   Top of page

The updateView() method is called after a row is either added or deleted. This method gets the contents of the new vector and column names and refreshes the view.creates a new view dynamically and diplays the contents new dynamicallyblah

  private void updateView()  
    { 
      // Create the dynamic view and initialize it 
      String sColumnNames [] = {"column1", "column2"}; 
      m_treeDataManager = new AgoTreeDataManager( 
         m_vectorData, sColumnNames); 
      AgiRowCursor rowCursor = m_treeDataManager.getRootRowCursor(); 
      m_viewFormat = createView(vwVector, rowCursor, m_viewFormat); 
    } 





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