Accessing Hardware Devices From a JavaFX Application Using Java API

To access your file system, print from your JavaFX application, or access any other hardware, call the appropriate Java classes in your JavaFX code. The following example illustrates how to use a file chooser (JFileChooser class) in your JavaFX application and it is implemented in the VideoCube example.

Note: The source code example displays a part of the code of the sample. To see the full code, go to the VideoCube sample page and download the VideoCube.fx file.

    Source Code: Importing Java Class and Calling JFileChooser class in the JavaFX code
    ... 
    import javax.swing.JFileChooser; 
    import javax.swing.filechooser.FileFilter; 
    ...
    
    
    ...
       onMouseClicked: function (event) {
         rotation.pause();
         if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(null)) {
           source = chooser.getSelectedFile().toURI().toString()
         }
         rotation.play();
       }
    ...
    

    Java APIs are different between desktop and mobile platforms. On a mobile device, use the appropriate JSR to access your hardware and include the appropriate JSR .jar file in the project. For example, if you want to use the accelerometer on you mobile device, use the JSR 256 Mobile Sensor API for accessing an accelerometer from JavaFX code.

    Related Links