The Case of the Selection That Didn't Follow Its Row
October 30, 2026
Every so often a bug report arrives fully-formed — not “something’s broken,” but the exact repro, the exact expectation, and the exact wrongness. This one: in jterm’s Table widget, select row 2. Insert a new row above or at row 2. The selection stays on row 2 — but that’s now a different row. It should slide down to row 3. That is a complete bug report, and it shipped in the same session as the fix.
What the highlight is hiding
Here’s the thing that makes this bug sneaky: after the insert, the highlight looks exactly the same. Same highlight band, same terminal cell. If the user isn’t reading carefully, nothing appears to have happened at all.
But the widget’s selection is an int — an index into the model’s rows. A row index in an array-backed list is an address, not an identity. Insert a row at index 2 and the model does the honest thing: rows.add(2, newRow). Every row that used to live at index ≥ 2 now lives one slot lower. The content the user selected didn’t disappear — it moved, by exactly one row. The selection didn’t move at all. Same number, different row. The highlight didn’t glitch or flicker; it just kept faithfully pointing at an address whose tenant had changed.
Why the code thought it was done
jterm’s Table subscribes to its model and receives a TableModelEvent for every change — the event even carries the affected index range. So the listener tableChanged() existed, and it even did real work: on removals and bulk refreshes it clamped the selection to the valid range, and on a full STRUCTURE_CHANGED rebuild it deliberately preserved the user’s selection instead of resetting to row 0. All of that law had tests.
What the listener never had was a branch for ROWS_ADDED. The insert event fired, arrived at the listener, and the listener did nothing with it. Selection stays at its index. Meanwhile nobody had reported the reverse case — delete a row above the selection and the highlight slides up one slot onto the row that fell into the vacated slot. Same disease, opposite sign, and it had been there all along.
Proving it the boring way
Law test first, straight from the report:
- build a table with rows 0, 1, 2 — select row 2 (
row2) model.insertRow(2, "NEW")— the new row takes index 2,row2slides to index 3- assert
getSelectedRow() == 3and the model’s value at the selection equals"row2"
Expected 3, got 2. Red. Same failure with an insert at the top of the table, and with a custom model firing one event for a two-row block — the selection had to ride down by the whole block, not one.
The fix
All of it lives in tableChanged() — the one listener the model calls. The rule is one sentence: rows shifted under the selection shift the selection with them.
ROWS_ADDEDat or above the selection: slide the selection down by the length of the inserted block.ROWS_REMOVEDabove the selection: slide it up. A removal that covers the selected row anchors the selection at the removed block’s start — the slot where the row that followed now lives — and the existing clamp catches the tail.- Everything else keeps its old law: appends below the selection change nothing,
STRUCTURE_CHANGEDstill preserves rather than resets, the clamp still catches out-of-range.
Anyone who has used Java Swing’s ListSelectionModel will recognize what this converged on — it’s the insertIndexInterval/removeIndexInterval behavior. That’s not a coincidence; it’s the same principle arrived at independently: the selection means the row, not the number.
The takeaway
An index is an address, not an identity. Any widget that keeps a “current position” into a list — selection, cursor, visible window, iterator — should get one question asked of it at design time: what happens to you when the list mutates at or above your position? If the answer is “nothing,” the position is silently re-pointable, and the bug is only a matter of who notices first.
The quieter lesson is about the code that was already there. The clamping logic predated the bug report and was half correct: on a removal that shrank the table past an end-of-table selection, the clamp landed on the right row by luck, which made the event pipeline look handled. It’s the same shape as a lot of classic bugs — a system with one real mechanism and one accidental one reads as “has a mechanism.” The red test didn’t just catch the insert case; it made every other mutation case state its behavior out loud, on the record, in a test that can’t pass by coincidence.
The full fix, with the law tests, is in jterm on GitHub — released as 0.1.7 on Maven Central (io.jterm:jterm).