Java - 设置特定单元格的背景颜色

时间:2012-03-24 13:42:28

标签: java swing jtable

我有以下显示的代码:

if ("1".equals(tmp.get(h))) {
   tmp2[0][h] = 1;
   for (int j = 0; j < truthTable.getModel().getColumnCount(); j++) {
      renderer = (DefaultTableCellRenderer) truthTable.getCellRenderer(0,
            j);
      renderer.setBackground(Color.yellow);
   }
}

getCellRenderer方法包含参数row和column,并且应该将行0设置为黄色背景,而是将整个表设置为黄色背景。我很困惑,所以最好的解决办法是什么?

2 个答案:

答案 0 :(得分:2)

JTable单元格渲染器不是组件,而是更类似于橡皮图章,它们使用组件在JTable中多次绘制自己。如果将渲染器的背景设置为黄色并且如果它不是感兴趣的单元格则不设置它,那么在绘制所有单元格时它将保持黄色。

答案 1 :(得分:2)

将鼠标指向桌面。

package test;

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class SingleCellColor {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                String[][] data = {
                        {"a", "1","2","3"},
                        {"b", "3","4","5"},
                        {"c", "6","7","8"}
                };
                String[] columns = {
                    "Letter", "Num 1", "Num2 ", "Num 3" 
                };
                DefaultTableModel tableModel = new DefaultTableModel(data, columns);

                final SingleCellRenderer renderer = new SingleCellRenderer(); 
                final JTable table = new JTable(tableModel);
                table.setDefaultRenderer(new Object().getClass(), renderer);
                table.addMouseMotionListener(new MouseAdapter() {
                    @Override
                    public void mouseMoved(MouseEvent me) {
                        Point p = me.getPoint();
                        renderer.setColumnHightlight(table.columnAtPoint(p));
                        renderer.setRowHighlight(table.rowAtPoint(p));
                        table.repaint();
                    }
                });
                JScrollPane tableScroll = new JScrollPane(table);

                JOptionPane.showMessageDialog(null, tableScroll);
            }
        });
    }
}

class SingleCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;

    int rowHighlight = -1;
    int colHighlight = -1;

    @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.setBackground(Color.ORANGE);
        if (c instanceof JComponent) {
            boolean isFocused = (colHighlight == column && rowHighlight == row);
            ((JComponent)c).setOpaque(isFocused);
        }
        return c;
    }

    public void setColumnHightlight(int colHighlight) {
        this.colHighlight = colHighlight;
    }

    public void setRowHighlight(int rowHighlight) {
        this.rowHighlight = rowHighlight;
    }
}