Loading Jtable and processor gets high in process

Asked

Viewed 81 times

2

I’m making a sisteminha and in it has a JTable and I need to loader my data, I’ve been analyzing by task manager that when this table load function is called, it greatly increases the processor load. What can it be? I’ve tried it and I’m sure it’s because of that function. I remember that I’m NOT using the DefaultTableModel but one of mine. Does anyone know why? My processor is an i5 6500.

Class that contains the method of loading the table, remembering that I am using MVC, so it returns a ArrayList that will be used in mine 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;
    }



}
  • It could be an iniquity of things, you can’t deduce. It can be a lot of data, it can be a poorly written logic, etc. There’s no way to kick like that. I have an app that loads over 5,000 lines of a bank into a jtable with no locks, so you can see how relative this is. Anyway, add a [mcve] containing a small amount of data so it is possible to simulate and see if the problem is in your code.

  • Another thing, if you are running your application through the IDE, your testing makes even less sense, because the IDE is also an application that needs processing.

  • I ran out of the IDE and it worked that way described. This code I posted I think is what’s needed because it is he who carries my Jtable.

  • Unfortunately, I can’t come to any conclusions that might help you with this code. As I said, it depends on a plethora of situations, which in most cases, can only be tested. Just reading this code, it’s almost impossible to help you solve.

  • I understand it is because it is several columns?

  • As I said, it is not possible to say if this is the cause. Ah and the task manager is not a suitable tool for measuring performance. The tip I give you is not to worry about it, especially if it is a desktop application of swing, which soon will not even receive support. In Javafx, I believe it is more optimized, if you still feel that the performance is bad, maybe it is a good change of technology too.

  • 1

    The JDBC usually gives a peak processing when connecting and disconnecting in some banks, but in my experience with swing and high amounts of data in table, it has never been significant to the point of disrupting the application. Sometimes the concern with performance turns out to be more our paranoia than a real problem :D

  • Beauty. Thank you very much. I know it is not very right to do this by here but can give me some link to see how javafx works or tutorials.

  • 1

    You say the problem is the swing and the JTable, who made his own TableModel, but in the end it only gives the DAO code?

  • Sorry. I just edited.

  • Creating a tablemodel to cast Object is a huge waste. And unless Victor notices something I’ve been through, I haven’t seen anything in the codes to justify the slowness you’re quoting.

  • What do you mean? Can you explain to me what I meant? I don’t understand, I’m new to Java.

  • And what would be the best solution then?

  • I asked a question regarding the use of tablemodels, see: http://answall.com/q/121513/28595 Its main utility is when you have custom objects (POJO) to fill the table, but you don’t want to cast to display. The tablemodel is powerful, in the simplistic example of this link, da para fazer praticamente um CRUD completo direto na tabela.

  • Ok, I understand but am I doing something wrong using it this way? If so, what is the best way to use the tablemodel in my case?

Show 10 more comments
No answers

Browser other questions tagged

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