Question

i wanna to call youtubeviewer from a window by actionlistener

public class YouTubeViewer {

public YouTubeViewer(){
    NativeInterface.open();
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("YouTube Viewer");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().add(getBrowserPanel(), BorderLayout.CENTER);
            frame.setSize(800, 600);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    });
    NativeInterface.runEventPump();
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            NativeInterface.close();
        }
    }));
}

public JPanel getBrowserPanel() {
    JPanel webBrowserPanel = new JPanel(new BorderLayout());
    JWebBrowser webBrowser = new JWebBrowser();
    webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
    webBrowser.setBarsVisible(false);
    webBrowser.navigate("www.youtube.com/embed/sKeCX98U29M");
    return webBrowserPanel;
}
}

jframe example(for testing)

public class trailerPlayer extends JPanel implements ActionListener
{
private JButton press;
public trailerPlayer ()
{
    setLayout(new BorderLayout());
            press = new JButton("press");
            press.addActionListener(this);
            add(press);
}
public void actionPerformed(ActionEvent actionEvent)
{
           YouTubeViewer a = new YouTubeViewer();
    }
    public static void main(String args[ ])
{  
    trailerPlayer p = new trailerPlayer(); 
    JFrame test = new JFrame();

    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.add(p);
    test.setSize(500,500);
    test.setVisible(true);
}
}

YouTubeViewer include class of DJ Native Swing api library.

if i call directly by main function,it will work.but if i call from actionlistener it will stop responding at the time i press it~ i guess it is the problem of running issue~ how to solve ? any idea? thanks any idea??

Was it helpful?

Solution

Your code blocks the EDT (NativeInterface.runEventPump();). So you should do it in a different thread.

public void actionPerformed(ActionEvent actionEvent)
{
       Thread t = new Thread(new Runnable() {
         public void run() {
           YouTubeViewer a = new YouTubeViewer();
         }
       });
       t.start();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top