Activating and Closing In-place Editors in Java Swing JTable

Applies to TestComplete 14.20, last modified on September 11, 2019

Java Swing JTable lets users change data directly within grid cells. To modify cell values, you need to perform certain actions with a cell’s in-place editor. This topic describes how you can activate the editor and apply changes you made:

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.

Activating In-place Editors

You can activate the focused cell’s in-place editor in one of the following ways:

  • By clicking the cell. To simulate cell clicks, you can use the ClickCell and DblClickCell methods of the JTable object.
  • By pressing the F2 key. To simulate keystrokes, use the Keys action.
  • By typing into the focused cell. At that, the cell’s value is replaced with the typed value.

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

JavaScript, JScript

function Main ()
{
  var p, Grid;
   
  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);

  // Simulate double-clicking the cell
   Grid.DblClickCell(3, "Last Name");

  // Simulate pressing the F2 shortcut
  // Grid.ClickCell(3, "Last Name");
  // Grid.Keys ("[F2]");
}

Python

def Main ():
   
  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)

  # Simulate double-clicking the cell
  Grid.DblClickCell(3, "Last Name")

  # Simulate pressing the F2 shortcut
  # Grid.ClickCell(3, "Last Name")
  # Grid.Keys ("[F2]")

VBScript

Sub Main
  Dim p, Grid

  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)

  ' Simulate double-clicking the cell
  Call Grid.DblClickCell(3, "Last Name")

  ' Simulate pressing the F2 shortcut
  ' Call Grid.ClickCell(3, "Last Name")
  ' Call Grid.Keys "[F2]"
End Sub

DelphiScript

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

  // Simulate double-clicking the cell
   Grid.DblClickCell(3, 'Last Name');

  // Simulate pressing the F2 shortcut
  // Grid.ClickCell(3, 'Last Name');
  // Grid.Keys ('[F2]');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;
   
  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);

  // Simulate double-clicking the cell
   Grid["DblClickCell"](3, "Last Name");

  // Simulate pressing the F2 shortcut
  // Grid["ClickCell"](3, "Last Name");
  // Grid["Keys"] ("[F2]");
}

Closing In-place Editors

After you have inputted a new cell value, you need to close the editor and save the changes you have made. You can do this simulating the Enter keystroke.

The code snippet below demonstrates how to change the grid cell value and finish editing.

Two notes:
  • To set the new value to the cell and finish editing, the routine sends keystrokes to the activated in-place editor, not to the grid object.

  • The sample code implies that the cells whose values are changed contain boxes to which text can be entered. This sample works well for most types of in-place editors, but the JTable control can also use specific in-place editors. In this case, you may need to work with them in a custom way.

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 the in-place editor
  Grid.DblClickCell(1, "First Name");

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

  // Finish editing the cell
  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 the in-place editor
  Grid.DblClickCell(1, "First Name")

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

  # Finish editing the cell
  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 the in-place editor
  Call Grid.DblClickCell(1, "First Name")

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

  ' Finish editing the cell
  Call Grid.SwingObject("Table.editor").Keys ("[Enter]")
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);

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

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

  // Finish editing the cell
  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 the in-place editor
  Grid["DblClickCell"](1, "First Name");

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

  // Finish editing the cell
  Grid["SwingObject"]("Table.editor")["Keys"]("[Enter]");
}

You may also need to cancel the editing and discard any changes made to the cell value. For this purpose, you can send the Esc keystroke to the in-place editor:

JavaScript, JScript

textbox.Keys ("[Esc]");

Python

textbox.Keys ("[Esc]")

VBScript

Call textbox.Keys ("[Esc]")

DelphiScript

textbox.Keys ('[Esc]');

C++Script, C#Script

textbox["Keys"] ("[Esc]");

See Also

Working With Java Swing JTable
Selecting Cells in Java Swing JTable
Obtaining and Setting Cell Values in Java Swing JTable

Highlight search results