2012-03-27 21 views
0

我有SWT应用程序。我需要在应用程序启动后运行一些任务(带进度条),并且其窗口可见。如何/在哪里做?如何在应用程序窗口创建并可见后运行一些任务?

import org.eclipse.jface.action.MenuManager; 
import org.eclipse.jface.action.StatusLineManager; 
import org.eclipse.jface.action.ToolBarManager; 
import org.eclipse.jface.window.ApplicationWindow; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Control; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class TestApp extends ApplicationWindow { 

    /** 
    * Create the application window. 
    */ 
    public TestApp() { 
     super(null); 
     createActions(); 
     addToolBar(SWT.FLAT | SWT.WRAP); 
     addMenuBar(); 
     addStatusLine(); 
    } 

    /** 
    * Create contents of the application window. 
    * @param parent 
    */ 
    @Override 
    protected Control createContents(Composite parent) { 
     Composite container = new Composite(parent, SWT.NONE); 

     return container; 
    } 

    /** 
    * Create the actions. 
    */ 
    private void createActions() { 
     // Create the actions 
    } 

    /** 
    * Create the menu manager. 
    * @return the menu manager 
    */ 
    @Override 
    protected MenuManager createMenuManager() { 
     MenuManager menuManager = new MenuManager("menu"); 
     return menuManager; 
    } 

    /** 
    * Create the toolbar manager. 
    * @return the toolbar manager 
    */ 
    @Override 
    protected ToolBarManager createToolBarManager(int style) { 
     ToolBarManager toolBarManager = new ToolBarManager(style); 
     return toolBarManager; 
    } 

    /** 
    * Create the status line manager. 
    * @return the status line manager 
    */ 
    @Override 
    protected StatusLineManager createStatusLineManager() { 
     StatusLineManager statusLineManager = new StatusLineManager(); 
     return statusLineManager; 
    } 

    /** 
    * Launch the application. 
    * @param args 
    */ 
    public static void main(String args[]) { 
     try { 
      TestApp window = new TestApp(); 
      window.setBlockOnOpen(true); 
      window.open(); 
      Display.getCurrent().dispose(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Configure the shell. 
    * @param newShell 
    */ 
    @Override 
    protected void configureShell(Shell newShell) { 
     super.configureShell(newShell); 
     newShell.setText("New Application"); 
    } 

    /** 
    * Return the initial size of the window. 
    */ 
    @Override 
    protected Point getInitialSize() { 
     return new Point(450, 300); 
    } 

} 

回答

1

您可以使用画笔侦听器。

@Override 
protected Control createContents(Composite parent) { 
    Composite container = new Composite(parent, SWT.NONE); 

    container.addPaintListener(new PaintListener() { 

     @Override 
     public void paintControl(PaintEvent e) { 
      System.out.println("I'm ready to go..."); 

     } 
    }); 
    return container; 
} 
+0

出错了,方法'paintControl'没有被调用。对不起,但'getShell()。addPaintListener()';)谢谢。 – marioosh 2012-03-27 18:13:39

+0

但是,它部分工作(再次移动窗口调用该方法),但感谢您注册监听器的想法,它解释了很多。 – marioosh 2012-03-27 18:25:00

0

感谢汤姆的注册监听者的想法。我发现了一些适合我的东西 - ShellListener。下面的例子。

/** 
* Create contents of the application window. 
* @param parent 
*/ 
@Override 
protected Control createContents(Composite parent) { 
    Composite container = new Composite(parent, SWT.NONE); 

    // build gui... 

    getShell().addShellListener(new ShellAdapter() { 
     @Override 
     public void shellActivated(ShellEvent shellevent) { 
      // some task... 
     } 
    });  

    return container; 
} 
+0

但我认为这个解决方案并不能解决问题,因为每当窗口获得焦点f.e.时,都会执行shellActivated。在改变程序窗口或从模态对话框返回之后。我会用SWT.Show代替。 – altralaser 2016-01-22 00:33:54