效果图:
进度条:
package com.teamcenter.rac.form.mytool;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class ProgressCustomDialog implements ActionListener
{
private static final String DEFAULT_STATUS = "Please Waiting";
private static JDialog dialog;
private JProgressBar progressBar;
public static JLabel lbStatus;
private JButton btnCancel;
private static Thread thread; //处理业务的线程
private String statusInfo;
public static String resultInfo;
public static String cancelInfo;
private Dialog parentDlg ;
String message = "";
public static void show(Thread thread,Dialog parentDlg)
{
new ProgressCustomDialog(thread, DEFAULT_STATUS, null, null,parentDlg);
}
public static void show(Thread thread, String statusInfo,Dialog parentDlg)
{
new ProgressCustomDialog(thread, statusInfo, null, null,parentDlg);
}
public static void show(Thread thread, String statusInfo,
String resultInfo, String cancelInfo,Dialog parentDlg)
{
new ProgressCustomDialog(thread, statusInfo, resultInfo, cancelInfo,parentDlg);
}
private ProgressCustomDialog(Thread thread, String statusInfo,
String resultInfo, String cancelInfo,Dialog parentDlg)
{
this.thread = thread;
this.statusInfo = statusInfo;//Status
this.resultInfo = resultInfo;//处理完毕
this.cancelInfo = cancelInfo;//Cancel
this.parentDlg = parentDlg ;
initUI();
startThread();
dialog.setVisible(true);
}
private void initUI()
{
if (parentDlg == null)
{
dialog = new JDialog();
}
else
{
dialog = new JDialog(parentDlg);
}
final JPanel mainPane = new JPanel(null);
progressBar = new JProgressBar();
lbStatus = new JLabel("" + statusInfo);
btnCancel = new JButton("Cancel");
progressBar.setIndeterminate(true);
btnCancel.addActionListener(this);
mainPane.add(progressBar);
mainPane.add(lbStatus);
//mainPane.add(btnCancel);
dialog.getContentPane().add(mainPane);
//dialog.setUndecorated(true); // 除去title
dialog.setResizable(true);
dialog.setAlwaysOnTop(true);
dialog.setSize(590, 100);
dialog.setLocationRelativeTo(null); //设置此窗口相对于指定组件的位置
dialog.setTitle("进度信息");
dialog.requestFocusInWindow();
dialog.validate();
dialog.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosed(WindowEvent e)
{
}
@Override
public void windowClosing(WindowEvent e)
{
// MessageBox.post("提醒",
// "关闭该窗口后报表将无法生成。", MessageBox.INFORMATION);
dialog.dispose();
thread.stop();
}
});
mainPane.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
layout(mainPane.getWidth(), mainPane.getHeight());
}
});
}
private void startThread()
{
new Thread()
{
public void run()
{
try
{
thread.start(); // 处理耗时任务
// 等待事务处理线程结束
thread.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
// 关闭进度提示框
if (dialog == null)
{
return ;
}
dialog.dispose();
if(resultInfo != null && !resultInfo.trim().equals(""))
{
String title = "进度信息";
JOptionPane.showMessageDialog(new Dialog(dialog), resultInfo, title,
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}.start();
}
private void layout(int width, int height)
{
progressBar.setBounds(20, 20, 550, 15);
lbStatus.setBounds(20, 40, 550, 25);
btnCancel.setBounds(width - 85, height - 31, 75, 21);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
resultInfo = cancelInfo;
thread.stop();
}
public static void StopStatus()
{
if (dialog != null)
dialog.dispose();
}
//test
public static void main(String[] args) throws Exception
{
Thread thread = new Thread()
{
public void run()
{
ProgressCustomDialog.lbStatus.setText("正在更新 ....");
int index = 0;
while(index < 5)
{
try
{
sleep(1000);
System.out.println(++index);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
};
ProgressCustomDialog.show(thread, "Status", "数据处理完毕", "Cancel",null);
}
public Dialog getParentDlg() {
return parentDlg;
}
public void setParentDlg(Dialog parentDlg) {
this.parentDlg = parentDlg;
}
public static JDialog getDialog() {
return dialog;
}
public static void setDialog(JDialog dialog) {
ProgressCustomDialog.dialog = dialog;
}
}