Selecting Cells in Java Swing JTable

Applies to TestComplete 14.0, last modified on January 23, 2019

One of the actions that you will perform the most over a grid is selecting cells. This topic describes two approaches that can be used to select a particular cell in Java Swing JTable. Note that before selecting a cell, you need to identify the row and the column where the needed cell resides. For example, you can search for the row by the value or text in a particular column.

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.

Simulating Cell Clicks

You can select cells by simulating clicks. To simulate clicks on JTable cells, you can use the ClickCell, ClickCellR, DblClickCell and similar actions of the JTable object. All of these actions have parameters that specify the row and the column that contain the needed cell. They also have an additional parameter which specifies the key or a combination of keys (Ctrl, Alt, Shift) that are pressed when simulating a click.

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

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);

  // Obtain and select the cell
  RowIdx = 4;
  ColumnName = "Last Name";

  // Simulate a click on the cell
  grid.ClickCell (RowIdx, ColumnName);
}

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)

  # Obtain and select the cell
  RowIdx = 4
  ColumnName = "Last Name"

  # Simulate a click on the cell
  grid.ClickCell (RowIdx, ColumnName)

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)

  ' Obtain and select the cell
  RowIdx = 4
  ColumnName = "Last Name"

  ' Simulate a click on the cell
  Call grid.ClickCell (RowIdx, ColumnName)
End Sub

DelphiScript

procedure Main;
var p, grid, RowIdx, ColumnName : 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);

  // Obtain and select the cell
  RowIdx := 4;
  ColumnName := 'Last Name';

  // Simulate a click on the cell
  grid.ClickCell (RowIdx, ColumnName);
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);

  // Obtain and select the cell
  RowIdx = 4;
  ColumnName = "Last Name";

  // Simulate a click on the cell
  grid["ClickCell"] (RowIdx, ColumnName);
}

Simulating Keyboard Shortcuts

The JTable control supports various keyboard shortcuts which can be used to navigate through the grid. For example, pressing an arrow key selects the adjacent cell in the direction of the arrow, Ctrl+Home navigates to the upper cell in the selected column, Ctrl+End navigates to the bottom cell, and so on.

To simulate pressing a keyboard shortcut, use the Keys action applied to the grid control. The code snippet below demonstrates how to simulate pressing the Ctrl+Home shortcut to select the upper cell in the selected column:

JavaScript, JScript

Grid.Keys ("^[Home]");

Python

Grid.Keys ("^[Home]")

VBScript

Call Grid.Keys ("^[Home]")

DelphiScript

Grid.Keys ('^[Home]');

C++Script, C#Script

Grid["Keys"]("^[Home]");

See Also

Working With Java Swing JTable
ClickCell Action (Grid Controls)
Obtaining and Setting Cell Values in Java Swing JTable

Highlight search results