JTable渲染器不会改变背景

时间:2015-11-20 14:05:05

标签: java swing colors jtable tablecellrenderer

我正在使用以下JTable的女儿班级,并且无法更改任何背景颜色单元格:

@SuppressWarnings("serial")
   public abstract class GenericConfigurationTable  extends JTable {
    private EditableCellRenderer t = new EditableCellRenderer();
    private DefaultCellRenderer d = new DefaultCellRenderer();
    protected boolean[] editable;

    public GenericConfigurationTable(AbstractTableModel m) {
        this();
        this.setModel(m);
    }

    public GenericConfigurationTable() {
        this.changeSelection(0, 0, false, false);
        this.setBackground(ViewsPreferences.P_TABLE_BACKGROUND_COLOR);
        this.setShowGrid(false);
        this.setPreferredScrollableViewportSize(this.getPreferredSize());
    }

    protected void setRenderers() {
        TableColumnModel u = this.getColumnModel();

        for(int i = 0; i < this.getModel().getColumnCount(); i++) {
            if(editable[i])
                u.getColumn(i).setCellRenderer(t);
            else
                u.getColumn(i).setCellRenderer(d);
        }
    }

    public class EditableCellRenderer extends JLabel implements TableCellRenderer {
        public EditableCellRenderer() {
            this.setFont(ViewsPreferences.SU_EDITABLE_CELL_FONT);
            this.setForeground(ViewsPreferences.SU_EDITABLE_FOREGROUND_COLOR);
            this.setBackground(ViewsPreferences.SU_EDITABLE_BACKGROUND_COLOR);
            setBorder(null);
            this.setHorizontalAlignment( JLabel.CENTER );
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if(value != null)
                setText(value.toString());
            else
                setText("");

            return this;
        }
    }

    public class DefaultCellRenderer extends JLabel implements TableCellRenderer {
        public DefaultCellRenderer() {
            this.setFont(ViewsPreferences.SU_CELL_FONT);
            this.setForeground(ViewsPreferences.SU_CELL_FOREGROUND_COLOR);
            this.setBackground(ViewsPreferences.SU_CELL_BACKGROUND_COLOR);
            setBorder(null);
            this.setHorizontalAlignment( JLabel.CENTER );
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if(value != null)
                setText(value.toString());
            else
                setText("");
            return this;
        }
    }
}

所以我认为它与我尝试在两个渲染器中渲染任何类型的事实相关联,因此我尝试使用table.setDefaultRenderer(Type.class, myRenderer)设置渲染器,但没有任何改变。

以下是使用的表格模型的一部分:

public class T extends AbstractTableModel {
        @SuppressWarnings("rawtypes")
        private final Class[] columns_class = { String.class, String.class, Integer.class, Integer.class, String.class, Double.class}; 
        private final String[] columns_names = { "Type", "Champs", "HM", "A", "V", "Fc" };
        private final String[] row_labels = {"J1", "J2", "J3", "J4", "J5", "J7", "J8", "J9", "J10", "J11", "J100", " ", " ", "JS", "JC"};

1 个答案:

答案 0 :(得分:1)

默认情况下,JLabel是透明的,因此永远不会绘制背景。

要绘制标签的背景,您需要使标签不透明。在渲染器的构造函数中,您需要添加:

this.setOpaque( true );