简体   繁体   中英

How to call a class in the done() method of a class which extends Swingworker?

I've written a class which opens cd/dvd (named Cdopener) and I have another class (named Burn) which extends Swingworker and it does something in the done() method of it, I want to make an object of class Cdopener so that it opens the cd when Burn class runs.

it is my Cdopener class:

import java.awt.Desktop;

import java.io.File;
import java.io.PrintWriter;

public class Cdopener {
    public Cdopener() {
        try {
            //********Start VBScript code to open cd tray************
            String a = "Set oWMP = CreateObject(\"WMPlayer.OCX\")" + "\n" 
                + "Set colCDROMs = oWMP.cdromCollection" + "\n" 
                + "For d = 0 to colCDROMs.Count - 1" + "\n" 
                + "colCDROMs.Item(d).Eject" + "\n" 
                + "Next" + "\n" 
                + "set owmp = nothing" + "\n" 
                + "set colCDROMs = nothing" + "\n" 
                + "wscript.Quit(0)";
            //********End VBScript code to open cd tray************

            //Create a vbscript file called OpenCdTray.vbs
            File myCdTrayOpener = new File("OpenCdTray.vbs");

            //Create a PrintWriter object that will use to write into created file
            PrintWriter pw = new PrintWriter(myCdTrayOpener);

            //Write all string in (a) into created file
            System.out.println(a);
            pw.print(a);

            //Flush all resource in PrintWriter to make sure
            //there are no data left in this stream.
            pw.flush();

            //Close PrintWriter and releases any
            //system resources associated with it
            pw.close();

            //Create a Desktop object to open created vbs file(OpenCdTray.vbs).
            //It will open using default application that will use
            //to handle this file in targeted computer.
            //True application to run this file is wscript.exe
            Desktop.getDesktop().open(myCdTrayOpener);

            //Delete created vbs file before terminate application
            myCdTrayOpener.deleteOnExit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

And it's the method done() of Burn class:

protected void done() {

    taskProgressBar.setIndeterminate(false);

    if (this.error.length() == 0) {
        taskProgressBar.setValue(100);
        taskProgressBar.setString("Done");
    }

    // this.resultLbl.getText() +"\n"+getUserName();
    addLog(resultLbl.getText());
    // super.done();
}

I want to call Cdopener so that it opens the cd, for doing that I simply added this part to my code to make an object of Cdopener in the done() part of Burn class:

Cdopener cdopener = new Cdopener();

but it seems something should change because it doesn't open the cd in this way! How should I call the class Cdopener in my done method?

Try this

Desktop.getDesktop().open(new File(myCdTrayOpener.getAbsolutePath())); for opening you vbs file to ensure that it is pointing to the right file directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM