java - JPanel 线程在主应用程序上的 jdbc 操作期间冻结
问题描述
当应用程序使用 jdbc 驱动程序进行一些数据库检查时,我有一个主 GUI 界面冻结。然后我的想法是启动一个线程,该线程将创建一个新的“加载”jpanel,以让用户了解应用程序正在执行的操作的状态。问题是这不起作用。加载 jpanel 卡住了。
您能否让我知道做我想做的事情的最佳方法或突出下面的问题?
非常感谢。这是我的课程:
导致应用程序冻结的主 GUI 上的方法:
private void snapshotOldVariables() {
LoadingThread loadingThread = new LoadingThread();
loadingThread.start();
loadingThread.updateStatus("Creating snapshot of values...");
// find selected inventory items on the main arraylist tableRows
for (int i = 0; i < selectedItems.length; i++) {
for (InventoryVO r : tableRows) {
if (r.getMake().equals(selectedItems[i][0]) && r.getModel().equals(selectedItems[i][1]) && r.getSerial().equals(selectedItems[i][2])) {
tableRowIndexes[i] = tableRows.indexOf(r);
}
}
}
for (int i = 0; i < selectedItems.length; i++) {
productIds[i] = db.getAssetId(selectedItems[i][0], selectedItems[i][1]);
oldLocationIds[i] = db.getLocationIdOfInventoryItem(productIds[i], selectedItems[i][2]);
oldStatusIds[i] = db.getStatusId(tableRows.get(tableRowIndexes[i]).getStatus());
}
loadingThread.updateStatus("Complete");
loadingThread.dispose();
}
加载线程:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.app.main;
import com.app.panels.LoadingJPanel;
import java.awt.Dimension;
import javax.swing.JFrame;
/**
*
* @author myself
*/
public class LoadingThread extends Thread {
JFrame loadingJFrame = null;
LoadingJPanel loadingJPanel = null;
@Override
public void run() {
loadingJFrame = new JFrame();
loadingJPanel = new LoadingJPanel(loadingJFrame);
Dimension d = new Dimension();
d.setSize(500, 500);
loadingJFrame.setResizable(false);
loadingJFrame.setMinimumSize(d);
loadingJFrame.setTitle("Loading...");
loadingJFrame.setLocationRelativeTo(null);
loadingJFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
loadingJFrame.add(loadingJPanel);
loadingJFrame.setVisible(true);
}
public void updateStatus(String newStatus) {
boolean tryUpdate = true;
while (tryUpdate) {
if (loadingJPanel != null) {
loadingJPanel.updateStatus(newStatus);
tryUpdate = false;
}
}
}
public void dispose() {
boolean tryDispose = true;
while (tryDispose) {
if (loadingJFrame != null) {
loadingJFrame.dispose();
tryDispose = false;
}
}
}
}
加载 JPanel:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.app.panels;
import javax.swing.JFrame;
/**
*
* @author myself
*/
public class LoadingJPanel extends javax.swing.JPanel {
public String currentStatus = "";
/**
* Creates new form LoadingJPanel
*/
public LoadingJPanel(JFrame loadingJFrame) {
initComponents();
}
public void updateStatus (String newStatus) {
currentStatus += " Done\n" + newStatus;
jTextAreaLoading.setText(currentStatus);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextAreaLoading = new javax.swing.JTextArea();
jLabel1.setText("Status");
jTextAreaLoading.setEditable(false);
jTextAreaLoading.setColumns(20);
jTextAreaLoading.setRows(5);
jScrollPane1.setViewportView(jTextAreaLoading);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextAreaLoading;
// End of variables declaration
}
解决方案
推荐阅读
- java - 如何在 XPages 系统事件中从文件系统中删除文件
- php - 有没有办法在 Lighthouse 中获取 Laravel 的经过身份验证的用户 ID?
- exception - 尽管有 catch 块的异常
- python - Django如何只为登录用户获取所有产品?
- datetime - 如何在 Dart 和 Flutter 中将时间舍入到最近的一刻钟?
- javascript - 本地组件数据在不应该更改 Vuex 状态时更改
- javascript - 从对象数组中删除重复项 - Javascript
- android - android中(未知url)0未知错误android的http失败响应?
- javascript - 在节点 js 中安装 udev 模块时出错
- ios - 如何知道在我们的快捷方式应用程序中添加了特定快捷方式还是不使用我们的 iOS 应用程序?