Domanda

Voglio chiamare Youtubeviewer da una finestra di 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 Esempio (per test)

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 la classe della biblioteca API swing nativa DJ.

Se chiamo direttamente dalla funzione principale, funzionerà. Ma se chiamerò da ActionListener smetterà di rispondere al momento che lo premerò ~ Immagino che sia il problema del problema di esecuzione ~ come risolvere?qualche idea?Grazie qualche idea??

È stato utile?

Soluzione

Il tuo codice blocca l'EDT (NativeInterface.runEventPump();).Quindi dovresti farlo in un filo diverso.

public void actionPerformed(ActionEvent actionEvent)
{
       Thread t = new Thread(new Runnable() {
         public void run() {
           YouTubeViewer a = new YouTubeViewer();
         }
       });
       t.start();
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top