Swing组件中的JFXPanel在JFXPanel构造上挂起应用程序(os x& JDK 1.8)

时间:2014-10-22 16:35:35

标签: java macos swing javafx java-8

我在swing组件(JPanel)中运行JFXPanel。我遇到的问题是,有时候,并非每次都会在创建JXFPanel时挂起(冻结)应用程序。见下面的代码。

public VideoPlayer(String url){
    if (MovieInfoConfig.DEBUG)
        System.out.println("1 Creating VideoPlayer Objct...");

    this.videoUrl = url;
    jfxPanel = new JFXPanel();

    if (MovieInfoConfig.DEBUG)
        System.out.println("2 JFXPanel object created...");

    createScene();
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(800, 560));
    add(jfxPanel, BorderLayout.CENTER);
}

您可以看到我的调试消息正在打印。我将始终到达第一步,但正如我所说,在某些情况下,程序在到达第二个调试消息之前会挂起。换句话说,行jfxPanel = new JFXPanel()似乎导致了问题。

我只在Mac OSX(Mavericks)JDK 1.8上测试了这个。对我来说,它有点像JavaFX / OSX JDK 1.8错误 - 但我还没有在网上找到任何关于它的信息。

有没有人有任何线索?有没有办法让我调试JFXPanel构造函数本身,看看它在应用程序挂起之前发生了什么?

谢谢大家!

编辑1 根据建议,我对主要方法进行了一些更改。然而,它没有消除这个问题。请参阅下面一个重现问题的完整示例:

import java.awt.*;
import java.awt.event.*;
import javafx.application.*;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import javax.swing.*;

public class BrowserTest extends JFrame {
    JPanel videoP = new JPanel();
    BrowserTest() {
        super("Test");
        System.out.println("Start BT");
        setSize(1200, 700);     
        setLayout(new BorderLayout()); 
        JPanel p1 = new JPanel();
        String[] videos = new String[3]; 
        videos[0] = "https://www.youtube.com/embed/W-J2OYN9fF8?autoplay=true&controls=0";
        videos[1] = "https://www.youtube.com/embed/8hP9D6kZseM?autoplay=true&controls=0";
        videos[2] = "https://www.youtube.com/embed/Rq9eM4ZXRgs?autoplay=true&controls=0";
        for(int x = 0; x < videos.length; x++) {
            JButton b = new JButton("Video " + x); 
            b.addActionListener(new bClick(videos[x])); 
            p1.add(b);
        }
        add(p1, BorderLayout.NORTH);
        add(videoP, BorderLayout.CENTER); 
        setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 Platform.setImplicitExit(false);
                 new BrowserTest(); 
             }
         });
    }

    class bClick implements ActionListener {
        String url; 
        bClick(String url) {
            this.url = url; 
        }
        public void actionPerformed(ActionEvent e) {
            if (videoP.getComponents().length > 0) {    
                Component c = videoP.getComponent(0);
                if (c instanceof VideoPlayer)
                    ((VideoPlayer) c).stopTrailer(); 
            }
            videoP.removeAll();

            videoP.add(new VideoPlayer(url)); 
            System.out.println("Clicked url " + url); 
            videoP.revalidate();
            videoP.repaint();
        }
    }
}

class VideoPlayer extends JPanel {  
    private Stage stage;
    private WebView browser;
    private JFXPanel jfxPanel;
    private WebEngine webEngine;
    private String videoUrl;
    int xPos, yPos; 

    public VideoPlayer(String url){
        this.videoUrl = url;    
        System.out.println("1 Creating VideoPlayer Objct...");
        jfxPanel = new JFXPanel();  
        System.out.println("2 JFXPanel object created...");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(800, 560));
        add(jfxPanel, BorderLayout.CENTER);
        createScene();
    }

    private void createScene() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println("3 createScene run metod started");
                stage = new Stage();
                System.out.println("4 createScene - stage created");
                stage.setTitle("Video");
                stage.setResizable(true);
                Group root = new Group();
                Scene scene = new Scene(root,80,20);
                stage.setScene(scene);
                System.out.println("5 createScene Group and Scene created - also set the Scene");
                //Set up the embedded browser:
                browser = new WebView();
                System.out.println("6 createScene - WbView created");
                webEngine = browser.getEngine();
                webEngine.load(videoUrl);
                System.out.println("7 createScene - Loeaded the video URL: " + videoUrl);
                ObservableList<Node> children = root.getChildren();
                children.add(browser);                     
                jfxPanel.setScene(scene);
                System.out.println("8 createScene - set the scene on the jfxPanel");
            }
        });
    }

    public void stopTrailer() { 
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                System.out.println(":: stopTrailer() called");
                remove(jfxPanel);
                webEngine.load(null);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:2)

同样的问题出现在我的OSX中。从eclipse运行时,一旦我为jar创建了一个安装程序(.app),它就会运行文件并挂起。所以我找到了一条出路,在Info.plist文件中为OSX创建.app时,我增加了最小和最大内存。这解决了问题,现在运行顺利。不确定这是否能解决您的问题,只是想与您分享。

谢谢!

答案 1 :(得分:2)

在stopTrailer方法中,在JavaFX线程中运行时,JavaFx面板将从其swing容器中删除。

Platform.runLater(new Runnable() {
            ...
            remove(jfxPanel); 
            webEngine.load(null);
        }
    });

在类似的代码中,我也错误地在JavaFX线程上添加了JFXPanel并且在mac上有相同的挂起。当我找到并删除了坏的添加时,代码就停止了。

我相信正在发生的事情是,在Windows和Linux上,由于AWT和JavaFX线程完全不同,代码虽然不好,但不会造成灾难。但是,在mac上,底层UI事件必须发生在同一本机OS线程上,从JavaFx线程中删除或添加jfxPanel几乎肯定会导致AWT / Swing和JavaFX最终导致这些UI资源死锁。

相关问题