For a solution with integrated JavaFX MediaPlayer in Swing
Use the JFXPanel and be careful to use JavaFX objects only in the JavaFX stream and after proper initialization of the JavaFX system.
JavaFX is just plain Java, which makes the question a bit confusing, but I think you mean Swing.
Here is an example of an audio player that starts from Swing. The example assumes that in the public folder with music samples for Windows 7, by default there are several mp3 files (C: \ Users \ Public \ Music \ Sample Music), and each file is played in turn.

JavaFXMediaPlayerLaunchedFromSwing.java
This code is responsible for creating the Swing application, which in turn initializes the JavaFX toolkit and creates the JavaFX scene in the JavaFX application thread.
import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.scene.Scene; import javax.swing.*; public class JavaFXMediaPlayerLaunchedFromSwing { private static void initAndShowGUI() {
MediaSceneGenerator.java
Creates a JavaFX media player that sequentially plays all .mp3 media files in the specified folder. It provides some controls for multimedia (play, pause, skip track, progress indicator of playback of the current track).
import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.layout.VBox; import javafx.scene.media.*; import javafx.util.Duration; import java.io.File; import java.util.*; public class MediaSceneGenerator { private static final String MUSIC_FOLDER = "C:\\Users\\Public\\Music\\Sample Music"; private static final String MUSIC_FILE_EXTENSION = ".mp3"; private final Label currentlyPlaying = new Label(); private final ProgressBar progress = new ProgressBar(); private ChangeListener<Duration> progressChangeListener; public Scene createScene() { final StackPane layout = new StackPane();
If you just need your own JavaFX application with MediaPlayer and without Swing
The solution above that uses Swing answers the question asked. However, I noted that sometimes people choose this answer and use it to create Java-based media players, even if they donβt have an existing Swing application in which they embed their application.
If you do not have an existing Swing application, completely remove the Swing code from your application and instead write your own JavaFX application. To do this, use the JavaFXMediaPlayer class below instead of the JavaFXMediaPlayerLaunchedFromSwing class from the example above.
JavaFXMediaPlayer
import javafx.application.Application; import javafx.stage.Stage; public class JavaFXMediaPlayer extends Application { @Override public void start(Stage stage) throws Exception { stage.setScene(new MediaSceneGenerator().createScene()); stage.show(); } }
Answers to the following questions
Will JavaFX automatically be added to my .JAR file created with Swing after I add it to the libraries in NetBeans?
Note: the information in this subsequent packaging answer is probably out of date, and other packaging preferences currently exist (e.g. https://github.com/openjfx/javafx-maven-plugin ).
Technically, Swing does not create Jar files, but makes Jar from javafx packaging commands.
If your application contains JavaFX, it is best to use JavaFX packaging tools . Without them, you might have deployment problems, because the Java runtime jar (jfxrt.jar) is not automatically in the java download path for jdk7u7. Users can manually add it to the classpath at runtime, but it can be a little painful. In future versions of jdk (possibly jdk7u10 or jdk8), the jfxrt.jar file will be in the class path. Even so, it is recommended that you use the JavaFX packaging tools, as this will be the best way to ensure that your deployment package works in the most compatible way.
The NetBeans project SwingInterop is an example of a NetBeans project that uses the JavaFX deployment tools for a Swing project in which JavaFX components are embedded. The source for SwingInterop is part of the JDK 7 download and JavaFX sample files and examples .