Obtaining and Setting Cell Values in Java Swing JTable

Applies to TestComplete 15.69, last modified on November 13, 2024

This topic describes approaches that you can use to obtain and change values stored in JTable cells. Note that before getting or setting the cell value, you need to know in which row and column the needed cell resides. For example, you can search for the record containing the needed cell within the grid.

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.

Obtaining Cell Values

To get the value from a particular grid cell, you can use the wValue property of the JTable object. The property has the Row and Column parameters which specify the row and the column that contain the cell.

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

  // Post the values from the first grid row to the log
  for (Col=0; Col<Grid.wColumnCount; Col++)
    Log.Message ("Cell (0, " + Col + ") value: " + Grid.wValue(0, Col));
}

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)

  # Post the values from the first grid row to the log
  for Col in range(0, Grid.wColumnCount-1):
    Log.Message ("Cell (0, " + Col + ") value: " + Grid.wValue[0, Col])

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)

  ' Post the values from the first grid row to the log
  For Col = 0 To Grid.wColumnCount-1
    Log.Message ("Cell (0, " & Col & ") value: " & CStr(Grid.wValue(0, Col)))
  Next
End Sub

DelphiScript

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

  // Post the values from the first grid row to the log
  for Col := 0 to Grid.wColumnCount-1 do
    Log.Message ('Cell (0, ' + aqConvert.VarToStr(Col) + ') value: ' + aqConvert.VarToStr(Grid.wValue[0, Col]));
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);

  // Post the values from the first grid row to the log
  for (Col=0; Col<Grid["wColumnCount"]; Col++)
    Log["Message"]("Cell (0, " + Col + ") value: " + Grid["wValue"](0, Col));
}

Setting Cell Values

There are two general approaches to modifying JTable cell values:

  • Simulating user actions over a cell’s in-place editor, for example, “typing” a new value into the cell.
  • Assigning a new value by using the wValue property of the JTable object.

Detailed information and script samples for both approaches are provided below. These approaches work well for most types of in-place editors. However, if the JTable control in the tested application uses specific in-place editors (for example, check boxes, combo boxes and others), you may need to work with them in a custom way.

Simulating User Input

To modify grid cell values, you can input new values directly to grid cells. Note that before that, you need to locate the row and the column in which the cell resides. For example, you can search for the row (card) containing the cell with the needed value. After you have determined the cell position, you need to select the needed cell within the grid and activate the cell’s in-place editor. When the cell is in edit mode, you can “type” the needed value into it using the Keys action applied to the in-place editor.

Below is an example that illustrates how you can do this. The routine activates the cell's edit mode by using the DblClickCell method. After that, it “types” a new value into the grid’s edit box and simulates the Enter keystroke to apply the made changes to the cell value. Note that the routine sends keystrokes to the in-place editor, not to the grid object.

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

  // Select the cell and activate its in-place editor
  Grid.DblClickCell(1, "First Name");

  // Type the new value
  Grid.SwingObject("Table.editor").Keys ("^a[Del]" + "Samuel");

  // Close the in-place editor and save the changes
  Grid.SwingObject("Table.editor").Keys ("[Enter]");
}

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)

  # Select the cell and activate its in-place editor
  Grid.DblClickCell(1, "First Name")

  # Type the new value
  Grid.SwingObject("Table.editor").Keys ("^a[Del]" + "Samuel")

  # Close the in-place editor and save the changes
  Grid.SwingObject("Table.editor").Keys ("[Enter]")

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)

  ' Select the cell and activate its in-place editor
  Call Grid.DblClickCell(1, "First Name")

  ' Type the new value
  Call Grid.SwingObject("Table.editor").Keys ("^a[Del]" + "Samuel")

  ' Close the in-place editor and save the changes
  Call Grid.SwingObject("Table.editor").Keys ("[Enter]")
End Sub

DelphiScript

procedure Main;
var p, Grid, Col: 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);

  // Select the cell and activate its in-place editor
  Grid.DblClickCell(1, 'First Name');

  // Type the new value
  Grid.SwingObject('Table.editor').Keys ('^a[Del]' + 'Samuel');

  // Close the in-place editor and save the changes
  Grid.SwingObject('Table.editor').Keys ('[Enter]');
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);

  // Select the cell and activate its in-place editor
  Grid["DblClickCell"](1, "First Name");

  // Type the new value
  Grid["SwingObject"]("Table.editor")["Keys"]("^a[Del]" + "Samuel");

  // Close the in-place editor and save the changes
  Grid["SwingObject"]("Table.editor")["Keys"]("[Enter]");
}

Assigning New Value Programmatically

Another way to modify grid cell values is to use the wValue property of the JTable object. This property is read-write, so it can be used to obtain grid cell values as well as to change them:

JavaScript

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

  // Modify the cells' values
  Grid.$set("wValue", 0, "First Name", "Samuel");
  Grid.$set("wValue", 0, "Last Name", "Clemens");
}

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

  // Modify the cells' values
  Grid.wValue(0, "First Name")= "Samuel";
  Grid.wValue(0, "Last Name") = "Clemens";
}

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)

  # Modify the cells' values
  Grid.wValue(0, "First Name")= "Samuel"
  Grid.wValue(0, "Last Name") = "Clemens"

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)

  ' Modify the cells’ values
  Grid.wValue(0, "First Name")= "Samuel"
  Grid.wValue(0, "Last Name") = "Clemens"
End Sub

DelphiScript

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

  // Modify the cells' values
  Grid.wValue(0, 'First Name') := 'Samuel';
  Grid.wValue(0, 'Last Name') := 'Clemens';
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);

  // Modify the cells' values
  Grid["wValue"](0, "First Name")= "Samuel";
  Grid["wValue"](0, "Last Name") = "Clemens";
}

See Also

Working With Java Swing JTable
wValue Property (Grid Controls)
Selecting Cells in Java Swing JTable
Activating and Closing In-place Editors in Java Swing JTable
Copying and Pasting Cell Values in Java Swing JTable

Highlight search results