简体   繁体   中英

JTable row color depending on Value in model?

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

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.

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.

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 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 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. 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(...);

You have to write your own cell renderer:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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