jtable的着色单元格引用数字

时间:2018-07-13 18:14:14

标签: java swing nullpointerexception jtable tablecellrenderer

我正在开发一个女巫应用程序,我的桌子上有一个数字,数字10的下标是绿色,其余的则用LIGHT_GRAY着色 为此,我使用了TABLECELLRENDERER,但它一直告诉我有错误,我无法确切地找出错误所在,因此我向您寻求帮助,如果您不能 我想将其应用于所有表的另一件事是我在上面为一列编写的代码,如何在所有列上进行转换 谢谢

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class RenduCellule extends DefaultTableCellRenderer {


    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        Component c = getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (column == 2) {
       float val = Float.parseFloat(table.getValueAt(row,column)+"");
            Color color = null;
            if (val < 10)
                color = Color.green;
            else
                color = Color.LIGHT_GRAY;

            c.setBackground(color);
        } else
            c.setBackground(Color.pink);
        return c;

    }
}

这是我写的主要内容

jTable.setDefaultRenderer(Object.class, new RenduCellule());

这是错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sagem02.RenduCellule.getTableCellRendererComponent(RenduCellule.java:17)
at javax.swing.JTable.prepareRenderer(JTable.java:5723)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent.paint(JComponent.java:1042)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
at java.awt.Container.paint(Container.java:1975)
at java.awt.Window.paint(Window.java:3912)
at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:0)

private static final TableCellRenderer RENDERER = new DefaultTableCellRenderer();

不需要。

Component c = RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

应该是:

Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

那是您需要先调用渲染器的默认逻辑,然后再调用自定义渲染代码。

Object result = table.getModel().getValueAt(row, column);

应为:

Object result = table.getValueAt(row, column);

因为行/列是相对于视图(表)而不是模型的。如果您的表格是经过排序或过滤的,则表格的行/列值将不直接与模型匹配。

编辑:

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
 Component c = getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

为什么有两行代码?我的建议只有一行代码。

覆盖方法是Java的基本功能,对渲染器类而言并没有特定的含义。

通常要覆盖某个方法,您可以执行以下操作:

public Component getTableCellRendererComponent(...)
{
    super.getTableCellRendererComponent(...);

    setBackground(Color.RED);

    return this;
}

或者因为该方法返回执行渲染的组件,所以您可以这样做:

public Component getTableCellRendererComponent(...)
{
    Component c = super.getTableCellRendererComponent(...);

    c.setBackground(Color.RED);

    return c;
}

如果您不知道如何使用方法,请使用方法名称搜索论坛,以在论坛中找到其他示例。利用论坛和网络上的所有信息。