How can I run a class (build a Jframe with browser) through an actionPerformed button?

Asked

Viewed 478 times

-1

I am 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 perfectly, so I just need to know how to call it by a Jbutton.

Follows browser constructor 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 made and I need to call the class above by means of an actionPerformed. What code do I use to accomplish this?

Follows actionPerformed of Jbutton:

private void btnNavegadorActionPerformed(java.awt.event.ActionEvent evt) {                                          
    /* 
       Call browser
       Code here
    */
}      
  • You want to open an executable by passing parameters via java?

  • I want to open the class above, by means of a button

  • Start by initializing the screen inside the constructor and remove this main ai. The main should only exist in the main application class.

  • then I must instantiate the class in the button actionPerformed?

  • Not quite what I said, I will try to answer, although the question does not have an executable code.

  • I have to put the code that builds the frame and the function that returns the browser inside the constructor?

  • See the answer below, was what gave to respond with the past information.

Show 2 more comments

1 answer

1

Your screen is being built in the main, unless it is extremely necessary, delegate the construction to your constructor, 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 on the button:

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

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

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

  • Now, it is instantiating normally, because even the window is started by the system, but it is not possible to see the window nor the application is started. Aparece o seguinte erro: "Exception "java.lang.ClassNotFoundException: org/openide/loaders/DataObject"while constructing DataFlavor for: application/x-java-openide-dataobjectdnd; mask=4; class=org.openide.loaders.DataObject
"

  • @Viniciusviana then it is necessary that you provide a [mcve] so that it is possible to test.

  • I have the following situation: I have a Jframe that starts the application and inside Jframe I have a button that calls the browser constructor class by initializing it with the url passed as a parameter. Then I need to know how to initialize the class with the parameter passed.

  • @Viniciusviana I read the question, but as I said, it is not enough to just explain by text, it is necessary to demonstrate the problem through a Minimum, Complete and Verifiable Example (read the link: https://answall.com/help/mcve) so that me and other people willing to help you can simulate the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.