# Using Multiple View Object Row Iterators in Code

The Business Components run-time framework creates a default iterator for each View Object. You can also create iterators to use in addition to, or instead of the default iterator. The interface `oracle.jbo.RowIterator` provides support for scrolling, row positioning, and related functions. Key methods include `setRangeSize` and `scrollRange`.

For example, suppose you want to give clients the option of scrolling through sets of rows (perhaps simulating a Page-Down key) or one row at a time, as shown in the Figure 1.

#### Figure 1: Scrolling Through Rows with Multiple Range Size Values

The following code example creates one View Object, uses the default iterator to display one row at a time, and creates a second iterator to display a range of five rows. The examples assume that a helper routine to initialize an Application Module has been implemented elsewhere. For an example, see Loading an Application Module.

      public static void pageDown() {

        // Helper routine connects to the generic Application Module.
        ApplicationModule appMod =
           QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);

        // Specify the Java file that defines the View Object.
        // Format: <package>.<filename>
        String voDefFile = "d2e.EmpView";

        // Identify the View Object. Must be a valid Java identifier.
        String voName = "demoEmpVO";

        // Create the View Object within the context defined
        // by the Application Module.
        // The View Object provides a default iterator.
        ViewObject vo = appMod.createViewObject(voName, voDefFile);

        // Create another iterator.
        RowSetIterator secondIter = vo.createRowSetIterator("Two");
        secondIter.setRangeSize(5);

         // Array simulates user keystrokes/commands.
        int[] keys = {1, 1, 5, 5, 1};
        try {
          vo.executeQuery();
          for (int i = 0; i < keys.length; i++) {
            switch (keys[i]) {

              // Use the default iterator to print one row.
              case 1 :
                if (vo.hasNext()) {
                  Row[] rows = new Row[1];
                  rows[0] = vo.next();
                  printRows(rows);
                }
                break;

              case 5 :
                // Use the second iterator to print a range of rows.
                secondIter.scrollRange(secondIter.getRangeSize());
                printRows(secondIter.getAllRowsInRange());
                break;

              default :
                System.exit(0);
                break;
            }
          }
        }
        catch (Exception e) {

          e.printStackTrace();
        }
      }

      // This is a helper method that prints data to the screen.
      public static void printRows(Row[] rows) {
        for (int r = 0; r < rows.length; r++) {
          String rowAttrs = "";
          for (int a = 0; a < rows[r].getAttributeCount(); a++) {
            rowAttrs += (rows[r].getAttribute(a) + "\t");
          }
          System.out.println(rowAttrs);
        }
        System.out.println("-------------");
     }

The following code example uses two iterators to navigate through a table. The first (default) iterator moves through the even-numbered rows while the second iterator moves through the odd-numbered rows.

      public static void oddEven() {
        // Helper routine connects to the generic Application Module.
        ApplicationModule appMod =
            //amdemo.AppModDemo.getGenericAppMod(platform, -1);
           QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);

        // Specify the Java file that defines the View Object.
        // Format: <package>.<filename>
        String voDefFile = "d2e.DeptView";

        // Identify the View Object. Must be a valid Java identifier.
        String voName = "demoDeptVO";

        // Create the View Object within the context defined
        // by the Application Module.
        // The View Object provides a default iterator.
        ViewObject vo = appMod.createViewObject(voName, voDefFile);
        vo.setRangeSize(-1); // -1 = all rows

        // Create another iterator.
        RowSetIterator secondIter = vo.createRowSetIterator("Two");
        secondIter.setRangeSize(-1);  // -1 = all rows.

        if (!vo.hasNext()) return; // Execute the query.
         vo.executeQuery();

        // Default iterator gets even-numbered rows.
        // Second iterator gets odd-numbered rows.
        long nRows = vo.getRowCount();
        String msg = "";
        for (int i = 0; i < nRows; i +=2) {

          // Get and set row index values relative to a range.
          // Index of first row = 0.
          vo.setCurrentRowAtRangeIndex(i);
          Row currRow = vo.getCurrentRow();
          msg = "  Default iterator (even): " + vo.getRangeIndexOf(currRow);
          printRow(currRow, msg);

          secondIter.setCurrentRowAtRangeIndex(i + 1);
          currRow = secondIter.getCurrentRow();
          msg = "  Second iterator (odd): " + vo.getRangeIndexOf(currRow);
          printRow(secondIter.getCurrentRow(), msg);
        }
      }

      // This is a helper method that prints data to the screen.
      public static void printRow(Row row, String msg) {
        String rowAttrs = "";
        for (int i = 0; i < row.getAttributeCount(); i++) {
          rowAttrs += (row.getAttribute(i) + "\t");
        }
        System.out.println(msg + "\t" + rowAttrs);
      }