Lookup Fields

If you look closely at the data shown in the Priority column you can see that it displays a numeric value. This is actually a key value that is supposed to be used with the Priorities table to retrieve the human readable value like Low, Medium or High. We will change that now.

Just as with local data we can set this up in Data Abstract using a lookup field. Data Abstract for Java provides the class LookupDataColumn that does all the work for us.

The LookupDataColumn class extends DataColumn and represents a field which is not stored in the DataTable on the Relativity Server. Its value is pulled from a secondary table based on a source field from the primary table; here that source field is Priority' and the secondary table is thePriorities` table.

When you added the method to load the data from the Relativity Server in the DataModule class, in addition to retrieving the Tasks table from the schema, you also retrieved the Priorities table. This means that the DataModule class already has access to the data we need.

Creating a Lookup Field

Open DataModule.java if you haven't already and add a new method called setupTable.

We are creating a new LookupDataColumn called lookup, to the constructor we pass the:

  • name of the column, here PriorityText. This name is what will appear in the column header unless you set a caption.
  • the source column that will act as the key into the secondary table, here the Priority column of the Tasks table.
  • the name of the column that contains the key in our lookup table, here the Id column of the Priorities table
  • the name of the column from the lookup table that contains the result we want to display, here the Name column from the Priorities table.
protected void setupTable() {   
    //Create a lookup column based on Priority
    LookupDataColumn lookup = new LookupDataColumn("PriorityText",
            tasksTable.getColumns().getColumn("Priority"),
            prioritiesTable.getColumns().getColumn("Id"),
            prioritiesTable.getColumns().getColumn("Name"));
}

To make the column name a little prettier we set its caption name to Priority Lvl

protected void setupTable() { 
    // snipped code 

    lookup.setCaption("Priority Lvl");
}

Finally we add the column to the table model

private void setupTable() { 
    //snipped code

    tasksTable.getColumns().add(lookup);
}

Now change the loadData method to call setupTable after the data has been retrieved from the server.

public void loadData() {
    // snipped code

    this.dataAdapter.fill(tables);
    setupTable();
}

If you ran the app at the point you would not see the new column because of the changes we made to the JTable in the previous page.

Displaying the Lookup Field

Swap over to TodoApp.java and change the tweakTable method to use the new LookupFieldColumn we created. Simply change the name of the column passed to getColumn from Priority to Priority Lvl.

private void tweakTable() {
    //snipped code

//    TableColumn priorityLevelColumn = this.table.getColumn("Priority");
    TableColumn priorityLevelColumn = this.table.getColumn("Priority Lvl");

    // snipped code
}

Now when you run the app, instead of seeing 1 in the priority column, you see Low in the new Priority Lvl column.

App showing the priority text retrieved from the priority table

On the next page we will change the Done column to show Done and Open instead of 1 and 0.