gpt4 book ai didi

Java/Swing 应用程序随机卡住

转载 作者:行者123 更新时间:2023-12-01 08:00:24 24 4
gpt4 key购买 nike

我有一个简单的 Swing 按钮,可以刷新 jtable,但是在随机次数的单击之后,应用程序会卡住,有时会无限期地卡住,有时会在一分钟左右后响应。我从未执行过任何线程特定的方法,所以我认为这可能是问题所在,但我不知道从哪里开始或如何修改方法(如果这是卡住的原因)。

这是代码:

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();

}

编辑2:我做了建议的操作,但出现了空指针异常,这是我的代码:

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;


}

编辑 3:我将此代码添加到按钮的 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();
}

从而导致 Event Dispatch Thread被阻止直到 doInBackground()已完成:

enter image description here

您应该使用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 我怀疑你的 table 型号是 DefaultTableModel (如果不是这种情况,请忽略这个离题评论),这不是处理复杂模型类的最佳盟友。

如果是这种情况(并且考虑到您似乎会说西类牙语),请参阅 this post (in Spanish) (或 in English ,由谷歌翻译提供)关于使用域类创建可重用表模型。如果您不会说西类牙语,您仍然可以查看 this entry 末尾发布的 GenericDomainTableModel 类代码。 (或 in English ) 因为,这是非常不言自明的。

关于Java/Swing 应用程序随机卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25744771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com