I am making a system and it has a JTable
and I need to load my data, I was analyzing through the task manager that when this function load the table is called, it increases the processor load a lot. What can it be? I already tested it and I'm sure it's because of this function. I remember that I am NOT using DefaultTableModel
but mine. Does anyone know the reason? My processor is an i5 6500.
The class that contains the method of loading the table, remembering that I'm using MVC, so it returns a ArrayList
that will be used in my TableModel
.
package lifetech.model;
import java.sql.Connection;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import lifetech.util.Conexao;
public class TarefasDataDAO {
private Conexao conexao = new Conexao();
private Connection conn = conexao.conectar();
public ArrayList carregarTabela(String date) throws ParseException{
ArrayList linhas = new ArrayList();
//Formata a data que é passada por parametro, para conseguir pesquisar no banco de dados
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
java.sql.Date data = new java.sql.Date(format.parse(date).getTime());
try{
String sql = "SELECT * FROM tb_tarefas WHERE data = ? ORDER BY id ASC";
conexao.pstm = conn.prepareStatement(sql);
conexao.pstm.setDate(1, data);
conexao.rs = conexao.pstm.executeQuery();
while(conexao.rs.next()){
linhas.add(new Object[]{conexao.rs.getInt("id"),format.format(conexao.rs.getDate("data")),
conexao.rs.getString("titulo"), conexao.rs.getString("descricao"),
conexao.rs.getString("comeco"), conexao.rs.getString("termino"),
conexao.rs.getString("observacao"), conexao.rs.getString("status")});
}
return linhas;
}catch(Exception ex){
System.out.println("Erro ao preencher tabela de Tarefas da data " + date + "\n"
+ ex.getMessage());
return null;
}finally{
conexao.desconectar();
}
}
TableModel
package lifetech.model;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel {
private ArrayList linhas;
private String[] colunas;
//Construtor
public TableModel(ArrayList linhas, String[] colunas) {
this.linhas = linhas;
this.colunas = colunas;
}
//Metódos
public String getColumnName(int numColumn){
return this.colunas[numColumn];
}
@Override
public int getRowCount() {
return linhas.size();
}
@Override
public int getColumnCount() {
return colunas.length;
}
@Override
public Object getValueAt(int numLinha, int numColuna) {
Object[] linha = (Object[]) linhas.get(numLinha);
return linha[numColuna];
}
//Getters e setters
public ArrayList getLinhas() {
return linhas;
}
public void setLinhas(ArrayList linhas) {
this.linhas = linhas;
}
public String[] getColunas() {
return colunas;
}
public void setColunas(String[] colunas) {
this.colunas = colunas;
}
}