2

I've created a jtable then dragged and dropped my database into it, so it would automatically generate all columns into the table.

The problem is, it's taking too long to load, like twenty seconds.

Any way to optimize it?

When I run the query directly on MySQL Workbench, it takes like two seconds.

Relevant part of the generated code:

    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    MyDbPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("MyDbPU").createEntityManager();
    userQuery = java.beans.Beans.isDesignTime() ? null : MyDbPUEntityManager.createQuery("SELECT u FROM User u");
    userList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : userQuery.getResultList();


    org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, userList, jTable1);
    org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
    columnBinding.setColumnName("Id");
    columnBinding.setColumnClass(Integer.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
    columnBinding.setColumnName("Nome");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sexo}"));
    columnBinding.setColumnName("Sexo");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cpf}"));
    columnBinding.setColumnName("CPF");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cidade}"));
    columnBinding.setColumnName("Cidade");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${estado}"));
    columnBinding.setColumnName("Estado");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ndoc}"));
    columnBinding.setColumnName("RG");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    columnBinding = 

jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dataNasc}"));
    columnBinding.setColumnName("Data Nascimento");
    columnBinding.setColumnClass(String.class);
    columnBinding.setEditable(false);
    bindingGroup.addBinding(jTableBinding);
    jTableBinding.bind();
  • how much time the query is taking? execute it directly at the database? – Braj Jul 24 '14 at 13:40
  • Directly on Mysql Workbench, it takes like two seconds. –  Jul 24 '14 at 13:42
  • One row of this table has like thirty four columns, I'm only displaying the ones I want in the code, but I don't think the number of columns would be the problem. Do you know if I use JDBC instead of generated code, the performance will increase drastically? –  Jul 24 '14 at 13:47
  • 1
    by using org.jdesktop.beansbinding isn't chance to change something – mKorbel Jul 24 '14 at 13:56
  • @mKorbel, I'm sorry, what do you mean? –  Jul 24 '14 at 13:58
  • 1
    Use `SwingWorker`, for [example](http://rubenlaguna.com/wp/2010/01/13/jtable-bound-to-a-database-with-lazy-loading/). – trashgod Jul 24 '14 at 14:30
  • @trashgod is beansbinding a bad choice? Is there faster, better alternatives? Thanks! –  Jul 24 '14 at 14:56

1 Answers1

3

Is org.jdesktop.beansbinding a bad choice?

The binding is irrelevant. Because database access latency is inherently stochastic, it should take place on a separate thread to avoid blocking the Swing event dispatch thread (EDT). At the same time, all GUI updates must be done on the EDT. SwingWorker is ideal for this. A complete example using a worker thread with org.jdesktop.beansbinding is seen here.

Are there faster, better alternatives?

Profiling may guide your search for optimal performance.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045