本文介绍了setSelectionBackGround()不会为自定义渲染的单元格设置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义CellRenderer(TableCellRenderer的一个实例)来呈现CLOSE列的单元格,如下所示.我已经为表格的setSelectionBackGround设置了颜色,但是选中该列的bg颜色的单元格不会被绘制.请向我提供我将不胜感激的见解.

I'm using a custom CellRenderer (an instance of TableCellRenderer) to render the Cells of CLOSE column depicted below. I have set a color for the table's setSelectionBackGround but the cells of the said column's bg color doesn't get painted when selected. please provide me with any insight for which I shall be extremely grateful.

这是我的TablecellRenderer类

Here is my TablecellRenderer Class

class LabelRenderer extends JLabel implements TableCellRenderer {

Font f;
Color selectionBG;    
Color upDirection;

LabelRenderer(){
    super();
    f=new java.awt.Font("Trebuchet MS", 0, 12);
    selectionBG = new java.awt.Color(204, 255, 255);
    upDirection= new Color(0,102,0);
}


@Override
public Component getTableCellRendererComponent(   JTable table, Object value, boolean isSelected, boolean hasFocus,  int row, int column) {
    //structure of passing array (value)
    // new Object[]{boolean direction, String close (change%)}

    Object arr[] = (Object[])value;

    Boolean direction = (Boolean)arr[0];

    if( direction )
        this.setForeground(upDirection);//GREEN
    else
        this.setForeground(Color.red);

    this.setText(arr[1].toString());
    this.setFont(f);


    return this;
}

}

推荐答案

您的渲染器扩展了JLabel,默认情况下它不是不透明的.您可以在渲染器中执行setOpaque(true).或者,您可以扩展默认情况下不透明的DefaultTableCellRenderer.例如:

Your renderer extends JLabel which is not opaque by default. You can execute setOpaque(true)in the renderer . Or alternatively you can extend DefaultTableCellRenderer which is opaque by default. For example:

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class TableBgDemo {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Object[][] rows = { { "Column 1", "Column 2" },
                { "Column 1", "Column 2" }, { "Column 1", "Column 2" },
                { "Column 1", "Column 2" } };

        Object[] columns = { "Column 1", "Column 2" };

        DefaultTableModel model = new DefaultTableModel(rows, columns);

        JTable table = new JTable(model);
        table.getColumnModel().getColumn(0).setCellRenderer(new MyRenderer());
        table.setSelectionBackground(Color.CYAN);
        frame.add(new JScrollPane(table));
        frame.pack();
        frame.setVisible(true);
    }

    static public class MyRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
            c.setForeground(Color.RED);
            return c;
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

这篇关于setSelectionBackGround()不会为自定义渲染的单元格设置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 10:14