我做一个本地搜索的软件,点击开始按钮后后就把搜索的过程显示在text上,我想进行暂停操作,但是
这个时候界面内的东西都是不可点击的
(我用了asyncExec和BusyIndicator.showWhile(sShell.getDisplay(), paramer); 都不行),请问如何解决。
这个时候界面内的东西都是不可点击的
(我用了asyncExec和BusyIndicator.showWhile(sShell.getDisplay(), paramer); 都不行),请问如何解决。
楼主【bestseal】截止到2008-07-06 15:51:06的历史汇总数据(不包括此帖):
发帖的总数量:13 发帖的总分数:250
结贴的总数量:9 结贴的总分数:180
无满意结贴数:4 无满意结贴分:110
未结的帖子数:4 未结的总分数:70
结贴的百分比:69.23 % 结分的百分比:72.00 %
无满意结贴率:44.44 % 无满意结分率:61.11 %
楼主加油
* Created on July 7 2008
*
* Copyright by 布谷鸟
*/
package gui;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
*
* @author cuckoo
*
*/
public class MultithreadingExercise { public static void main(String args[]){
new MultithreadingExercise().createShell();
}
public void createShell(){
_shell = new Shell(_display,SWT.MAX |SWT.MIN |SWT.RESIZE);
_shell.setSize(500,400);
_shell.setText("MultiThreading");
createContents();
_shell.open();
while( !_shell.isDisposed()){
if( !_display.readAndDispatch()){
_display.sleep();
}
}
_display.dispose();
}
public void createContents(){
_displayLabel = new Label(_shell,SWT.NONE);
_displayLabel.setText("Roll display");
_displayLabel.setBounds(10, 10, 180, 15);
_displayLabel.setBackground(new Color(_display,0,0,0));
_displayLabel.setForeground(new Color(_display,140,140,142));
_stopButton = new Button(_shell,SWT.PUSH);
_stopButton.setBounds(200, 10, 40, 25);
_stopButton.setText("Stop");
_stopButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
_stopThread = true ;
}
});
ProcessThread();
}
/** create a thread */
public void ProcessThread(){
Thread thread = new Thread(){
public void run() {
// TODO Auto-generated method stub
while( !_stopThread){
_index++;
_display.asyncExec(new Runnable(){
public void run() {
// TODO Auto-generated method stub
_displayLabel.setText("布谷鸟 //-->"+_index);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread.start();
}
private Label _displayLabel = null;
private Button _stopButton = null;
private Shell _shell = null;
private Display _display = Display.getDefault();
private boolean _stopThread = false;
private int _index = 0;
}
-------------------------------------------------------------
Quietly through .....
现在我做的是通过开始按钮打开一个线程进行搜索,搜索内容实时的显示在界面上,代码如下
Thread thread = new Thread() {
public void run() {
if (!display.isDisposed()) {
Runnable runnable = new Runnable() {
public void run() {
textArea.setText("");
MainControl.goOnFlag = true;
MainControl.showSystemFile();
}
};
display.syncExec(runnable);
}
}
};
thread.start();
当我启动这个线程以后界面是不可操作的,就是其他按钮是不可点击的,我不知道是这个问题如何解决,谢谢。
当后台线程要访问前台界面的时候一定要用到Display对象
比如:
Display.getDefault().syncExec(new Runnable() {
public void run() {
//访问前台的代码
}
});希望能对你有所帮助~~
_displayLabel = new Label(_shell,SWT.NONE);
_displayLabel.setText("Roll display");
_displayLabel.setBounds(10, 10, 180, 15);
_displayLabel.setBackground(new Color(_display,0,0,0));
_displayLabel.setForeground(new Color(_display,140,140,142));
_stopButton = new Button(_shell,SWT.PUSH);
_stopButton.setBounds(200, 10, 40, 25);
_stopButton.setText("Stop");
_stopButton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
_stopThread = true ;
}
});
ProcessThread();
}
private static void showSubFile(File file){
if(file.isDirectory()){
File [] subFile = null;
if(goOnFlag){
subFile = file.listFiles();
if(subFile == null){
return;
}
for(int i = 0; i< subFile.length; i++){
if(!goOnFlag){
return;
}
Text textArea = MainFrame.textArea;
Text text = MainFrame.text;
textArea.append(subFile[i].getAbsolutePath()+ Source.lineChangeTag);
if(subFile[i].getName().equals(text.getText())){
textArea.append("**************************************************" +
Source.lineChangeTag+"找到了!!文件在:"+Source.lineChangeTag
+ subFile[i].getAbsolutePath()+ Source.lineChangeTag); goOnFlag = false;
return;
}
if(subFile[i].isDirectory()){
showSubFile(subFile[i]);//去掉或不去掉
}
}
}
else{
return;
}
}我就是这么递归的,只要把那句注释掉就可以操作按钮,不注释掉就无法控制按钮,整个这部分的操作都是由界面那边另外的线程启动的。