我创建了一些costum单元格渲染来更改jtable的外观,更改了某些行的颜色和字体,我尝试执行相同的操作,但是它不起作用,这是我的代码:

@Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                int firstRow = 0;
                int lastRow = jTable1.getRowCount() - 1;
                if (row == lastRow) {
                    ((JComponent) c).setBackground(new Color(218, 244, 247));
                //((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
                } else if (row == firstRow) {
                    ((JComponent) c).setBackground(new Color(218, 244, 247));
                    ((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
                     ((JComponent) c).setForeground(new Color(11, 114, 126));
                } else if (row == 5) {
                    ((JComponent) c).setBackground(new Color(218, 244, 247));
                    ((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
                } else {
                    ((JComponent) c).setBackground(jTable1.getBackground());
                }

                return c;
            }


这是我面临的问题:

(((JComponent)c).setForeground(new Color(11,114,126));

if (row == firstRow) {
((JComponent) c).setBackground(new Color(218, 244, 247));
((JComponent) c).setFont(new java.awt.Font("Tahoma", 1, 12));
((JComponent) c).setForeground(new Color(11, 114, 126));}


谢谢

最佳答案

,我更改了某些行的颜色和字体,但我尝试这样做,但是
它不起作用,这是我的代码:



完全适合prepareRenderer的工作,您的问题必须在其余代码中(如我们所见,您可以看到上一个线程),为了更好地帮助,尽快发布SSCCE,简短,可运行,可编译,大约与JFrame和与JTable
for example the same/similair code
但必须覆盖prepareRenderer,因为convertXxxToModel could be reordered and/or rows can be sorted or filtered,在这种情况下,ColumnModel中的索引不同于对JTables view的索引,然后XxxTableModel无法正常工作

09-13 01:49