Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTable-为什么单元格颜色不';行不通_Java_Jtable - Fatal编程技术网

Java JTable-为什么单元格颜色不';行不通

Java JTable-为什么单元格颜色不';行不通,java,jtable,Java,Jtable,我有一个4列的JTable。我只希望根据每个单元格的值呈现索引2的列(即“Change”列)。 看看我的代码 private DefaultTableModel tmodel; private final int table_colID_at_compID = 0; private final int table_colID_at_compName = 1; private final int table_colID_at_stockPresentageGrowth = 2; private f

我有一个4列的JTable。我只希望根据每个单元格的值呈现索引2的列(即“Change”列)。 看看我的代码

private DefaultTableModel tmodel;
private final int table_colID_at_compID = 0;
private final int table_colID_at_compName = 1;
private final int table_colID_at_stockPresentageGrowth = 2;
private final int table_colID_at_compNetWorth = 3;

..........more code(unimportant code)..........

    tmodel = new DefaultTableModel( new String[][] {} ,new String[]{"Comp. ID","Com. Name","Change %","Net Worth"} );

    table = new JTable(tmodel){
        public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){
            Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);

            if(columnIndex == table_colID_at_stockPresentageGrowth) {
                double value = new Double(getValueAt(rowIndex, columnIndex).toString());
                if( value < 0 )
                    componenet.setBackground(Color.RED);
                else if( value == 0)
                    componenet.setBackground(Color.WHITE);
                else
                    componenet.setBackground(Color.GREEN);
            }

            return componenet;
        }
    };
private DefaultTableModel tmodel;
在compID=0处的专用最终整型表\u colID\u;
私有最终整型表\u colID\u在\u compName=1;
stockPresentageGrowth的私有最终整数表colID=2;
私有最终整型表colID在compNetWorth=3;
更多代码(不重要的代码)。。。。。。。。。。
tmodel=新的DefaultTableModel(新字符串[][]{},新字符串[]{“Comp.ID”,“Com.Name”,“Change%”,“净值”});
表=新JTable(tmodel){
公共组件prepareRenderer(TableCellRenderer渲染器、int-rowIndex、int-columnIndex){
Component componenet=super.prepareRenderer(渲染器、行索引、列索引);
if(columnIndex==表\u colID\u at\u stockPresentageGrowth){
double value=新的double(getValueAt(rowIndex,columnIndex).toString());
如果(值<0)
组件。挫折背景(颜色。红色);
else if(值==0)
组件背景(颜色:白色);
其他的
组件。挫折背景(颜色。绿色);
}
返回组件;
}
};
  • 所有名为
    table\u colID\u at
    的变量表示表列ID
  • 如下图所示,该程序绘制了两列(“更改”和“净值”)(它应该只绘制“更改”列)
  • 我调试了这个程序,发现if语句还可以
  • 所以我认为问题在于操作背后的逻辑
图像:(imgur有错误)


对于其他每一列,必须将颜色设置回白色。

但我在IF语句中询问具体的列索引。我只检查“更改”列,而不检查其他列,这是错误的。该组件是一个共享对象。如果你设置了颜色,它就会保留下来。非常感谢你!我刚刚在大IF语句后面添加了ELSE,将背景设置为白色。虽然我不知道为什么它会在下一排画上颜色。