Setting SelectedRow & Changing Scroll Position


I am working on a project that involves a long table object. I have a script that runs on a numeric text box ‘propertyChange’ event when the property that has changed is the value property. The script then takes that value and selects a row in the table based on the value that was entered.

For example:

The user enters “57” in the text box.
The propertyChange script is triggered.
The propertyChange script loops through every row in the table data until it finds the row (thisRow) where a particular column equals “57.”
The propertyChange script sets Table.selectedRow = thisRow.

In the designer, when running on a PC, the script will cause the table to scroll to a position where the selected row is visible. On the HOPE industrial touch screens our clients are actually running on, the row is selected but the table does not scroll to a position where the row is visible. Is there a key difference between running the application in “touch screen” mode vs “mouse & keyboard” mode that is causing this? Any suggestions or ideas would be appreciated. Thanks!

The table component has a ‘viewport’ property that you may be able to manipulate to force the scroll where you want it. The ‘viewportBorderBounds’ property gives you the visible container rectangle that the viewport is aligned to.

For anyone who might be having a similar problem, myself and a couple coworkers finally found a way to make this work the way we wanted it to. The hardest part was knowing that you could call

myJtable = table.getTable()

to get a JTable object to work with. We spent a lot of time trying to do this with the table.viewport object, but kept hitting dead ends when we found out we needed a JTable object. Once table.getTable has produced a JTable object for you, the following two lines of code take care of moving the view to show the selected row:

rectangle = myJTable.getCellRect(targetRow,0,0) myJTable.scrollRectToVisible(rectangle)

In our application we wanted to have the selected row at the top of the table object. Using the above code alone, the selected row will appear either in the top row or the bottom row of the current view area, depending on which direction the table scrolled from when moving to that row.

I spent a few minutes trying to do some calculating to figure out what kind of offset to factor in to make the selected row always be at the top, but it turned out that it was much easier to simply move to the bottom of the table first, THEN to the selected row. This guaranteed that your selected row was as close to the top as the scrollbars would allow:

#Move the view to the very last row of the table.
rectangle = myJTable.getCellRect(rowCount,0,0)

#Move the view to the target row (in this case, the selected row)
rectangle = myJTable.getCellRect(targetRow,0,0)