How can I pass arguments into SwingWorker?

偶尔善良 提交于 2019-12-05 07:19:11

Why not give it a File field and fill that field via a constructor that takes a File parameter?

class ClassB extends SwingWorker<Void, Integer>
{
    private File cfgFile;

    public ClassB(File cfgFile) {
       this.cfgFile = cfgFile;
    }

    protected Void doInBackground()
    {
        ClassC.runProgram(cfgFile);
    }

    protected void done()
    {
        try 
        { 
        tabs.setSelectedIndex(1);
        } 
        catch (Exception ignore) 
        {
           // *** ignoring exceptions is usually not a good idea. ***
        }
    }
}

And then run it like so:

public void actionPerformed(ActionEvent e) 
{
    new ClassB(cfgFile).execute();
}

Use constructor to pass the parameter. For example, like this:

class ClassB extends SwingWorker<Void, Integer> 
{

private File cfgFile; 

public ClassB(File cfgFile){
this.cfgFile=cfgFile;
}

protected Void doInBackground() 
{ 
    ClassC.runProgram(cfgFile); 
} 

protected void done() 
{ 
    try  
    {  
    tabs.setSelectedIndex(1); 
    }  
    catch (Exception ignore)  
    { 
    } 
} 

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!