简体   繁体   English

JTable行颜色取决于模型中的值?

[英]JTable row color depending on Value in model?

I have this code in my Table model: 我在Table模型中有这个代码:

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

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. 我想更改每一行的背景颜色,例如,如果myDocument.getIsRegistered() == true ,我希望此行有黄色背景,如果myDocument.getIsValid == false行是蓝色等等。

I've found examples that recolor rows depending on values in JTable. 我找到了根据JTable中的值重新着色行的示例。 But getIsValid and getIsRegistered() aren't actually displayed, they exist only in model. 但getIsValid和getIsRegistered()实际上并未显示,它们仅存在于模型中。 Any advice or example would really help. 任何建议或例子都会有所帮助。 thanks in advance. 提前致谢。

update. 更新。 my TableCellRenderer: 我的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;
    }
}

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. 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 is empty:( 表是空的:(

Since you want to color an entire row, its easier to use Table Row Rendering than it is to create muuliple custom renderers. 由于您希望为整行着色,因此更容易使用表行渲染,而不是创建多个自定义渲染器。

I've found examples that recolor rows depending on values in JTable. 我找到了根据JTable中的值重新着色行的示例。 But getIsValid and getIsRegistered() aren't actually displayed, they exist only in model 但getIsValid和getIsRegistered()实际上并未显示,它们仅存在于模型中

You can still access the model from the table. 您仍然可以从表中访问该模型。 You just use: 你只需使用:

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

You have to write your own cell renderer: 您必须编写自己的单元格渲染器:

http://www.exampledepot.com/egs/javax.swing.table/CustRend.html http://www.exampledepot.com/egs/javax.swing.table/CustRend.html

You need to implement a custom cell renderer. 您需要实现自定义单元格渲染器。 Here is a good start: EDIT: Code updated 这是一个好的开始:编辑:代码更新

public class MyCellRenderer extends JLabel implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
        DocumentProjectTableModel mymodel = (DocumentProjectTableMode) table.getModel(); 
        MyDocument actualValue = (MyDocument ) mydocumentModel.getDocument(rowIndex) ;
        // Set the colors as per the value in the cell...
        if(myDocument.getIsRegistered() == ... ){
            setBackground(Color.YELLOW);
        }// and so on...         
        return this;
    }
}

Set the renderer for all columns this way: 以这种方式为所有列设置渲染器:

resultTable.setDefaultRenderer(MyColumnType.class, new MyCellRenderer ());

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM