[BUG-975] Vision: Web Browser: PopUp

Hi!

I created a page, where I pasted Web Browser component, so I connected to the page from perspective Module (for example ‘/main’).
In this ‘/main’ page I have the button and when I press this button the page opens in new tab.

In Vision the new tab opens in popup…

How should I set width and height parameters to this popup? It opens very-very small…

image

Thanx in Advance!

1 Like

please…

https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013056-handling-pop-ups-swing

You’ll need to translate this Java code to Jython. Also, the underlying implementation is going to change from JxBrowser 6 to JxBrowser 7 in a future Ignition 8 release, which as the article mentions will change how this has to be done.

1 Like

Thanx for the replay!
It’s the easiest way to do it?? :cold_face:
Are there any examples how to translate java to python?

And where to paste this code? :flushed:

If you’re on 7.9.13 or .14, the web browser component has an initialize extension function that’s perfect for this.

Translating Java to Jython is actually pretty easy; mostly you just need to drop extra syntax; Python doesn’t type variables or parameters, nor require strict function signatures; although a tradeoff is the increased verbosity of not having anonymous classes. Something like this; untested, but should work:

from javax.swing import JFrame
from java.awt.event import WindowAdapter
from com.teamdev.jxbrowser.chromium.events import DisposeListener
from com.teamdev.jxbrowser.chromium.swing import BrowserView
from com.teamdev.jxbrowser.chromium import PopupContainer, PopupHandler

class MyWindowAdapter(WindowAdapter):
    def windowClosing(self, e):
        browser.dispose();

class MyDisposeListener(DisposeListener):
    def __init__(self, frame):
        self.frame = frame

    def onDisposed(self, event):
        self.frame.setVisible(False)

def invokedLater():
    BrowserView browserView = BrowserView(browser)
    browserView.setPreferredSize(initialBounds.getSize())
    frame = JFrame("Popup")
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
    frame.add(browserView, BorderLayout.CENTER)
    frame.pack()
    frame.setLocation(initialBounds.getLocation())
    frame.setVisible(True)
    frame.addWindowListener(MyWindowAdapter())

    browser.addDisposeListener(MyDisposeListener(frame))

class MyPopupContainer(PopupContainer):
    def insertBrowser(self, browser, initialBounds):
        system.gui.invokeLater(invokedLater)

class MyPopupHandler(PopupHandler):
    def handlePopup(self, params):
        return MyPopupContainer()

browser.setPopupHandler(MyPopupHandler())
1 Like

I use Ignition v.8.0.11

Okay, so you don’t have the initialize hook, which makes this slightly harder, but you can still run the same code. In propertyChange, put all the code behind an if event.propertyName == "componentRunning":, then change the browser on the last line to event.source.browser.

1 Like

Try removing the semi-colon–that’s a left-over from java syntax.

1 Like

it didn’t help((

Double check that you aren’t mixing tabs and spaces in your indentation.

2 Likes

THanx!! the problem is in spaces) thanx so much)

The script is now without errors, but where should I set PopUp’s width and height?

I think that’s handled by the line:

You can try doing the following to set your own size though i’m not 100% positive if that will work:

import java.awt.Dimension; //<--- add this with your other imports
browserView.setPreferredSize(Dimension(300,400))
1 Like

the popup doesn’t open when I am lunching the project))

Blockquote
Traceback (most recent call last):
File “event:propertyChange”, line 45, in
AttributeError: ‘NoneType’ object has no attribute ‘setPopupHandler’

source.browser.setPopupHandler(MyPopupHandler()) also doesn’t work…

can you post your complete code from property change?

I’m guessing the source. shouldn’t be there

1 Like
if event.propertyName == 'componentRunning':
	from javax.swing import JFrame
	from java.awt.event import WindowAdapter
	import java.awt.Dimension
	from com.teamdev.jxbrowser.chromium.events import DisposeListener
	from com.teamdev.jxbrowser.chromium.swing import BrowserView
	from com.teamdev.jxbrowser.chromium import PopupContainer, PopupHandler

	
	class MyWindowAdapter(WindowAdapter):
   		def windowClosing(self, e):
			browser.dispose()

	class MyDisposeListener(DisposeListener):
		def __init__(self, frame):
			self.frame = frame

		def onDisposed(self, event):
			self.frame.setVisible(False)

	def invokedLater():
		BrowserView.browserView = BrowserView(event.source.browser)
		browserView.setPreferredSize(initialBounds.getSize())
		frame = JFrame("Popup")
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
		frame.add(browserView, BorderLayout.CENTER)
		frame.pack()
		frame.setLocation(initialBounds.getLocation())
		frame.setVisible(True)
		#frame.setSize(700,500)
		#frame.setPreferredSize(1365, 768)
		frame.addWindowListener(MyWindowAdapter())

		browser.addDisposeListener(MyDisposeListener(frame))

	class MyPopupContainer(PopupContainer):
		def insertBrowser(self, browser, initialBounds):
			system.gui.invokeLater(invokedLater)

	class MyPopupHandler(PopupHandler):
		def handlePopup(self, params):
			return MyPopupContainer()

	event.source.browser.setPopupHandler(MyPopupHandler())

from what I can tell, to get an instance of the browser you have to call:

browser = event.source.getBrowser() 

so in your case you’d have to add the line above and change the last line to:

browser.setPopupHandler(MyPopupHandler())

Hopefully this helps

1 Like

like that?:
image

doesn’t work… oh gosh…