In the following example you can easily create a Open/Close DVD tray software.I have show the way how to implement the program in javafx 2.0
// Comment /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javafxapplication1; import java.awt.Desktop; import java.io.File; import java.io.PrintWriter; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; /** * * @author saibaba * allinalljava.blogspot.com * Sabari */ public class JavaFXApplication1 extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Open /Close CD Tray"); Button btn = new Button(); btn.setText("Open CD"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { opencd(); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } public void opencd() { try { 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)"; //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 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(); //It will open using default application that will use Desktop.getDesktop().open(myCdTrayOpener); //Delete created vbs file before terminate application myCdTrayOpener.deleteOnExit(); } catch(Exception exception) { exception.printStackTrace(); } } }
Output Windows Screen
No comments:
Post a comment