0

I have a Jtable setup with Groovy SwingBuilder, and I'm trying to keep an empty row in the table so users can continue to add new values. After the empty row (single column) is filled in, I immediately add a new row, edit the cell of that new row, and request focus on the table. During the first call to request focus, everything goes correctly (assume that I'm clicking on the cell first). After hitting enter to save my cell contents, I add another empty row and the request focus fails. Anyone have any suggestions on what might be going on?

SwingBuilder swingBuilder = new SwingBuilder()
JTable myTable = swingBuilder.table(autoCreateRowSorter: true, focusable: true)
table.myTable = swingBuilder.tableModel(list: [[value: "test"]) {
swingBuilder.closureColumn(header: "Values", 
  read: { row -> return row[value] }, 
  write: { row, newValue ->
    row[value] = newValue
    addNewRow(myTable)
  })
}

void addNewRow(JTable myTable) {
  List<Map<String, String>> myTableRows = myTable.model.getRows()
  if(myTableRows.findAll{ it[value].trim().isEmpty() }.size() == 0) { // prevent more than one empty row
    myTableRows.add([value: ""])
    myTable.model.fireTableDataChanged()

    // set focus on the newly created row
    int indexOfNewRow = myTable.convertRowIndexToView(myTableRows.findIndexOf { it[value] == "" })
    myTable.changeSelection(indexOfNewRow, 0, false, false)
    myTable.editCellAt(indexOfNewRow, 0)
    myTable.requestFocusInWindow() // succeeds on first call when I click this cell, fails on second when I hit enter after filling in a row
}

I have checked values for visible, displayable, and focusable, and all return true both times in this scenario. The one thing I did notice was when I overrode the input/action for the enter key on the table. After I initially click and edit, enter will save the row, create a new one, then focus on the new row. After I edit this new row and hit enter, nothing happens. Are two different objects in focus somehow?

Skeeterdrums
  • 460
  • 1
  • 6
  • 13

2 Answers2

0

Instead of

myTable.editCellAt(indexOfNewRow, 0)
myTable.requestFocusInWindow()

Have you tried

if (myTable.editCellAt(indexOfNewRow, 0)) {
    myTable.editorComponent.requestFocusInWindow()
}
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • this should be done automatically, another issue will be moving with Caret or selectAll – mKorbel Sep 13 '14 at 19:38
  • I did check the return values of myTable.editCellAt(indexOfNewRow, 0), and it's returning true in both cases. Everything seems to be succeeding up until the final requestFocusInWindow call. – Skeeterdrums Sep 16 '14 at 20:06
  • You're trying to call it on the editorComponent yeah? – tim_yates Sep 16 '14 at 22:53
  • Ahh, my mistake on misreading that solution. I have not tried calling it on the editor component, only the table at this point. I'll have some time to play around with it tonight, so I'll give it a shot and report back. Thanks! – Skeeterdrums Sep 18 '14 at 19:20
  • No dice on this; it still seems to fail trying to gain focus on the second created row. I've even tried grabFocus (not recommended by JDK) and running requestFocusInWindow multiple times. – Skeeterdrums Sep 18 '14 at 21:31
0

The JTable needs to be declared with surrendersFocusOnKeystroke set to true. This allows for focus to be gained on any table editors for this JTable after a keyboard event (i.e. - me hitting the enter key from the previous edit).

Skeeterdrums
  • 460
  • 1
  • 6
  • 13