[英]Java/Swing app Freezes randomly
我有一個簡單的Swing按鈕,它刷新了一個jtable,但是在隨機單擊之后,應用程序凍結了,有時是無限期的,有時它在一分鍾左右后就會響應。 我從未做過任何線程專用方法,所以我認為可能是s the issue but I don
所在, s the issue but I don
如果這是凍結的原因, s the issue but I don
知道從哪里開始或如何修改方法。
這是代碼:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
recorrerLista();
}
public EventList recorrerLista(){
Persistir p = Persistir.getInstancia();
Vector<Pedidos> lista2 = p.listarPedidos();
Iterator it = lista2.iterator();
if(!pedList.isEmpty()){
pedList.clear();
}
while(it.hasNext()){
Vector v = new Vector();
v = (Vector) it.next();
int index = (Integer)v.get(0);
Pedidos ped = new Pedidos();
ped = p.buscarPedidoPorId(index);
pedList.add(ped);
}
return pedList;
}
編輯:我嘗試在SwingWorker中插入持久性類(Persistir)的方法,但現在UI不顯示。
這是代碼
public Vector listarPedidos() throws InterruptedException, ExecutionException{
SwingWorker listar = new SwingWorker<Vector, Vector>(){
@Override
protected Vector doInBackground() throws Exception {
Vector<Articulos> lista = null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PedidosPU" );
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
Query query = em.createNativeQuery("SELECT * FROM pedidosview WHERE ENTREGADO='P' ORDER BY fecha_pedido");
lista = (Vector) query.getResultList();
} catch (Exception e) {
System.out.println( e.getMessage() );
em.getTransaction().rollback();
} finally {
em.close();
emf.close();
}
return lista;
}
};
return (Vector) listar.get();
}
public Vector listarPedidos(){
//Vector<Articulos> lista = null;
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("PedidosPU" );
final EntityManager em = emf.createEntityManager();
final SwingWorker worker = new SwingWorker(){
private Vector lista;
@Override
protected Object doInBackground() throws Exception {
em.getTransaction().begin();
try {
Query query = em.createNativeQuery("SELECT * FROM pedidosview WHERE ENTREGADO='P' ORDER BY fecha_pedido");
lista = (Vector) query.getResultList();
} catch (Exception e) {
System.out.println( e.getMessage() );
em.getTransaction().rollback();
} finally {
em.close();
emf.close();
}
return lista;
}
};
worker.execute();
return lista;
}
ActionPerformed()
。 但是似乎完全跳過了代碼 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
final Persistir p = Persistir.getInstancia();
SwingWorker<Void, Pedidos> worker = new SwingWorker<Void, Pedidos>() {
//private Vector lista2;
@Override
protected Void doInBackground() throws Exception {
EntityManagerFactory emf = Persistir.createEntityManagerFactory("PedidosPU" );
EntityManager em = emf.createEntityManager();
String jqpl = "SELECT * FROM pedidosview WHERE ENTREGADO='P' ORDER BY fecha_pedido";
//TypedQuery<Articulos> query = em.createQuery(jpql, Articulos.class);
Query query = em.createNativeQuery("SELECT * FROM pedidosview WHERE ENTREGADO='P' ORDER BY fecha_pedido");
//query.setParameter("param", "P");
//lista = (Vector) query.getResultList();
List<Pedidos> lista2 = query.getResultList();
publish((Pedidos[])lista2.toArray());
em.close();
emf.close();
return null;
}
@Override
protected void process(List<Pedidos> chunks) {
Iterator it = lista2.iterator();
if(!pedList.isEmpty()){
pedList.clear();
}
while(it.hasNext()){
Vector v = new Vector();
v = (Vector) it.next();
int index = (Integer)v.get(0);
Pedidos ped = new Pedidos();
ped = p.buscarPedidoPorId(index);
pedList.add(ped);
}
}
@Override
protected void done() {
jButton1.setEnabled(true);
}
};
worker.execute();
}
您的SwingWorker實現幾乎就像什么都不做一樣,因為在listarPedidos()
的末尾您調用get()方法:
public Vector listarPedidos() throws InterruptedException, ExecutionException {
...
return (Vector) listar.get();
}
因此,導致事件調度線程被阻塞,直到doInBackground()完成為止:
您應該改為使用publish()和process() 。 由於您使用的是JPA,因此我假設您可以設法獲得一個稱為Articulos
的實體,因此您可以執行以下操作:
SwingWorker<Void, Articulos> worker = new SwingWorker<Void, Articulos>() {
@Override
protected Void doInBackground() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PedidosPU" );
EntityManager em = emf.createEntityManager();
String jqpl = "SELECT a FROM Articulos a WHERE a.entregado = :param";
TypedQuery<Articulos> query = em.createQuery(jpql, Articulos.class);
query.setParameter("param", "P");
List<Articulos> resultList = query.getResultList();
publish((Articulos[])resultList.toArray());
em.close();
emf.close();
}
@Override
protected void process(List<Articulos> chunks) {
// add your items to the table model here
// this code runs in the EDT
}
@Override
protected void done() {
refreshButton.setEnabled(true);
}
};
worker.execute();
考慮到使用Vector classm,我懷疑您的表模型是DefaultTableModel (如果不是這種情況,請忽略此無關緊要的注釋),這不是處理復雜模型類的最佳盟友。
如果是這種情況(考慮到您似乎會說西班牙語的事實),請參閱有關使用域類創建可重用表模型的文章(西班牙語) (或英語 ,由Google翻譯提供) 。 如果您不會說西班牙語,您仍然可以查看此條目末尾(或用英語 )發布的GenericDomainTableModel
類代碼,因為這很容易解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.