I implement icon for sorting column in my jTable. I have a button for find result and i implement a default sorting when i launch the worker. Like this :
public void refreshStripedViewTable(final List<FlightBox> boxList,
final List<FlightBoxModel> boxesModels, final String sortBy) {
if (boxList != null && !boxList.isEmpty()) {
stripedViewTableModel.setContent(boxesModels);
tableRenderer.setStripedViewModelList(boxesModels);
final RowSorter<? extends TableModel> sorter = tableFacade
.getTooltipsTable().getRowSorter();
if (sorter != null) {
List<SortKey> sortKeys = new ArrayList<SortKey>();
javax.swing.RowSorter.SortKey sk = new SortKey(getColumnIndex(
sortBy, tableFacade.getTooltipsTable()),
SortOrder.ASCENDING);
sortKeys.add(sk);
sorter.setSortKeys(sortKeys);
tableFacade.getTooltipsTable().setRowSorter(sorter);
tableFacade.getTooltipsTable().validate();
tableFacade.getTooltipsTable().getModelTable()
.fireTableDataChanged();
}
}
}
i add a renderer for setIcon when user click on header on my JTable.
public Component getTableCellRendererComponent(final JTable table,
final Object value, final boolean isSelected, final boolean hasFocus,
final int row, final int column) {
setIcon(null);
super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column);
setOpaque(false);
setForeground(Color.white);
final RowSorter<?> sorter = table.getRowSorter();
if (sorter != null) {
java.util.List<? extends javax.swing.RowSorter.SortKey> sortKeys = sorter
.getSortKeys();
if (sortKeys.size() > 0) {
javax.swing.RowSorter.SortKey sk = sortKeys.get(0);
// take in account if the user moves the column
if (column == table.convertColumnIndexToView(sk.getColumn())) {
Icon icon = null;
if (sk.getSortOrder() == SortOrder.ASCENDING) {
icon = iconService
.getRessourceIcon(MediaHmiService.TABLE_SORT_ORDER_ASCENDING);
} else if (sk.getSortOrder() == SortOrder.DESCENDING) {
icon = iconService
.getRessourceIcon(MediaHmiService.TABLE_SORT_ORDER_DESCENDING);
}
setIcon(icon);
}
}
}
setHorizontalTextPosition(SwingConstants.LEFT);
return this;
}
I have a problem when i do this : - Found result --> Icon is set on default column sorting OK - Click on header for set icon OK - Relaunch search --> Not icon is visible.KO My renderer don't have sortKeys but i set rowSorter in my methode refreshStripedViewTable().I don't understand why...
Thanks guys for your help. Regards.
note there are three types of
SortOrder
ASCENDING
DESCENDING
UNSORTED
f.i.