Skip to main content

JDIC Web Browse - can I capture window popup or new window event??

12 replies [Last post]
inkhaton
Offline
Joined: 2007-10-20

I am trying to make an application where i use JDIC as an embedded web browser.
The problem is that whenever I com across a link that opens in a new browser window, such as pop-ups with the target="_blank" in the html link tag, the page opens in a new web browser (IE or Firefox )outside of my application. Is there any way to make the link open in my JDIC embedded browser or somehow trap that event so that i can send it to a new Browser object?

Any help would be appreciated.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
lmei007
Offline
Joined: 2008-01-22

Hi madroadie,

Which JDIC version are you using? I just started using JDIC and the latest version does not have "protected boolean willOpenWindow(URL url)" method in the WebBrowser class anymore. Looks like JDIC cannot handle pop-up or new window.

rajashreemeganathan
Offline
Joined: 2008-04-29

Madroadie,

Can I obtain tabbed browser window using WebBrowser? I am trying to achieve the same.. Could you help me on this..

Thanks,
Rajashree

eitanarat
Offline
Joined: 2008-09-16

Hi, thanks for this great tip, I used your tip successfully
and the new window is opened now in my browser by :

string newWindowURL = new URL(e.getData());
setURL(newWindowURL);

1. Is the new window is still child window and any parent window updates
from the child window will occur ?

2. Is the new window has the same session as the parent window ?

3. Is there any new enhancement that make it available and easier to catch new window and use it as same as window.open popup ?

Thanks in advacne.

dhodtong
Offline
Joined: 2007-12-26

Hello,

I also have such a requirement to capture the window popup event. However when someone click on the Jidebrowser to open another browser window, dispatchWebBrowserEvent() did not get call in WebBrowser.java. Hence my listener did not get call.

Any insights from anyone on this ?

Thanks

inkhaton
Offline
Joined: 2007-10-20

but how can i handle these events.
They are not visible to me anywhere. They are default package.
What event handler would i use? do i have to extend the eventhandler?

madroadie
Offline
Joined: 2006-04-20

Create you own browser class.

public class Browser extends WebBrowser

Then from the host client panel do something like this

public Browser getBrowser()
{
if (browser == null)
{
// This is needed for Vista.
this.browser = (Browser) AccessController
.doPrivileged(new PrivilegedAction()
{
public Object run()
{
return new Browser();
}
});

browser.setPreferredSize(new Dimension(0, 0));
browser.addWebBrowserListener(new WebListener());
}

return browser;
}

class WebListener extends WebBrowserListener

lsmithlin
Offline
Joined: 2008-02-13

Madroadie,

Can you be more specific here? I need to capture a popup and time how long it takes to load. I'm trying to make a hacky monitor to help tell us when a particular page on our website is slow.

I've extended WebBrowser and added the handy-dandy WillOpenUrl and WillOpenWindow methods. I can't even get that to work, I get a blank control.

I've also implemented WebBrowserListener (can't extend it, it gets mad if you try to do that). But I'm not sure where to put the WebBrowserEvent.WEBBROWSER_BEFORE_NAVIGATE and
WebBrowserEvent.WEBBROWSER_BEFORE_NEWWINDOW
mentioned in your earlier post.

I guess I'm not smart enough to figure out how to do this from your descriptions so far.

Hope you can help.

Leon

madroadie
Offline
Joined: 2006-04-20

I also created a Runnable that processes the WillOpenWindow(URL url) method something like the following.

class Browser extends WebBrowser
{
protected PopupBlockerOpenWindowRunnable openWindowRunnable = new PopupBlockerOpenWindowRunnable();

protected URL lastToOpenURL = null;

....
}

/*
* (non-Javadoc) call for the COM event OnBeforeNavigate
*
* @see org.jdesktop.jdic.browser.WebBrowser#willOpenWindow(java.net.URL)
*/
@Override
protected boolean willOpenWindow(URL url)
{
// we always return false as there is serious timing issues with
// underlying browser process with regards to creating threads
// etc.
// Creation of any popup is handled in later thread.

openWindowRunnable.setUrl(url);
SwingUtilities.invokeLater(openWindowRunnable);
return false;
}

protected boolean willOpenURL(URL url)
{
boolean rc = super.willOpenURL(url);
setLastToOpenURL(url);
return rc;
}

public class PopupBlockerOpenWindowRunnable implements Runnable
{
protected URL url;

public PopupBlockerOpenWindowRunnable()
{
}

public void run()
{
URL lastToOpenURL = getLastToOpenURL();
URL srcURL = getURL();
URL popupURL = url != null ? url : lastToOpenURL;

if (shouldBlockPopup(srcURL, popupURL))
processBlockedPopup(srcURL, popupURL);
else
createAllowedContent(popupURL);
}

/**
* @return Returns the url.
*/
public URL getUrl()
{
return url;
}

/**
* @param url
* The url to set.
*/
public void setUrl(URL url)
{
this.url = url;
}
}

// Here's some of my Web Status Bar Listener, don't forget to register it with the //browser addWebBrowserListener(new WebStatusBarListener());
public class WebStatusBarListener extends WebBrowserAdapter
{
protected boolean navError = false;

public void downloadStarted(WebBrowserEvent event)
{
navError = false;
getBrowserStatusBar().updateStatusInfo("Loading started.");
// Save your start time here.
}

public void downloadCompleted(WebBrowserEvent event)
{
getBrowserStatusBar()
.updateStatusInfo("Download completed.");
// Your done downloading a single page item like an image
}

public void downloadError(WebBrowserEvent event)
{
navError = true;
getBrowserStatusBar().updateStatusInfo("Loading error.");
}

public void documentCompleted(WebBrowserEvent event)
{
getBrowserStatusBar().updateStatusInfo(
"Document loading completed.");
// Save the end time of the document load.
}

/*
* (non-Javadoc)
*
* @see org.jdesktop.jdic.browser.WebBrowserListener#downloadProgress(org.jdesktop.jdic.browser.WebBrowserEvent)
*/
public void downloadProgress(WebBrowserEvent webEvent)
{
logEvent("downloadProgress", webEvent);
String values = webEvent.getData();
if (values != null)
{
StringTokenizer tokenizer = new StringTokenizer(values,
" ");

if (tokenizer.hasMoreTokens())
{
String current = tokenizer.nextToken();
if (tokenizer.hasMoreTokens())
{
String max = tokenizer.nextToken();

int progress = Integer.parseInt(current);
int progressMax = Integer.parseInt(max);

onDownloadProgress(webEvent, progress,
progressMax);
}
}
}
}

/**
* @param webEvent
* @param progress
* @param progressMax
*/
protected void onDownloadProgress(WebBrowserEvent webEvent,
int progress, int progressMax)
{
}
}

Madroadie

lsmithlin
Offline
Joined: 2008-02-13

What does createAllowedContent do? I'm new to jdic, so forgive me if you have to baby me along.

madroadie
Offline
Joined: 2006-04-20

Create a New Window or Panel that hosts another Instance of your Browser? I've implemented a Tabbed Browser Panel for this, I create a new Tab and add my BrowserPanel

You don't want IE to create a Window for you or you will loose control, and all context.

BTW WebBrowserListener in an interface not an Object, so you need to use implements not extends.

public class WebBrowserAdapter implements WebBrowserListener
{
....
}

uta
Offline
Joined: 2007-01-10

Potentially you can implement the child of [i]WebBrowser[/i] class with rewritten methods
[b] protected boolean willOpenURL(URL url) { return false; }
protected boolean willOpenWindow() { return false; }[/b]

To make the link open in JDIC embedded browse you have to handle browser evens with ids:
[b]WebBrowserEvent.WEBBROWSER_BEFORE_NAVIGATE[/b] and
[b]WebBrowserEvent.WEBBROWSER_BEFORE_NEWWINDOW[/b]

The destination URL is getting by [b]e.getData()[/b] call (where [b]e[/b] is an instance of [i]WebBrowserEvent[/i] class).

zenmusic
Offline
Joined: 2008-04-20

uta , I'm new here;, this is great information, I have searched the documentation for the browser and find none of these details.. where would I find this?

???like this??

To make the link open in JDIC embedded browse you have to handle browser evens with ids:
WebBrowserEvent.WEBBROWSER_BEFORE_NAVIGATE and
WebBrowserEvent.WEBBROWSER_BEFORE_NEWWINDOW

The destination URL is getting by e.getData() call (where e is an instance of WebBrowserEvent class).

thanks