Aside: The code at this link is thread-unsafe in that it creates
and manipulates a whole pile of Swing components while not running on
the Event Dispatch Thread (EDT). This could cause trouble ...
... but probably doesn't cause your problem. (Evidence: Changing
things to use EventQueue.invokeLater() doesn't fix it. For me; YMMV.)
> Step 2: at the end of the main section, set a row height.
> table.setRowHeight(2,35);
"At the end" -- before the table is made visible, or after?
(Might be irrelevant: I tried it both ways, with the same outcome.)
> Run test and see there is a row that is taller.
>
> Step 3: fire change.
> ((AbstractTableModel) table.getModel()).fireTableDataChanged();
I did this by adding a "Change data" JButton which fired the event
when clicked.
> I thought this was supposed to tell the table to repaint because a cell got a different value, but this is losing the row height I just set.
JTable is a complicated beast (perhaps a victim of the urge to be
all things to all people), and the documentation is less than splendid.
Sometimes you need to read not only between the lines, but between the
squiggles as well.
So, it's a search for clues. One clue appears in the Javadoc for
fireTableDataChanged (emphasis mine):
"[...] The number of rows may also have changed and the JTable
should redraw the table *from scratch*. [...]"
Putting this hint together with your observation leads to (I think) a
rational explanation: All the old table rows -- with their heights and
background colors and whatnot -- are now junk, and will be replaced with
brand-new rows. "From scratch," as the Javadoc so helpfully puts it.
... which makes a certain kind of sense, I guess. Suppose you've
changed the height of Row 42, and then after fireTableDataChanged the
table discovers that there are now only 17 rows of data. Is the table
supposed to remember "Oh, if the model ever gives me enough more rows,
I must remember to use a special height for Row 42?"
As for what to do about it, it depends on, well, on what you're
trying to do. fireTableDataChanged is a "big hammer" (not as big as
fireTableStructureChanged, but plenty big) that smashes the old table
to fragments and builds a fresh one from the ruins (if you'll pardon
the violence of the imagery). There are other events with more limited
and less drastic effects:
- fireTableRowsUpdated notifies the table that the contents of a
range of rows may have changed. Listeners typically just repaint
those rows, leaving their decorations and whatnot intact.
- fireTableCellUpdated is an even more limited event, reporting
the possible update of one single cell (which gets repainted if it
happens to be visible).
- fireTableRowsDeleted and fireTableRowsInserted do what you'd
expect. I believe they'll leave the appearance of the surviving or
relocated rows unchanged. (Trust, but verify.)
--
eso...@comcast-dot-net.invalid
Thirteen hundred forty-two days to go.