How to add and delete rows in a view by using row cursors.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
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.
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); }
getRootRowCursor()
on the new AgoTreeDataManager
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(); }
getText()
to obtain the user-entered data intended for the two columns of the new row in the view, and store them in an array.
addElement()
to add the array of two-column values to the vector, as its last element.
updateView()
. See
Updating the view.
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(); }
getProperty()
.
removeElementAt()
and provide the index of the element as its parameter.
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); }