JTable行的颜色取决于模型中的值? [英] JTable row color depending on Value in model?

查看:147
本文介绍了JTable行的颜色取决于模型中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的表模型中有这个代码:

I have this code in my Table model:

public class DocumentProjectTableModel extends AbstractTableModel{

    private List<MyDocument> myDocuments;
    public String getValueAt(int row, int column) {
            String toReturn = null;
            MyDocument myDocument = myDocuments.get(row);
            SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

            switch (column) {
                case 0:
                    if(myDocument.getProject().getRegDate()!=null) toReturn = format.format(myDocument.getProject().getRegDate());
                    break;
                case 1:
                    toReturn = myDocument.getProject().getRegNum();
                    break;
                case 2:
                    toReturn = myDocument.getProject().getDescription();
                    break;
                case 3:
                    toReturn = myDocument.getProject().getShortName();
                    break;
                case 4:
                    toReturn = myDocument.getProject().getSecondName()+myDocument.getProject().getFirstName()+myDocument.getProject().getMiddleName();
                    break;

            }
            return toReturn;
        }
//  some other stuff is not shown

每行的背景颜色,例如如果 myDocument.getIsRegistered()== true ,我想这行有黄色背景,如果 myDocument.getIsValid == false 行是蓝色的等等。

I want to change background color of each row, for example if myDocument.getIsRegistered() == true, I want this row to have yellow background, if myDocument.getIsValid == false row is blue and so on.

我发现了一些例子,根据JTable中的值重绘行。但是getIsValid和getIsRegistered()没有实际显示,它们只存在于模型中。任何建议或示例将真正帮助。请提前感谢。

I've found examples that recolor rows depending on values in JTable. But getIsValid and getIsRegistered() aren't actually displayed, they exist only in model. Any advice or example would really help. thanks in advance.

更新。我的TableCellRenderer:

update. my TableCellRenderer:

public class MyTableCellRenderer extends JLabel implements TableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
        String actualValue = (String) value;
        // Set the colors as per the value in the cell...
        if(actualValue.equals("lifesucks") ){
            setBackground(Color.YELLOW);
        }
        return this;
    }
}

使用renderer:

using renderer:

            int vColIndex = 0;
            TableColumn col = resultTable.getColumnModel().getColumn(vColIndex);
            col.setCellRenderer(new MyTableCellRenderer());
 resultTable.setModel(new DocumentProjectTableModel(docs));

表显示为通常没有黄色。为什么?

table is shown as usual no yellow color. why?

update2。

resultTable=new JTable(new DocumentProjectTableModel(docs)){
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
            {
                Component c = super.prepareRenderer(renderer, row, column);
                //  Color row based on a cell value
                if (!isRowSelected(row)) {
                    c.setBackground(getBackground());
                    int modelRow = convertRowIndexToModel(row);
                    String type = (String) getModel().getValueAt(modelRow, 0);

                        c.setBackground(Color.GREEN);
                }
                return c;
            }
        };

表为空:(

推荐答案

由于要对整行进行着色,因此更容易使用 Table Row Rendering 比创建多个自定义渲染器。

Since you want to color an entire row, its easier to use Table Row Rendering than it is to create muuliple custom renderers.


我发现了根据值重绘行的示例但是getIsValid和getIsRegistered()实际上并不显示,它们只存在于模型中

I've found examples that recolor rows depending on values in JTable. But getIsValid and getIsRegistered() aren't actually displayed, they exist only in model

你仍然可以访问模型您只需使用:

You can still access the model from the table. You just use:

table.getModel().getValueAt(...);

这篇关于JTable行的颜色取决于模型中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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