2

I have JTable with custom renderer which sets icon in cell.

myTable.setDefaultRenderer(MyClass.class, new DefaultTableCellRenderer() {
   public Component getTableCellRendererComponent(...) {

         JLabel label = table.getTableCellRendererComponent(...);
         label.setIcon(iconMap.get( object_type );

         return label;
   }
});

Where iconMap holds references to different icons, and object_type is a type based on which I want icon to be displayed next to label. As a result table displays cells in one of the columns with icons distinct based on type. This is a behavior which is expected.

Next I would like to filter rows based on type, I am using

TableRowSorter<> sorter = new TableRowSorter(myModel);
RowFilter<> filter = new RowFilter<>() {
    public boolean include(...) {
         if ( expected_type ) 
                return true;

          return false;
    }

}
sorter.setRowFilter(filter);
myTable.setRowSorter(sorter);

So basically it is done 'by-the-book', nothing breathtaking.

The problem is that cell's icons are displayed as there were no filter set.

Running application without filtering will display two columns, with proper match of pair icon-type <-> object-type

| A-type-icon | A-type-object |
| B-type-icon | B-type-object |
| B-type-icon | B-type-object |
| B-type-icon | B-type-object |

Running the same with filter which filters A-type-objects will

| A-type-icon | B-type-object |
| B-type-icon | B-type-object |
| B-type-icon | B-type-object |

It looks like objects are rendered first and than filtered. What can I do (or what I am doing wrong) to display icons properly.

David Warsow
  • 155
  • 1
  • 4
  • 12
  • You are missing return in the renderer class. Might be just here. – Jakub Zaverka Mar 14 '12 at 20:25
  • This is just a brief code, and return statement is present. Without it icons would not render at all. – David Warsow Mar 14 '12 at 20:30
  • OK. What you describe MIGHT be a discrepancy between model indexes and view indexes. If you sort/filter data, then the indexes in the table no longer match indexes in the model. Do you fetch an object somewhere by its position in the table? – Jakub Zaverka Mar 14 '12 at 20:33
  • Exactly, now I know what methods table.convertRowIndexToView(row) are for :) Nice, mate. – David Warsow Mar 14 '12 at 20:40
  • @David Warsow please see my answer here – mKorbel Mar 14 '12 at 22:05
  • Since someone has clearly figured out the issue here, can somebody post an answer? I suspected it was something to do with the literal row index versus the displayed row index, but that does me no good because I don't know how to control that. All I have is the renderer giving me an index, and the editor giving me an index. Both are correct, it's just drawing in the incorrect row. Where do I have to convert the row? – searchengine27 Dec 15 '15 at 01:59
  • I ended up figuring it out. I had a renderer that was determining how to paint it based off the row number that was coming in to the renderer, which was a view row. So I asked the table to give me the row from the model based on the view-row, and that resolved the issue. – searchengine27 Dec 15 '15 at 19:56

0 Answers0