TableCellRenderer与空单元格问题

时间:2016-11-22 22:21:17

标签: java swing jtable tablecellrenderer

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import java.math.BigDecimal;
import javax.swing.JLabel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author SWDP
 */
public class Renderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        if (value != "null") {

            if (value instanceof String) {
                String s;
                s = String.valueOf(table.getValueAt(row, 3));
                BigDecimal valor = new BigDecimal(s);
                if (column == 3) {

                    if (valor.compareTo(BigDecimal.valueOf(0.001)) >= 0 && valor.compareTo(BigDecimal.valueOf(0.1)) <= 0) {
                        setBackground(Color.green);
                    }

                    if (valor.compareTo(BigDecimal.valueOf(0.1)) > 0 && valor.compareTo(BigDecimal.valueOf(0.9)) <= 0) {
                        setBackground(Color.yellow);
                    }

                    if (valor.compareTo(BigDecimal.valueOf(0.9)) > 0 && valor.compareTo(BigDecimal.valueOf(3.0)) <= 0) {
                        setBackground(Color.pink);
                    }

                    if (valor.compareTo(BigDecimal.valueOf(3.0)) > 0) {
                        setBackground(Color.red);
                    }
                } else {
                    setBackground(Color.white);
                }

            }

        }

        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}`

我想根据值的间隔绘制一个单元格。当该单元格中存在数据时,此代码运行良好。如果单元格为空,则会收到错误消息。否则,如果存在空单元格,则会显示错误消息,并且错误地显示JTable

0 个答案:

没有答案