I have an applet running in a browser tab. When I switch to a different tab in the same browser, or ALT-TAB to another application entirely, the applet loses focus. When I return to the browser tab, the applet doesn't gain focus again. Is there a nice way to make this happen?
I assume it's a JavaScript change that's needed, not in the applet itself? Some onFocus handler perhaps? If it's relevant, our applet is created using an HTML <applet> tag.
I believe java has the requestFocus() method. It's in the Component class, so your JPanel or whatever you are using may be able to use this.
For the javascript part, this is what a quick googling reveals: http://www.raditha.com/java/javascript.php. I hope it helps you!
Related
Does a Java Applet always execute its code even when it losts focus? I've to put this applet in a web page.
I'm tryng to understand this cause i've to develop an applet that listen to some hardware components through JavaPos. I have a callback method defined inside the applet and i'm not sure if it works even when users click on other page component.
Thanks
Does a Java Applet always execute its code even when it losts focus?
Yes, unless of course the applet code intentionally stops execution on loss of focus.
I'm new here and not so much familiar with applets.
I searched the site and many other forums for answer but i found nothing.
I have made an application in Java which starts with an applet login form, and continues with JFrame subforms which are doing several things like running JOptionPanes for example when a customer make a change in his profile.
My problem is that when the JOptionPane appears, the applet start form comes on top even if i have opened 2 or 3 JFrames.
I forgot to say that my app is big so i cannot post any code (and i think is not needed).
Thanks in advance!
Avoid mixing frames and applets, your working with competing paradigms. If you MUST use an applet, use something like a CardLayout or JTabbedPane to allow the user to switch between forms.
If you can, try using Java Web Start, which will allow you to start your application off the web, but have the same restrictions as that of any embedded GUI.
As to your problem, it sends like you are mixing the native peer for the JOptionPane. Make sure that the parent reference is correct (ie references the frame or child of the frame)
We are hosting web applications in a .Net webbrowser control embedded in a winform.
When a java applet in a web application displays a modal dialog, only the applet area within the webapplication is deactivated. (The requirement is firefox-style modality).
Is there a way to detect that the applet in the webbrowser control displays a modal dialog? (alternatively, get a window handle, when a java applet dialog is opening?)
Use some JavaScript to directly call the Java method Window.getWindows() for an array of existing Window objects. (J)Frame objects and (J)Dialog objects all extend Window.
Note that, AFAIR, each applet gets a Frame to contain it. Because there will be more 'windows' on screen than just dialogs, you'll need to check the types.
Do that at an appropriate interval repeatedly from JS, and it should be possible to detect any dialogs produced by Java applets.
Alternately it might be easier to extend the applet and add some extra methods to make those same checks outlined above, then call a JavaScript to notify that there are dialogs opened.
Use sun.plugin2.main.client.ModalityHelper.installModalityListener(). This will signal modalityPopped and modalityPushed.
http://www.javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/plugin2/main/client/ModalityHelper.html#installModalityListener%28sun.plugin2.main.client.ModalityInterface%29
Requires Java 6 and may only work on some specific platforms (scope yet unknown).
I'm working with the eclipse SWT toolkit, and I'm trying to use it to create a browser window that only passes mouse clicks to the underlying document conditionally (I want to stop Flash and Javascript in the page from getting clicks). I'd like some way of doing one of:
Examining mouse events as they come in, and only passing them on to other listeners based on conditions I specify.
Removing all listeners from a window, and only putting back the ones I want.
Are either of these possible?
Browser, like other SWT components, have addMouseListener method. So you could implement your own listener a pass only which one you want.
see javadoc of browser
Edit
According to your request, there could be two possible ways to do it.
First, you could use listening of events from JavaScript in browser (there is no way to avoid JavaScript if you work with html pages). If you know that you will have Mozilla browser render core (you have to install XUL Runner), you could use JavaXPCOM, but that's big unknown for me.
snippet - listen for DOM mousedown events with javascript
Second, you can call Java functions from JavaScript (again, handle onclick event, and then decide on Java, if you don't want to use JavaScript for it).
snippet - call Java from JavaScript
But frankly both ways are more ugly-er that proposed way by pure JavaScript.
In an applet when opened in Internet Explorer, the focus should come back automatically to the applet instead of going to IE when any window comes over IE and goes back (Internet Explorer again becomes the active window).
To get the focus RequestFocus() and RequestFocusInWindow() are not working for an applet in my case.
Can anyone explain how this can be done?
I can only suppose here. Java can't influence on the environment out of the it's, I don't know exactly how to name it, control area -> you can't influence on IE from an applet.