I have a JTable in my application that has 100+ rows that are added dynamically one by one.
It stores data about clients that have ordered something and the more orders during the day the more rows there are. When we have a busy day with 100+ orders the JTable becomes extremely slow when scrolling.
I use a custom renderer to change the background color of a few cells and thats about it. I have tried using TableModelEvent to only alter the color of the rows that are visible.
Doing so has increased the speed a bit but still not enough to scroll smoothly. What are the options I have to try optimize my JTable that has/will have a lot of data?
EDIT: I can provide my Jtable and my CustomCellRenderer If needed.
private static class CustomRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = -5699527287605065L;
public CustomRenderer() {
setHorizontalAlignment(SwingConstants.CENTER);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
column);
vis.set(row, column);
if (vis.isVisible(table)) {
Object o = table.getValueAt(row, column);
if (column != 1 && column != 5 && column != 10 && !isSelected) {
cellComponent.setBackground(Color.WHITE);
if ((row % 2) == 0) cellComponent.setBackground(Colors.gray);
}
if (o != null) {
if (o.toString().contains("Liqui") && !isSelected) {
cellComponent.setBackground(Colors.green);
cellComponent.setForeground(Color.BLACK);
} else if (o.toString().contains("Green Alert") && !isSelected) {
cellComponent.setBackground(Colors.yellow);
cellComponent.setForeground(Color.BLACK);
} else if (o.toString().contains("Red Alert") && !isSelected) {
cellComponent.setBackground(Colors.red);
cellComponent.setForeground(Color.WHITE);
} else if (column == 10 && o.toString().contains("Finalizado")
&& !isSelected) {
cellComponent.setBackground(Colors.green);
cellComponent.setForeground(Color.BLACK);
} else if (column == 10 && o.toString().contains("Cancelado")
&& !isSelected) {
cellComponent.setBackground(Colors.red);
cellComponent.setForeground(Color.WHITE);
} else if (column == 10
&& o.toString().contains("Comprovante em analise")
&& !isSelected) {
cellComponent.setBackground(Colors.yellow);
cellComponent.setForeground(Color.BLACK);
} else if (column == 10 && o.toString().contains("Aguardando retirada")
&& !isSelected) {
cellComponent.setBackground(Colors.ltOrange);
cellComponent.setForeground(Color.BLACK);
} else if (column == 10 && o.toString().contains("Entrega agendada")
&& !isSelected) {
cellComponent.setBackground(Colors.orange);
cellComponent.setForeground(Color.BLACK);
} else if (column == 1
&& o.toString().toLowerCase().contains("test1")
&& !isSelected) {
cellComponent.setBackground(Colors.test1);
cellComponent.setForeground(Color.BLACK);
} else if (column == 1 && o.toString().contains("test2")
&& !isSelected) {
cellComponent.setBackground(Colors.test2);
cellComponent.setForeground(Color.WHITE);
} else if (column == 1 && o.toString().contains("test3")
&& !isSelected) {
cellComponent.setBackground(Colors.test3);
cellComponent.setForeground(Color.WHITE);
} else if (!isSelected && (row % 2) == 0) {
cellComponent.setBackground(Colors.gray);
cellComponent.setForeground(Color.BLACK);
} else if (!isSelected) {
cellComponent.setBackground(Color.WHITE);
cellComponent.setForeground(Color.BLACK);
}
if (Lists.op_list != null) {
for (int i = 0; i < Lists.op_list.size(); i++) {
if (column == 8 && !isSelected) {
if (table.getValueAt(row, 8) != null && table.getValueAt(row, 7) != null) {
if (Lists.op_list.get(i).getFornIDSent() == 1
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i)
.getOperationID()) {
cellComponent.setBackground(Colors.green);
} else if (Lists.op_list.get(i).getFornIDSent() == 2
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i)
.getOperationID()) {
cellComponent.setBackground(Colors.yellow);
} else if ((int) table.getValueAt(row, 7) == Lists.op_list.get(i)
.getOperationID()) {
cellComponent.setBackground(Color.WHITE);
if ((row % 2) == 0) cellComponent.setBackground(Colors.gray);
}
}
}
if (table.getValueAt(row, 10).toString().contains("Comprovante em analise") && column == 10
&& !isSelected) {
if (Lists.op_list.get(i).getSituation() == 1
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i).getOperationID()) {
cellComponent.setBackground(Colors.green);
cellComponent.setForeground(Color.BLACK);
} else if (Lists.op_list.get(i).getComprovanteAllowed() == 1
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i).getOperationID()) {
cellComponent.setBackground(Colors.green_dark);
cellComponent.setForeground(Color.BLACK);
} else if (Lists.op_list.get(i).getComprovanteAllowed() == 2
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i).getOperationID()) {
cellComponent.setBackground(Colors.yellow_dark);
cellComponent.setForeground(Color.WHITE);
}
}
if (column == 9 && !isSelected) {
if (Lists.op_list.get(i).getPreBolSent() == 1
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i).getOperationID()) {
cellComponent.setBackground(Colors.green);
} else if (Lists.op_list.get(i).getPreBolSent() == 2
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i).getOperationID()) {
cellComponent.setBackground(Colors.yellow);
} else if (Lists.op_list.get(i).getPreBolSent() == 0
&& (int) table.getValueAt(row, 7) == Lists.op_list.get(i).getOperationID()) {
cellComponent.setBackground(Color.WHITE);
if ((row % 2) == 0) cellComponent.setBackground(Colors.gray);
}
}
}
}
}
} else {
cellComponent.setBackground(Color.WHITE);
cellComponent.setForeground(Color.BLACK);
}
vis.reset();
return cellComponent;
}
}