我为JTabe标头创建了一个自定义渲染器,但是在对表进行排序后,当我使用默认系统L&F(在我的情况下是Windows 8)时,排序图标不会出现,但在使用Java L&F时,该图标就会出现。
使用Java L&F:


使用系统L&F:


这是我的类渲染器:

private class HeaderRenderer implements TableCellRenderer{
        private final LineBorder lb = new LineBorder(new Color(0, 152, 206));
        private final Dimension dim = new Dimension(150, 25);
        private TableCellRenderer delegate;

        public HeaderRenderer(TableCellRenderer delegate) {
            this.delegate = delegate;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocused, int row, int column) {
            Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);

            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                label.setBackground(Color.WHITE);
                label.setPreferredSize(dim);
                label.setHorizontalAlignment(SwingConstants.CENTER);
                label.setBorder(lb);
            }

            return comp;
        }

    }


使用:

JTableHeader tableHeaderCompnoent = table.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));


编辑:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

public class TableRenderingAndSorting extends JFrame {

    private JPanel contentPane;
    private JTable table_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TableRenderingAndSorting frame = new TableRenderingAndSorting();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TableRenderingAndSorting() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 497, 347);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        table_1 = new JTable();
        table_1.setModel(new DefaultTableModel(
            new Object[][] {
                {"1", "Jack", "Developper"},
                {"2", "Richard", "Developper"},
                {"3", "Jassmine", "Developper"},
                {"4", "Tom", "Project leader"},
                {"5", "Anna", null},
            },
            new String[] {
                "Col1", "Col2", "Col3"
            }
        ));
        table_1.setAutoCreateRowSorter(true);
        contentPane.add(new JScrollPane(table_1), BorderLayout.CENTER);
        JTableHeader tableHeaderCompnoent = table_1.getTableHeader();
        tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
        table_1.getTableHeader().setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
    }

    private class HeaderRenderer implements TableCellRenderer{
        private final LineBorder lb = new LineBorder(new Color(0, 152, 206));
        private final Dimension dim = new Dimension(150, 25);
        private TableCellRenderer delegate;

        public HeaderRenderer(TableCellRenderer delegate) {
            this.delegate = delegate;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocused, int row, int column) {
            Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);

            if (comp instanceof JLabel) {
                JLabel label = (JLabel) comp;
                label.setBackground(Color.WHITE);
                label.setPreferredSize(dim);
                label.setHorizontalAlignment(SwingConstants.CENTER);
                label.setBorder(lb);
            }

            return comp;
        }

    }
}

最佳答案

当我使用默认系统L&F(在我的情况下是Windows 8)时,排序图标不会出现,但是当我使用Java L&F时,该图标就会出现。


默认L&F(金属)和系统L&F之间的区别在于,第一个在标题单元格的中心(垂直对齐)绘制排序图标,而后者在标题单元格的顶部绘制。

接下来发生的是为标头单元格绘制边框。从组件的边缘向内绘制边框,这意味着它在组件的区域上绘制。由于排序图标位于系统L&F的边缘,因此会被边框覆盖。

您可以通过删除边框或将标签添加到将处理边框的JPanel来解决此问题:

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocused, int row, int column) {

    Component comp = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocused, row, column);
    JPanel panel = new JPanel(new BorderLayout());
    if (comp instanceof JLabel) {
        JLabel label = (JLabel) comp;
        label.setBackground(Color.WHITE);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label);
        panel.setBorder(lb);
    }
    return panel;
}


我已按照@mKorbel的说明删除了setPreferredSize,并且应该将dim字段删除。

另外,在行中

JTableHeader tableHeaderCompnoent = table_1.getTableHeader();
tableHeaderCompnoent.setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));
table_1.getTableHeader().setDefaultRenderer(new HeaderRenderer(tableHeaderCompnoent.getDefaultRenderer()));


第2个和第3个相同,您可以删除第3个。

07-24 19:09