Java – JTable line color depends on the value in the model?

I have this code in the 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

I want to change the background color of each line, for example, if mydocument Getisregistered() = = true. I want this line to have a yellow background if mydocument Getisvalid = = false the line is blue and so on

I found an example of re coloring rows according to the values in JTable But getisvalid and getisregistered () are not actually displayed, they only exist in the model Any suggestion or example will help Thank you in advance

to 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;
    }
}

Using the renderer:

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

The form is not yellow as usual 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 is empty:(

Solution

Because you want to shade the entire row, it's easier to use table row rendering than creating multiple custom renderers

You can still access the model from the table You just need to use:

table.getModel().getValueAt(...);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>