1

I'm currently making an MP3 player on Java and I'd like to use Swing GUI elements exclusively if possible. But I also need the functionality of Media and MediaPlayer classes from JavaFX inside my program. Is there a way to instantiate only these logical elements in my application without the hassle of Scene making and the like? I honestly don't know much about them and I can't seem to understand very clearly the Oracle tutorials on it. Here's the places where I'd be using such items: (I call this class to a main class later on)

    class MP3Panel extends JFrame
    {
        private Media playingnow;
        private MediaPlayer player;

        public MP3Panel()
        {
            player = new MediaPlayer(intoMedia(playlist.getFirstSong()));

            player.setOnEndOfMedia(new Runnable() {

        @Override
        public void run() {
            if(playlist.nextSong(thissong)==null)
            {   player.stop();
                nowplaying.setText("");
            }    
            else
            {    
                player = new MediaPlayer(intoMedia(playlist.nextSong(thissong)));
                nowplaying.setText(thissong.printSongInfo());
                player.play();
            }    
        }
    }

    );

    ...
    private Media intoMedia(Song a)
    {

    thissong = a; 
    thisfile = new File(thissong.getFilename());
    thisURI = thisfile.toURI().toString();
    playingnow = new Media(thisURI);
    return playingnow;
    }

I also use these for an ActionManager class:

    private class ActionManager implements ActionListener
{    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == playbtn)
        {

            if(playbtn.getIcon() == play)
            {
                if(player.getStatus()==MediaPlayer.Status.STOPPED)
                {
                    player = new MediaPlayer(intoMedia(playlist.getFirstSong()));
                    nowplaying.setText(thissong.printSongInfo());
                }
                player.play();
                playbtn.setIcon(pause);
            }
            else
            {
                player.pause();
                playbtn.setIcon(play);
            }
        }
        if(e.getSource() == fwdbtn)
        {
            if(playlist.nextSong(thissong) == null)
            {    
                player.stop();
                playbtn.setIcon(play);
            }
            else
            {
                player = new MediaPlayer(intoMedia(playlist.nextSong(thissong)));
                nowplaying.setText(thissong.printSongInfo());
                player.play();
                playbtn.setIcon(pause);
            }
        }

There are also a backwards and forwards buttons there but I think this is enough to clarify what my dilemma is. Thanks in advance.

Just_Ic3
  • 11
  • 2

1 Answers1

1

Yes, you can embedd any JavaFX user interface into a Swing application by using a JFXPanel which is a Swing component that can display a JavaFX-Scene.

But you won't be able to do this without a JavaFX scene graph.

Have a look at the oracle tutorial on this topic. If you're unfamiliar with javafx scene graphs, you should probably read the Getting Started with JavaFX tutorial.

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
  • And say if I only want this Scene for logical purposes is there a method for hiding this Scene? Or maybe a more elegant solution to it? – Just_Ic3 May 27 '14 at 07:36
  • *"is there a method for hiding this Scene?"* `CardLayout`, `setVisible(false)` on the parent Swing container, `JTabbedPane`.. – Andrew Thompson May 27 '14 at 07:45