0

How to display Google maps using JFrame? What is the mistake in this code? It's not showing proper location.

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class hyperlink extends JFrame {

    public static void main(String arg[])throws Exception {
        new hyperlink();
    }

    public hyperlink() throws Exception {
        String s = "https://maps.google.com/maps?z=10&q=36.26577+-92.54324";
        JEditorPane pane = new JEditorPane(s);
        pane.setEditable(false);
        final JEditorPane finalpane = pane;
        pane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent r) {
                try {
                    if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                        finalpane.setPage(r.getURL());
                } catch(Exception e) {}
            }
        });
        setContentPane(new JScrollPane(pane));
        setSize(1000,1000);
        setVisible(true);
    }
}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
mani
  • 1
  • 2
  • 1) Please use code formatting for code, input/output & structured documents like HTML or XML. To do that, select the sample and click the `{}` button above the messaged posting/editing form. 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 3) Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..` – Andrew Thompson Dec 02 '13 at 11:08

2 Answers2

4

JEditorPane was never intended to be a general purpose browsing component. You might try the Java-FX based WebView, but I am not sure if that is much better.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1 to Andrew Thompson, @user3056660 see [this](http://stackoverflow.com/questions/13717769/jeditorpane-javascript-and-css-support/13718130#13718130) example. JavaFX webview supports HTML 5 and full CSS support so im sure it will be sufficient in displaying web based items – David Kroukamp Dec 02 '13 at 11:44
1

If you simply want to display maps, better display them as an image. You will need a browser component that will display a dynamic map from google. Also, there could be other issues with javascript compatibility and updates to the map service.

Google provides a way of downloading maps as an image:

https://developers.google.com/maps/documentation/staticmaps/#quick_example

sanket
  • 789
  • 4
  • 16