How can I execute a class (builds a JFrame with a browser) by means of an actionPerformed of a button? [closed]

-1

I'm building a java application that has a browser built through a java class.

I want to know how I can call this class (executable class) through an actionPerformed of a JButton, that is, when I click a button I want the browser to be built by the class and initialized.

Without the button call the browser works fine, so I just need to know how to call it by a JButton.

Follow the browser builder class:

package view;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;

import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;

public class YoutubeViewer{
    public static String url;

    public YoutubeViewer(String url) {
        this.url = url;
    }   

    public static void main(String[] args) {
        NativeInterface.open();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Reprodução de Áudio/Vídeo");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_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 static JPanel getBrowserPanel() {
        url = "https://www.youtube.com/v/UWz_Sqj-mvI";
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        JWebBrowser webBrowser = new JWebBrowser();
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        webBrowser.setBarsVisible(false);
        webBrowser.navigate(url);
        return webBrowserPanel;
    }
}

I have a button already done and need to call the above class via an actionPerformed. What code do I use to accomplish this?

Following the actionPerformed of the JButton:

private void btnNavegadorActionPerformed(java.awt.event.ActionEvent evt) {                                          
    /* 
       Call browser
       Code here
    */
}      
    
asked by anonymous 24.07.2017 / 17:43

1 answer

1

Your screen is being built in main, unless it is extremely necessary, delegate the build to your builder, removing everything from the main:

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;

import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;

public class YoutubeViewer{

    public String url;

    public YoutubeViewer(String url) {
        this.url = url;

        NativeInterface.open();

        JFrame frame = new JFrame("Reprodução de Áudio/Vídeo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_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() {
         //url = "https://www.youtube.com/v/UWz_Sqj-mvI";
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        JWebBrowser webBrowser = new JWebBrowser();
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        webBrowser.setBarsVisible(false);
        webBrowser.navigate(url);
        return webBrowserPanel;
    }
}

And then the button:

private void btnNavegadorActionPerformed(java.awt.event.ActionEvent evt) {                                          

    YoutubeViewer ytv = new YoutubeViewer("link a ser aberto");
}   

Note that the getBrowserPanel method no longer needs to be static if you delegate to the builder the screen creation. And the url = "https://www.youtube.com/v/UWz_Sqj-mvI"; line needs to be removed otherwise the url passed as parameter will never be loaded.

    
24.07.2017 / 17:54