Reputation: 127
I've created ColumnAutofitTable extending JTable, with reference to http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/
However it shows strange behavior sometimes as attached picture.
By writing some logs I found that values in TableModel and value in CellRenderer are different when the column width is incorrectly expanded, which are same in normal cases.
Normal case, with Value of TableModel and text of CellRenderer are same.
Row == 0, Column == 1, Old Width == 0, New Width == 78, Value == 00d 01:52:07, Renderer == javax.swing.table.DefaultTableCellRenderer$UIResource[Table.cellRenderer,-74,-15,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@1dd085d,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text= 00d 01:52:07,verticalAlignment=CENTER,verticalTextPosition=CENTER]
Strange case, having wrong CellRenderer text value
Row == 465, Column == 1, Old Width == 78, New Width == 333, Value == 00d 00:20:23, Renderer == javax.swing.table.DefaultTableCellRenderer$UIResource[Table.cellRenderer,216,336,74x15,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@1dd085d,flags=25165832,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=DRIVER:S120146165 IN_QTY:0 OUT_QTY:1 TRUCKER:null ,verticalAlignment=CENTER,verticalTextPosition=CENTER]
I did not add any customized cell renderer to the table. I added data to the table using setDataVector() to prevent resizing for every row.
My main question is:
1) Why this sympton happens? Is it a bug of JTable or CellRenderer?
2) How can I avoid this kind of thing without modifying core java modules?
Upvotes: 0
Views: 219
Reputation: 347332
Renderers are a shared resource, it is likely you are seeing the last value/cell used by the renderer, which may not be the cell you are reporting
Upvotes: 2