如果使用java满足某些条件,如何将颜色设置为某一行? [英] How to set color to a certain row if certain conditions are met using java?

查看:102
本文介绍了如果使用java满足某些条件,如何将颜色设置为某一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jtable。 (tablesummary)。其中一个专栏是EXPIRY。我想突出显示当前日期已经失效的客户的行..

I have A jtable. (tablesummary). one of it's column is EXPIRY. i want to highlight the row with the client whose expiry date already lapsed on the current date..

我已经得到了逻辑,但我不能让行变为红色或任何其他颜色。
这是我的代码:

i already got the logic but i can't make the row turn red or any other colors. here's my code:

         int count = (tableSummary.getRowCount());
         NumberFormat formatter = new DecimalFormat("###,###");          
         String no = formatter.format(count);
         txtNo.setText(no);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     
            Calendar cal  = Calendar.getInstance();           
            String expDateString = sdf.format(cal.getTime()).toString();

            for(int i=0; i<=tableSummary.getRowCount()-1; i++){
                String nnn= tableSummary.getModel().getValueAt(i, 6).toString();   
                System.out.println(nnn);
                int res = nnn.compareTo(expDateString); 
                    if(res>=0){
                        System.out.println("if ni " + (res>=0));
                    }
                    else{ 
                        System.out.println("else ni" + (res>=0));
                        rowrenderer.setBackground(Color.RED);


                    }                        
            }

任何人都可以帮助我这个?因为它是我界面的主要亮点之一。
先谢谢!! :)

can anyone help me with this? because it's one of the main highlight of my interface. Thanks in advance !! :)

推荐答案

在以下链接中,您可以找到您要执行的操作的示例: http://www.roseindia.net/java/example/java/swing/SadingRows.shtml

In the following link, you can find an example of the action you want to do: http://www.roseindia.net/java/example/java/swing/SadingRows.shtml

您必须在 JTable prepareRenderer() $ c>并在返回的组件上添加backgroundColor。

You have to override prepareRenderer() on JTable and add the backgroundColor on the Component that gets returned.

PS:供将来参考,它会如果要包含更多代码,会更容易。您的 rowrenderer的定义 =)

PS: for future reference, it would be easier if you would include more code. The definition of your rowrenderer =)

编辑

使用以下内容(更改中的逻辑)而不是正常的 JTable表=新的JTable(模型)声明prepareRenderer 方法如果你想要别的东西而不是交替的颜色):

Instead of your normal JTable table = new JTable(model) declaration, use the following (change the logic in the prepareRenderer method if you want something else than an alternating color):

JTable table = new JTable(model) {
    public Component prepareRenderer(TableCellRenderer renderer, int Index_row, int Index_col) {
        // get the current row
        Component comp = super.prepareRenderer(renderer, Index_row, Index_col);
        // even index, not selected
        if (Index_row % 2 == 0 && !isCellSelected(Index_row, Index_col)) {
            comp.setBackground(Color.lightGray);
        } else {
            comp.setBackground(Color.white);
        }
        return comp;
    }
};

这篇关于如果使用java满足某些条件,如何将颜色设置为某一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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