Searching for Records in Java Swing JTable

Applies to TestComplete 15.47, last modified on January 20, 2023

To perform various actions over the JTable control, first, you need to locate the row containing the data that you are going to work with. You may also need to find a certain record to make sure that data has been loaded to the grid correctly, and so on. This topic describes how you can search for records in the JTable object.

To perform these actions, TestComplete should have access to internal objects, properties and methods of the JTable object. For this purpose, the Java Application Support and Java Control Support plugins must be installed and enabled. The latter lets you work with the JTable controls using methods and properties of the JTable object. Without this plugin, you will not be able to work with controls using their internal methods and properties.

When testing Java Swing JTable controls, use specific methods and properties of the corresponding JTable object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a JTable control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

To find the needed record within the specified JTable column, you can use the FindRow method of the JTable object. This method returns the index of the first grid row that contains the needed record. If the row containing the specified value is not found, the method returns -1.

Note that the grid control may display data with special formatting, so the actual value of a particular cell can be significantly different from the cell’s display text. You should keep this in mind when specifying the sought-for values for your search procedure.

The following example works with the SimpleTableDemo sample application that is available at http://java.sun.com/docs/books/tutorial/uiswing/components/table.html.

This example demonstrates how you can locate a grid row by its value in a particular column. The routine obtains the JTable object, locates the grid row by using the FindRow method and clicks on the found cells.

JavaScript, JScript

function Main ()
{
  var p, Grid;
   
  // Obtain the grid object
  p = Sys.Process("javaw");
  Grid = p.SwingObject("JFrame", "SimpleTableDemo", 0, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("SimpleTableDemo", "", 0).SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("JTable", "", 0);

  // Locate the row
  RowIndex = Grid.FindRow(1, "Zakhour");
  if (RowIndex != -1)
  {
    Grid.ClickCell(RowIndex, 1);
    Log.Message("The 'Zakhour' value was found in column 1.");
  }
}

Python

def Main ():
   
  # Obtain the grid object
  p = Sys.Process("javaw")
  Grid = p.SwingObject("JFrame", "SimpleTableDemo", 0, 1).SwingObject("JRootPane", "", 0).SwingObject("None.layeredPane").SwingObject("SimpleTableDemo", "", 0).SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("JTable", "", 0)

  # Locate the row
  RowIndex = Grid.FindRow(1, "Zakhour")
  if (RowIndex != -1): 
    Grid.ClickCell(RowIndex, 1)
    Log.Message("The 'Zakhour' value was found in column 1.")

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process("javaw")
  Set Grid = p.SwingObject("JFrame", "SimpleTableDemo", 0, 1).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("SimpleTableDemo", "", 0).SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("JTable", "", 0)

  ' Locate the row
  RowIndex = Grid.FindRow(1, "Zakhour")
  If RowIndex <> -1 Then
    Call Grid.ClickCell (RowIndex, 1)
    Log.Message ("The 'Zakhour' value was found in column 1.")
  End If
End Sub

DelphiScript

procedure Main;
var p, Grid, RowIndex: OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('javaw');
  Grid := p.SwingObject('JFrame', 'SimpleTableDemo', 0, 1).SwingObject('JRootPane', '', 0).SwingObject('null.layeredPane').SwingObject('SimpleTableDemo', '', 0).SwingObject('JScrollPane', '', 0).SwingObject('JViewport', '', 0).SwingObject('JTable', '', 0);

  // Locate the row
  RowIndex := Grid.FindRow(1, 'Zakhour');
  if RowIndex <> -1 then
  begin
    Grid.ClickCell (RowIndex, 1);
    Log.Message ('The "Zakhour" value was found in column 1.');
  end;
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;
   
  // Obtain the grid object
  p = Sys["Process"]("javaw");
  Grid = p["SwingObject"]("JFrame", "SimpleTableDemo", 0, 1)["SwingObject"]("JRootPane", "", 0)["SwingObject"]("null.layeredPane")["SwingObject"]("SimpleTableDemo", "", 0)["SwingObject"]("JScrollPane", "", 0)["SwingObject"]("JViewport", "", 0)["SwingObject"]("JTable", "", 0);

  // Locate the row
  RowIndex = Grid["FindRow"](1, "Zakhour");
  if (RowIndex != -1)
  {
    Grid["ClickCell"](RowIndex, 1);
    Log["Message"]("The 'Zakhour' value was found in column 1.");
  }
}

See Also

Working With Java Swing JTable
Obtaining and Setting Cell Values in Java Swing JTable
Iterating Through Rows in Java Swing JTable

Highlight search results