Classifying with colored cells and Date column

Asked

Viewed 109 times

3

I have two problems to finalize my table:

1) I have one TableCellRenderer customized to paint an entire row based on the value of a column. It works, but when I click to sort, the colors of the lines do not follow the classification. They get "crazy".

2) I need, in the same table, to classify a column by date, however, despite a lot of research on the internet, I did not understand the solutions shown. My spine is like Object, nay String, nor Integer.

Here’s my custom model:

model = new DefaultTableModel() {
    Class[] types = new Class[]{java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Integer.class, java.lang.String.class};

    @Override
    public Class getColumnClass(int columnIndex) {
        return types[columnIndex];
    }
};

model.setColumnIdentifiers(new Object[]{"", "LINHA", "TRÂNSITO", "PROMOTOR", "<html>ORDEM NALINHA", "<html>ESTADO ORDEM NA LINHA", "TÉCNICO NA LINHA", "<html>ORDEM DESCONEXÃO", "ORDEM TIPO", "<html>DATA ORDEM EXECUÇÃO", "<html>ÚLTIMA CONEXÃO", "ESTADO"});

And here the custom Tablecellrenderer:

table = new JTable(model) {
    private final Border outside = new MatteBorder(1, 0, 1, 0, new Color(200, 200, 200));
    private final Border inside = new EmptyBorder(0, 1, 0, 1);
    private final Border highlight = new CompoundBorder(outside, inside);

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component comp = super.prepareRenderer(renderer, row, column);
        JComponent jc = (JComponent) comp;
        Object transito = getModel().getValueAt(row, 2);
        Object linha = getModel().getValueAt(row, 1);

        /////////Start of conditions.
        if (linha.toString().length() != 12) {
            comp.setBackground(Color.RED);
        } else if (transito.equals("Volto da Rua")) {
            comp.setBackground(new Color(255, 230, 150));
        }

        ...

        else {
            comp.setBackground(Color.WHITE);
        }
        /////////End of conditions.


        // Add a border to the selected row
        if (isRowSelected(row)) {
            jc.setBorder(highlight);
            comp.setBackground(Color.LIGHT_GRAY);
            comp.setForeground(Color.BLACK);
            comp.setFont(new Font("Open Sans", 1, 12));
        }

        return comp;
    }
};

The 9th column is the date (DATE ORDER EXECUTION). When I click to sort, it is as String:

31/03/2015
31/01/2015
30/03/2015
30/01/2015
29/03/2015
29/02/2015
29/01/2015

Instead of:

31/03/3015
30/03/2015
29/03/2015

(Obs.: The date format is dd/MM/yyyy).

So, could someone help me with this? I’m new to Java, so if you have a solution, you can adapt it in code? I appreciate it in advance.

  • 2

    Screen printscreen to understand better

  • I didn’t fully understand your doubt, but perhaps this question can help you: How to color specific lines of a Jtable?

  • 1

    @Georgecosta, wouldn’t the problem be the types defined for each column? Your date column is as String type, while it should be as Date type. In other words, the sorting algorithm used will take into account comparison with String and not Date.

  • As @Cantoni said, you defined in the model in the first row that the 9th column is a String, so it will sort as String. The 10th that is Object .

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.