我已经创建了一个jtable,然后将我的数据库拖放到其中,因此它将自动生成表中的所有列。
问题是加载时间太长了,大概需要20秒。
有什么优化的方法吗?
当我直接在MySQL Workbench上运行查询时,它大约需要两秒钟。
生成代码的相关部分:
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();
org.jdesktop.beansbinding
是一个糟糕的选择吗?
绑定无关。由于数据库访问延迟本质上是随机的,因此应该在单独的线程上进行,以避免阻塞Swing事件调度线程(EDT)。同时,所有GUI更新都必须在EDT上完成。SwingWorker
是理想的。这里有一个使用org.jdesktop.beansbinding
的工作线程的完整示例。
有没有更快更好的替代方案?