【问题标题】:JTable renderer don't change backgroundJTable 渲染器不改变背景
【发布时间】:2016-02-23 01:23:19
【问题描述】:

我正在使用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"};

【问题讨论】:

    标签: java swing colors jtable tablecellrenderer


    【解决方案1】:

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

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

    this.setOpaque( true );
    

    【讨论】:

    • 谢谢camickr,我找得太远了^^
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多