有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JTable渲染器不更改背景

我正在使用以下子类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) 个答案

  1. # 1 楼答案

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

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

    this.setOpaque( true );