eclipse SWT progressbar 进度条

SWT does not allow non-gui threads to update gui thread components.

So you have to make use of the "asyncExec(Runnable)" and "syncExec(Runnable)" calls in "Display".

The *Exec() call will allow the GUI thread to run whatever runnable object passed as a parameter and run the object in a separate gui thread.
For more detailed explanation take a look at the API. Here's a link http://download.eclipse.org/eclipse/downloads/documentation/2.0/html/plugins/org.eclipse.platform.doc.isv/reference/api/

I've included an example for clarity.

I've created two classes: SWTGuiExample, and GUIUpdater.

SWTGuiExample contains the GUI widgets and GUIUpdater updates the progress bar using SWTGuiExample's UpdateStatusBar method;
the interesting part for this discussion is in "public void UpdateProgressBar( final int selection_value )".


// ----------------------------------------------------------------------------------------
// SWTGuiExample.java
// ----------------------------------------------------------------------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
 
import java.lang.Runnable;
 
public class SWTGuiExample 
{
	private Shell       main_shell;
	private Display     main_display;
	private ProgressBar pBar_status;
	private int         pBar_selection;
	
	public static final int PROGRESS_BY = 2;
	
	public SWTGuiExample()
	{
		pBar_selection = 0;
	}
	
	public void BuildGUI()
	{
		// initialize display and shell
		main_display = new Display();
		main_shell   = new Shell( main_display );
		main_shell.setSize(500, 250);
		
		// add progress bar
		pBar_status  = new ProgressBar( main_shell, SWT.SMOOTH );
		pBar_status.setSize(400,20);
		pBar_status.setMaximum( 100 );
		pBar_status.setMinimum(  0  );
		
		// open shell
		main_shell.open();
	}
	
	public void DestroyGUI()
	{
		main_display.dispose();
	}
	
	public void UpdateProgressBar( final int selection_value )
	{
		if( pBar_status.isDisposed() ) return;
		
		main_display.asyncExec
		(
			new Runnable()
			{
				public void run() { pBar_status.setSelection( selection_value ); }
			}
		);
	}
	
	public Shell   GetShell()   { return main_shell;   }
	public Display GetDisplay() { return main_display; }
	
	public static void main( String[] args )
	{
		SWTGuiExample gui = new SWTGuiExample();
		
		gui.BuildGUI();
		
		new Thread( new GUIUpdater(gui) ).start();
		
		while( !gui.GetShell().isDisposed() )
		{
			if( !gui.GetDisplay().readAndDispatch() )
			{
				gui.GetDisplay().sleep();
			}
		}
		gui.DestroyGUI();
	}
}
 
// ----------------------------------------------------------------------------------------
// GUIUpdater.java
// ----------------------------------------------------------------------------------------
import java.lang.Thread;
 
public class GUIUpdater implements Runnable 
{
 
	private SWTGuiExample m_gui;
	
	public GUIUpdater( SWTGuiExample gui )
	{
		m_gui = gui;
	}
 
	public void run() 
	{
		int current_seleciton_value = 0;
		
		while( current_seleciton_value <= 100 )
		{
			m_gui.UpdateProgressBar( current_seleciton_value );
			current_seleciton_value += SWTGuiExample.PROGRESS_BY;
			
			try
			{
				Thread.sleep(600); //0.6 sec
			}
			catch( Exception e )
			{}
		}
	}
}
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值