基于正值或负值渲染jtable单元格前景

时间:2014-10-23 18:08:45

标签: java swing jtable tablecellrenderer

我不确定如何处理这个问题,我想要做的是当值大于0时这些P& L单元前景为绿色,如果小于0则为红色。我知道我需要使用tablecellrenderer不知怎的,我只是不确定如何开始。

enter image description here

这是表格设置:

tbl_positions = new WebTable(posdata, posHeaders)
    {
        public Component prepareRenderer(TableCellRenderer renderer,
                int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex,
                    vColIndex);
            if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) 
            {
                c.setBackground((new Color(216, 216, 216)));
            } 
            else 
            {
                if(!isCellSelected(rowIndex, vColIndex))
                {
                    if(rowIndex != 21)
                        c.setBackground(getBackground());
                    else
                        c.setBackground((new Color(216, 249, 255)));
                }
                else
                {
                    c.setBackground((new Color(0, 128, 255)));
                }
            }

            if(vColIndex == 5 || vColIndex == 6 )
            {
                Integer intValue = (Integer) getValueAt(rowIndex, vColIndex);
                c.setForeground(getColor(intValue));
            } 
            else 
            {
                c.setForeground(getForeground());
            }

            return c;
        }

        private Color getColor(Integer intValue) 
        {
            Color color = null;
            if (intValue > 0) {
                color = Color.GREEN;
            } else if (intValue < 0) {
                color = Color.RED;
            } else {
                color = getForeground();
            }
            return color;
        }
    };

现在正在运作:

enter image description here

2 个答案:

答案 0 :(得分:5)

对于像设置前景这样简单的事情,您不需要需要自定义渲染器。您只需覆盖prepareRenderer的{​​{1}}即可。像

这样的东西
JTable

请花一些时间浏览How to Use Tables: Editors and Renderers以获取有关自定义渲染的更多信息


更新:完整演示

JTable table = new JTable(model) {
    @Override
    public Component prepareRenderer(TableCellRenderer renderer,
            int row, int col) {

        Component c = super.prepareRenderer(renderer, row, col);
        if (col == PL_COLUMN) {
            Integer intValue = (Integer) getValueAt(row, col);
            c.setForeground(getColor(intValue));
        } else {
            c.setForeground(getForeground());
        }
        return c;
    }

    private Color getColor(int intValue) {
        Color color = null;
        if (intValue > 0) {
            color = Color.GREEN;
        } else if (intValue < 0) {
            color = Color.RED;
        } else {
            color = getForeground();
        }
        return color;
    }
};

enter image description here


更新2:使用所有双精度值和最后一行的NumberFormat。

我留下了一堆评论,可以帮助您理解代码。请注意,您可能需要格式化程序。您拥有的图像显示为不同的格式。一个有货币符号,一个没有。您的一些值也是整数,有些是小数。所以你很可能需要两个格式化程序才能按照你想要的方式获得它。

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class TestTableRenderer {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                NikeSaysJustDoIt();
            }
        });
    }

    private static void NikeSaysJustDoIt() {
        Random random = new Random();
        Object[][] data = new Object[30][7];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++)  {
                data[i][j] = (random.nextInt(65536) - 32768);
            }   
        }
        String[] cols = new String[] { "col", "col", "col", "col", "col", "col", "col" };
        DefaultTableModel model = new DefaultTableModel(data, cols);
        JTable table = new JTable(model) {
            @Override
            public Component prepareRenderer(TableCellRenderer renderer,
                    int row, int col) {

                Component c = super.prepareRenderer(renderer, row, col);
                if (col == 5 || col == 6) {
                    Integer intValue = (Integer) getValueAt(row, col);
                    c.setForeground(getColor(intValue));
                } else {
                    c.setForeground(getForeground());
                }
                return c;
            }

            private Color getColor(int intValue) {
                Color color = null;
                if (intValue > 0) {
                    color = Color.GREEN;
                } else if (intValue < 0) {
                    color = Color.RED;
                } else {
                    color = getForeground();
                }
                return color;
            }
        };
        JOptionPane.showMessageDialog(null, new JScrollPane(table));
    }
}

答案 1 :(得分:0)

final class RenderRedGreen extends DefaultTableCellRenderer {

          RenderRedGreen () {
            setHorizontalAlignment(SwingConstants.RIGHT);   
          }

          @Override public Component getTableCellRendererComponent(
            JTable aTable, Object aNumberValue, boolean aIsSelected, 
            boolean aHasFocus, int aRow, int aColumn) {  

            Component renderer = super.getTableCellRendererComponent(
              aTable, aNumberValue, aIsSelected, aHasFocus, aRow, aColumn
            );
            if (aNumberValue != null && (Double)aNumberValue < 10) { //Checking if  cell´s values isnt null and the condition is true
                renderer.setForeground(Color.RED);
         }else{
             renderer.setForeground(Color.GREEN);
          }
            return renderer;
          }
        } 
    //Where you are retrieving data from database
        TableName.getColumnModel().getColumn(TableColumnName).setCellRenderer(new RenderRedGreen() );