Copying and Pasting Cell Values in Java Swing JTable

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

When testing an application containing Java Swing JTable, you may need to copy or paste grid data to and from the clipboard. For example, you can use this to specify the same values in multiple cells or organize a data exchange between the tested application and another one. With TestComplete, you can directly access the clipboard contents by using the Sys.Clipboard property. This way, you can get the text copied to the clipboard as well as set the clipboard contents to paste them to the application under test.

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.

You can copy the cell value to the clipboard or paste it from the clipboard by using the Ctrl+C and Ctrl+V keyboard shortcuts. You can simulate pressing these shortcuts by using the Keys action applied to cells’ in-place editors.

Before performing a copy or paste operation, you need to locate the needed cell in the grid, select it and activate its in-place editor. After the cell’s edit mode is activated, you need to specify the cell contents to be copied or replaced. For example, to select the entire cell value, you can simulate successive Home and Shift+End keystrokes (Home moves the caret to the beginning of the cell text, and Shift+End selects the cell text beginning from the caret position to the end). To be able to paste the clipboard contents to the cell, you need to clear the cell’s value, for example, by simulating the Del keystroke after selecting the cell’s contents. After performing the pasting operation, you need to apply the changes by simulating the Enter keystroke.

Below, is an example that illustrates how you can copy the JTable cell’s value and paste it to another cell. Note that to select cells and activate their in-place editors, you can call the DblClickCell action of the JTable object, but to copy data and paste it to cells, you need to send keystrokes to the in-place editors.

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

  // Select the cell and activate its in-place editor
  Grid.DblClickCell(1, "Last Name");
  // Copy the cell contents to the clipboard
  Grid.SwingObject("Table.editor").Keys ("[Home]![End]^c");

  // Select another cell and activate its in-place editor
  Grid.DblClickCell(2, "Last Name");
  // Paste the value from the clipboard to the cell
  Grid.SwingObject("Table.editor").Keys ("[Home]![End]^v");
  Grid.SwingObject("Table.editor").Keys ("[Enter]");
}

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)

  # Select the cell and activate its in-place editor
  Grid.DblClickCell(1, "Last Name")
  # Copy the cell contents to the clipboard
  Grid.SwingObject("Table.editor").Keys ("[Home]![End]^c")

  # Select another cell and activate its in-place editor
  Grid.DblClickCell(2, "Last Name")
  # Paste the value from the clipboard to the cell
  Grid.SwingObject("Table.editor").Keys ("[Home]![End]^v")
  Grid.SwingObject("Table.editor").Keys ("[Enter]")

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)

  ' Select the cell and activate its in-place editor
  Call Grid.DblClickCell(1, "Last Name")
  ' Copy the cell contents to the clipboard
  Call Grid.SwingObject("Table.editor").Keys ("[Home]![End]^c")

  ' Select another cell and activate its in-place editor
  Call Grid.DblClickCell(2, "Last Name")
  ' Paste the value from the clipboard to the cell
  Call Grid.SwingObject("Table.editor").Keys ("[Home]![End]^v")
  Call Grid.SwingObject("Table.editor").Keys ("[Enter]")
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);

  // Select the cell and activate its in-place editor
  Grid.DblClickCell(1, 'Last Name');
  // Copy the cell contents to the clipboard
  Grid.SwingObject('Table.editor').Keys ('[Home]![End]^c');

  // Select another cell and activate its in-place editor
  Grid.DblClickCell(2, 'Last Name');
  // Paste the value from the clipboard to the cell
  Grid.SwingObject('Table.editor').Keys ('[Home]![End]^v');
  Grid.SwingObject('Table.editor').Keys ('[Enter]');
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);

  // Select the cell and activate its in-place editor
  Grid["DblClickCell"](1, "Last Name");
  // Copy the cell contents to the clipboard
  Grid["SwingObject"]("Table.editor")["Keys"] ("[Home]![End]^c");

  // Select another cell and activate its in-place editor
  Grid["DblClickCell"](2, "Last Name");
  // Paste the value from the clipboard to the cell
  Grid["SwingObject"]("Table.editor")["Keys"]("[Home]![End]^v");
  Grid["SwingObject"]("Table.editor")["Keys"]("[Enter]");
}

See Also

Working With Java Swing JTable
Selecting Cells in Java Swing JTable
Activating and Closing In-place Editors in Java Swing JTable
Obtaining and Setting Cell Values in Java Swing JTable

Highlight search results