I have two problems with finalizing my table:
1) I have% custom% 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 are "crazy".
2) I need, in the same table, to sort one column by date, but despite a lot of research on the internet, I did not understand the solutions shown. My column is as TableCellRenderer
, not Object
, nor String
.
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's 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 PERFORMANCE). When I click to sort, it stays as Integer
:
3/31/2015
1/31/2015
03/30/2015
1/30/2015
03/29/2015
2/29/2015
1/29/2015
Instead of:
3/31/3015
3/30/2015
3/29/2015
(note: date format is String
).
So, could anyone help me with this? I'm new to Java, so if you have a solution, could you adapt it in the code? Thanks in advance.