首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Progressmonitor在JavaSwing中不显示

Progressmonitor在JavaSwing中不显示
EN

Stack Overflow用户
提问于 2015-03-14 05:49:06
回答 1查看 485关注 0票数 0

我正在创建一个简单的用户界面,用户可以通过单击按钮来运行特定的Java类。单击后,应向用户显示任务的进度,并提供一个取消按钮,供用户在任务运行期间的任何时间点终止任务。

在本例中,我使用了一个ProgressMonitor,当用户单击UI中的JButton时将显示该UI,从而调用包含runnable线程的runEngineerBuild()来执行另一个类(称为EngineerBuild.java)的方法。但是,不会显示ProgressMonitor对话框。如何才能显示ProgressDialog?我想知道这是不是因为多个运行线程的性质,或者我错过了什么。会非常感谢你的帮助!

在SecondPanel.java中:

代码语言:javascript
复制
package mainApplication;

import java.awt.Font;

public class SecondPanel extends JPanel {
	private MainApplication ma = null;  // main JFrame

	private JPanel pnlBtn;
	private JPanel pnlProgress;

	private JButton btnRunAll;
	private JButton btnEngBuild;
	private JButton btnWholeDoc;
	private JButton btnCancelProgress;

	private JLabel lblTitleSteps;
	private JLabel lblAlt;
	private JLabel lbl_1a;
	private JLabel lbl_1b_c;
	private JLabel lblTitleStatus;

	private JProgressBar progressRunAll;
	private JProgressBar progressEngBuild;
	private JProgressBar progressWholeDoc;

	private Property property = Property.getInstance();
	// private Task task;
	private boolean cancelFlag;

	/**
	 * Create the panel for Step 1 TabbedPane.
	 */

	public SecondPanel(MainApplication mainApp) {
		// TODO Auto-generated constructor stub
		super();
		ma = mainApp;
	}

	public SecondPanel() {
		this.setBackground(new Color(224, 255, 255));
		this.setBounds(0, 0, 745, 1350);
		this.setPreferredSize(new Dimension(745, 600));
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		pnlBtn = new JPanel();
		pnlBtn.setLayout(new BoxLayout(pnlBtn, BoxLayout.PAGE_AXIS));
		pnlBtn.setAlignmentY(Component.TOP_ALIGNMENT);

		pnlProgress = new JPanel();
		pnlProgress.setLayout(new BoxLayout(pnlProgress, BoxLayout.PAGE_AXIS));
		pnlProgress.setAlignmentY(TOP_ALIGNMENT);

		pnlBtn.add(Box.createRigidArea(new Dimension(0, 15)));

		btnEngBuild = new JButton("Run EngineerBuild.java");
		btnEngBuild.setToolTipText("Build search engineer");
		btnEngBuild.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				// start activity
				activity = new SimulatedActivity(1000);
				activity.start();

				// launch progress dialog
				progressDialog = new ProgressMonitor(ma,
						"Waiting for Simulated Activity", null, 0, activity
								.getTarget());
				progressDialog.setMillisToPopup(1000);

				// start timer
				activityMonitor = new Timer(500, null);
				activityMonitor.start();

				btnEngBuild.setEnabled(false);
			}
		});

		activityMonitor = new Timer(500, new ActionListener() {
			private PrintStream textArea;

			public void actionPerformed(ActionEvent event) {
				int current = activity.getCurrent();

				// show progress
				runEngineerBuild();
				textArea.append(current + "\n");
				progressDialog.setProgress(current);

				// check if task is completed or canceled
				if (current == activity.getTarget()	|| progressDialog.isCanceled()) {
					activityMonitor.stop();
					progressDialog.close();
					activity.interrupt();
					btnEngBuild.setEnabled(true);
				}
			}
		});

		btnEngBuild.setMinimumSize(new Dimension(200, 30));
		btnEngBuild.setPreferredSize(new Dimension(200, 30));
		btnEngBuild.setMaximumSize(new Dimension(200, 30));
		pnlBtn.add(btnEngBuild);

		pnlBtn.add(Box.createRigidArea(new Dimension(0, 15)));

		// components in panel progress
		lblTitleStatus = new JLabel();
		lblTitleStatus.setText("<html><u>Task Status</u></html>");

		progressEngBuild = new JProgressBar();
		Border border2 = BorderFactory.createTitledBorder("Run EngineerBuild");
		progressEngBuild.setBorder(border2);

		// title
		pnlProgress.add(lblTitleStatus);
		pnlProgress.add(Box.createRigidArea(new Dimension(0, 15)));
		pnlProgress.add(progressEngBuild);
		pnlProgress.add(Box.createRigidArea(new Dimension(0, 15)));
		
		this.add(Box.createRigidArea(new Dimension(15, 10)));
		this.add(pnlBtn);
		this.add(Box.createRigidArea(new Dimension(50, 10)));
		this.add(pnlProgress);
	}

	public void runEngineerBuild() 
	{
		EngineerBuildRunnable ebr = new EngineerBuildRunnable();
		ebr.run();

	}
	private class EngineerBuildRunnable implements Runnable {
		EngineerBuild eb;

		public EngineerBuildRunnable() {
			eb = new EngineerBuild();
		}

		public void run() {
			eb.initial();
			eb.storeIntoFile();
		}
	}

	private Timer activityMonitor;
	private ProgressMonitor progressDialog;
	private SimulatedActivity activity;

	public static final int WIDTH = 300;
	public static final int HEIGHT = 200;
}

/**
 * A simulated activity thread.
 */
class SimulatedActivity extends Thread {
	/**
	 * Constructs the simulated activity thread object. The thread increments a
	 * counter from 0 to a given target.
	 * 
	 * @param t
	 *            the target value of the counter.
	 */
	public SimulatedActivity(int t) {
		current = 0;
		target = t;
	}

	public int getTarget() {
		return target;
	}

	public int getCurrent() {
		return current;
	}

	public void run() {
		try {
			while (current < target && !interrupted()) {
				sleep(100);
				current++;
			}
		} catch (InterruptedException e) {
		}
	}

	private int current;
	private int target;
}

如果你感兴趣,这里有原始ProgressMonitor代码的链接:

User Interface Programming - Example 1-11 ProgressMonitorTest.java

EN

回答 1

Stack Overflow用户

发布于 2015-03-14 05:56:01

很有可能,调用runEngineerBuild()将调用长时间运行的代码,这就是您在Swing事件线程上所做的事情,这将占用线程,使您的图形用户界面变得无用并冻结,直到长时间运行的代码完成其运行。解决方案与所有类似问题都是一样的--在后台线程(如SwingWorker )中调用runEngineerBuild()

一种快速的解决方法是在一个简单的线程中显式调用runEngineerBuild()

代码语言:javascript
复制
EngineerBuildRunnable ebr = new EngineerBuildRunnable();
new  Thread(ebr).start();
// ebr.run(); // !!! don't call a Runnable's run method directly !!!!

有关如何使用SwingWorker的详细信息,请查看:Lesson: Concurrency in Swing

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29042545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档