Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JavaFX {

    private static String[] urlList = {"http://www.google.com", "https://paytm.com", "http://www.worthindore.com/", "http://www.facebook.com"};
    private static String scUrlCurrent = urlList[0];
    private static JComboBox urlCombo;

    private static void initAndShowGUI() {
        JFrame frame = new JFrame("FX");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null); // do the layout manually
        final JButton jButton = new JButton("Button");
        final JFXPanel fxPanel = new JFXPanel();
        urlCombo = new JComboBox(urlList);
        frame.add(jButton);
        frame.add(urlCombo);
        frame.add(fxPanel);
        frame.setVisible(true);
        urlCombo.setSize(new Dimension(400, 27));

        urlCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object selectedItem = urlCombo.getSelectedItem();
                scUrlCurrent = selectedItem.toString();
                System.out.println("Current Urllll=" + scUrlCurrent);
            }
        });

        fxPanel.setSize(new Dimension(800, 800));
        fxPanel.setLocation(new Point(0, 27));
        frame.getContentPane().setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setResizable(false);

        Platform.runLater(new Runnable() { // this will run initFX as JavaFX-Thread
            @Override
            public void run() {
                initFX(fxPanel);
            }
        });
    }

    /* Creates a WebView and fires up google.com */
    private static void initFX(final JFXPanel fxPanel) {
        Group group = new Group();
        Scene scene = new Scene(group);
        fxPanel.setScene(scene);
        WebView webView = new WebView();
        group.getChildren().add(webView);
        webView.setMinSize(800, 600);
        webView.setMaxSize(800, 600);
        // Obtain the webEngine to navigate
        WebEngine webEngine = webView.getEngine();
        System.out.println("Current Url=" + scUrlCurrent);
        webEngine.load(scUrlCurrent);
    }
    /* Start application */

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGUI();
            }
        });
    }
}
Posted
Updated 13-Mar-15 4:23am
v2
Comments
Darren_vms 13-Mar-15 9:05am    
and the problem is ?
Member 11096170 13-Mar-15 10:07am    
by default google or the selected item is running but when i select from combo-box then it occur an exception...! @ Darren_vms
Darren_vms 13-Mar-15 10:21am    
could you post the exception
Member 11096170 13-Mar-15 10:40am    
Current Url=http://www.google.com
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at epsilon02March.JavaFXDemo.initFX(JavaFXDemo.java:84)
at epsilon02March.JavaFX.access$200(JavaFX .java:27)
at epsilon02March.JavaFX$2.run(JavaFX .java:67)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
BUILD SUCCESSFUL (total time: 4 seconds)
Darren_vms 13-Mar-15 11:16am    
is this when you change the combo box ?

1 solution

Without the exception I'm still going to take a punt at the answer

Java
Object selectedItem = urlCombo.getSelectedItem();
scUrlCurrent = selectedItem.toString();


try changing that to

Java
scURLCurrent = (String) urlCombo.getSelectedItem();


See if that helps

/Darren
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900