Java-设置特定单元格的背景色 [英] Java - setting background color of a specific cell

查看:264
本文介绍了Java-设置特定单元格的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下显示的代码:

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设置为黄色背景,但相反,它将整个表设置为黄色背景.我很困惑,那么对此的最佳解决方案是什么?

The getCellRenderer method contains the parameter row and column and is supposed to set the row 0 with a yellow background but instead it sets the whole table in yellow background. I'm confused so what is the best solution for this?

推荐答案

将鼠标指向桌子.

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;
    }
}

这篇关于Java-设置特定单元格的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆