Search code examples
javascriptjavaswinggeolocationjxbrowser

How to read from a JOptionPane.showMessageDialog


I'm trying to get the location from a mouse click.I have the following code:

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test {
public static void main(String[] args) {
    final Browser browser = new Browser();
    BrowserView view = new BrowserView(browser);

    final JTextField addressBar = new JTextField(
            "https://developer.here.com/api-explorer/maps-js/v3.0/infoBubbles/position-on-mouse-click");
    addressBar.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            browser.loadURL(addressBar.getText());
        }
    });

    JPanel addressPane = new JPanel(new BorderLayout());
    addressPane.add(new JLabel(" URL: "), BorderLayout.WEST);
    addressPane.add(addressBar, BorderLayout.CENTER);

    JFrame frame = new JFrame("Website");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(addressPane, BorderLayout.NORTH);
    frame.add(view, BorderLayout.CENTER);
    frame.setBounds(1, 1, 1300, 700);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    browser.loadURL(addressBar.getText());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
    }

And when I click on that map from the website, I receive a messageBox with the coordinates.

My question is how to obtain these coordinates(numbers)?

I got that script from the website and created a hmtl file using it and a little javascript code the get,somehow the coordinates by writing them into a text file.

It didn't work and after that I realized that according to some ppl from the internet is impossible to write data in files using javascript(Being a security risk).

So after it,the javascript option is down.And now I wonder if is possible to somehow get these informations from the messageBox... I have to mention that I'm forced to code only with javascript and Java. And I'm using JxBrowser to display the webpage in my Java Application and Swing for GUI.

Calculating a Location from a Mouse Click is a site that provides you the location coordinates on click,the location being sent using an alertwindow(in javascript). When I open this site in my Java app I don't receive the alertwindow (because I open it using Java and not a browser), I receive an JOptionPane.showMessageDialog.

So what I want is to extract/get the coordinates from this JOptionPane.


Solution

  • The DialogHandler.onAlert() method is invoked when JavaScript alert dialog should be displayed. It happens when the window.alert() JavaScript function is invoked. that one is the equivalent you can manage alert by overriding the DialogHandler.onAlert()

    browser.setDialogHandler(new DialogHandler() {
        @Override
        public void onAlert(DialogParams params) {
            String url = params.getURL();
            String title = "The page at " + url + " says:";
            String message = params.getMessage();
            JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
        }
    

    for additional info check tutorial javascript dialog for JXBrowser here