简体   繁体   中英

Swing and javafx

I am trying to put a resized media player in a swing tabbed pane. The only way I can find to resize the media is to use a media view. But when I try to add the controller I get MediaControl cannot be cast to javafx.scene.Group.

My code is;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

public class JavaFX_insideTab {

private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";

private static void initFxLater(JFXPanel panel) {
    Group root = new Group();
    Scene scene = new Scene(root, 450, 200);
    // create media player
    Media media = new Media(MEDIA_URL);
    MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.setAutoPlay(true);

    MediaView mediaView = new MediaView(mediaPlayer);

    mediaView.setFitWidth(450);
    mediaView.setFitHeight(200);

    MediaControl mediaControl = new MediaControl(mediaPlayer);
    scene.setRoot(mediaControl);

    ((Group) scene.getRoot()).getChildren().add(mediaView);

        panel.setScene(scene);
}

private static void initSwingLater() {
    JFrame jFrame = new JFrame("- JFrame -");
    jFrame.setSize(540, 426);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.getContentPane().setLayout(null);

    final JFXPanel jFXPanel = new JFXPanel();
    jFXPanel.setBounds(0, 0, 540, 188);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(0, 0, 528, 392);
    jFrame.getContentPane().add(tabbedPane);

    JPanel panel = new JPanel();
    tabbedPane.addTab("New tab", null, panel, null);
    JPanel playerpanel = new JPanel();
    tabbedPane.addTab("Media Player", null, playerpanel, null);

    JPanel panel_1 = new JPanel();
    playerpanel.add(jFXPanel);

    Platform.runLater(new Runnable() {

    @Override
    public void run() {
    initFxLater(jFXPanel);
    }
    });

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
    public void run() {
        initSwingLater();
    }

    });
}

}

where am I going wrong?

Assuming this is your MediaControl , then it is already a Parent subclass, so you don't need to place it in a parent Group .

You can use the following code:

private static void initFxLater(JFXPanel panel) {
    MediaPlayer mediaPlayer = new MediaPlayer(new Media(MEDIA_URL));
    mediaPlayer.setAutoPlay(true);

    MediaView mediaView = new MediaView(mediaPlayer);
    mediaView.setFitWidth(450);
    mediaView.setFitHeight(200);

    panel.setScene(new Scene(new MediaControl(mediaPlayer));
}

I'm not quite sure what you mean by "resize the media", but the above code will always size the media to fit within the bounding box of size 450x200 (with the default of preserving the size ratio of the media).

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