Sorting Data in Java Swing JTable

Applies to TestComplete 12.60, last modified on September 17, 2018

The JTable control lets end-users sort displayed data. This topic describes the approach that can be used to sort grid data from test scripts.

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.

Data displayed in the JTable control can be sorted by clicking on the header of the column that you want to sort. The next click on the same header switches between sort directions (from ascending to descending and vice versa). Note that this functionality is enabled only if the grid’s AutoCreateRowSorter property is set to True. Otherwise, if the property is False, grid headers are hidden, and there is no way to simulate clicks on them.

With TestComplete, you can simulate clicks on JTable column headers by using the ClickColumnHeader action of the Java SwingWebDataGrid object. The example below demonstrates how to use this action to sort data displayed in the JTable control. To enable the sorting functionality, the routine calls the setAutoCreateRowSorter method of the control.

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

  // Enable the sorting functionality
  Grid.setAutoCreateRowSorter(true);

  // Click the column header to sort ascending by that column
  Grid.ClickColumnHeader("First Name");

  aqUtils.Delay (1000);

  // Click the column header again to sort descending by that column
  Grid.ClickColumnHeader("First Name");
}

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)

  # Enable the sorting functionality
  Grid.setAutoCreateRowSorter(True)

  # Click the column header to sort ascending by that column
  Grid.ClickColumnHeader("First Name")

  aqUtils.Delay (1000)

  # Click the column header again to sort descending by that column
  Grid.ClickColumnHeader("First Name")

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)

  ' Enable the sorting functionality
  Call Grid.setAutoCreateRowSorter(true)

  ' Click the column header to sort ascending by that column
  Call Grid.ClickColumnHeader("First Name")

  aqUtils.Delay (1000)

  ' Click the column header again to sort descending by that column
  Call Grid.ClickColumnHeader("First Name")
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);

  // Enable the sorting functionality
  Grid.setAutoCreateRowSorter(true);

  // Click the column header to sort ascending by that column
  Grid.ClickColumnHeader('First Name');

  aqUtils.Delay (1000);

  // Click the column header again to sort descending by that column
  Grid.ClickColumnHeader('First Name');
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);

  // Enable the sorting functionality
  Grid.setAutoCreateRowSorter(true);

  // Click the column header to sort ascending by that column
  Grid.ClickColumnHeader("First Name");

  aqUtils.Delay (1000);

  // Click the column header again to sort descending by that column
  Grid.ClickColumnHeader("First Name");
}

See Also

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

Highlight search results