简体   繁体   English

Jtable RowSorter图标

[英]Jtable RowSorter Icon

I have an JTable which is using RowSorter(Java 1.6) and I am using the look and feel which was implemented using Java 1.4, when RowSorter was not added in Java. 我有一个使用RowSorter(Java 1.6)的JTable,我使用的是使用Java 1.4实现的外观和感觉,当时没有在Java中添加RowSorter。 Now my problem is: when I click on the table header, table gets sorted but the RosSorter icon does not appear on the Table header. 现在我的问题是:当我点击表头时,表被排序,但RosSorter图标没有出现在表头上。 I need that icon somehow and I can not upgrade the existing look and feel. 我不知何故需要那个图标,我无法升级现有的外观。 Any help ? 有帮助吗?

The basic approach is to wrap the renderer that is supplied by the LAF, let it configure the rendering component and additionally make it paint a sort icon as appropriate. 基本方法是包装由LAF提供的渲染器,让它配置渲染组件,并使其适当地绘制排序图标。 Something like: 就像是:

final TableCellRenderer r = table.getTableHeader().getDefaultRenderer();
TableCellRenderer wrapper = new TableCellRenderer() {

    @Override
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {
        Component comp = r.getTableCellRendererComponent(table, value, isSelected, 
            hasFocus, row, column);
        if (comp instanceof JLabel) {
            JLabel label = (JLabel) comp;
            label.setIcon(getSortIcon(table, column));
        }
        return comp;
    }

    /**
     * Implements the logic to choose the appropriate icon.
     */
    private Icon getSortIcon(JTable table, int column) {
        SortOrder sortOrder = getColumnSortOrder(table, column);
        if (SortOrder.UNSORTED == sortOrder) {
            return null;
        }
        return SortOrder.ASCENDING == sortOrder ? ascendingIcon : descendingIcon;
    }

    private SortOrder getColumnSortOrder(JTable table, int column) {
        if (table == null || table.getRowSorter() == null) {
            return SortOrder.UNSORTED;
        }
        List<? extends SortKey> keys = table.getRowSorter().getSortKeys();
        if (keys.size() > 0) {
            SortKey key = keys.get(0);
            if (key.getColumn() == table.convertColumnIndexToModel(column)) {
                return key.getSortOrder();
            }
        }
        return SortOrder.UNSORTED;
    }

};
table.getTableHeader().setDefaultRenderer(wrapper);

That's the easiest case, working if the rendering component is-a JLabel and doesn't use its icon property somehow else. 这是最简单的情况,如果渲染组件是-JLabel并且不以其他方式使用其图标属性,则工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM