-1

I have an applet and a frame in it. I want this frame to be opened when applet is launched.

public class MyApplet extends JApplet {
    public void init() {
        Frame frame = new JFrame("myframe");
        frame.setVisible(true);
        frame.toFront();
    }
}

This doesn't work. Browser window is on top whereas frame is hidden.

Tried all that was written in How to bring a window to the front?

Community
  • 1
  • 1
Boris
  • 1,054
  • 1
  • 13
  • 23
  • So far you have asked 13 [questions](http://stackoverflow.com/users/368532/boraldomaster?tab=questions) and only accepted 3 answers. You might want to change that ratio lest you be thought of as an [help vampire](http://meta.stackexchange.com/questions/19665/the-help-vampire-problem). – Andrew Thompson Feb 03 '14 at 14:36
  • *"Tried all that was written in How to bring a window to the front?"* Really? [This answer](http://stackoverflow.com/a/596141/418556) explicitly mentions `setAlwaysOnTop(..)`, so either you did *not* try everything on that page, or you chose an answer that advised to do what you had *already tried.* Which is it? – Andrew Thompson Feb 03 '14 at 15:01
  • OK, accepted answers that were really helpful. – Boris Feb 05 '14 at 09:21
  • >> This answer explicitly mentions setAlwaysOnTop(..) - it mentions but id doesn't work in the way it is mentioned. Besides they advice to do setAlwaysOnTop(false) - whereas the right answer is setAlwaysOnTop(true). – Boris Feb 05 '14 at 09:23
  • 1
    Oh, so you *finally* noticed that comment.. Good for you. Pity you could not pay attention all the way through the linked answer. `super.setAlwaysOnTop(true); super.toFront(); super.requestFocus(); super.setAlwaysOnTop(false);` What is the first quoted statement? – Andrew Thompson Feb 05 '14 at 09:27
  • 1
    Hm, yes, this also works. Suppose I haven't tried it in whole, just pieces. OK, call me a vampire. – Boris Feb 05 '14 at 09:37
  • Finally! I think BTW, that setting a frame to be always on top *permanently* is a hack (if only used to bring a frame in front of an applet), and will run into problems as mentioned in my first comment to the accepted answer. – Andrew Thompson Feb 05 '14 at 09:41
  • 1
    Perhaps a better *overall* approach to this problem is to use a `JDesktopPane` within the applet itself. It side steps the 'goes behind' problem, and provides an automatic 'scroll the internal frames/dialogs' off the page as well.. But then, with you wanting to 'abstract' the problem beyond anything I can understand, I do get frustrated with your replies. When I ask a question, no matter how silly or trivial it seems, I expect to see an answer as best you can form it. If my questions don't make sense to you, ask me what I mean. (Is my advice.) – Andrew Thompson Feb 05 '14 at 09:46
  • OK, I'll try JDesktopPane too. – Boris Feb 05 '14 at 10:02
  • But don't be so strict, please, I just try to collect all possible information. Your notes about best practices are also valuable for me, but I'd also like to have reply to my exact questions as well as notes about what is a good/bad idea. Just to have full information. So I will appreciate answers like this: 'you can do this using method X, but it is bad idea to do it because ...'. – Boris Feb 05 '14 at 10:03
  • I don't have time for that level of 'leniency'. Take it or leave it. – Andrew Thompson Feb 05 '14 at 10:26

2 Answers2

5

Try frame.setAlwaysOnTop(true):

Sets whether this window should always be above other windows.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Omoro
  • 972
  • 11
  • 22
  • *"..should always be above other windows."* Yes.. except of course when those *other windows* also call it. Then they slug it out for top position. In that type of fight, I'd bet a frame launched from the JRE will lose to a native executable - because I doubt Oracle does much to hack the OS to achieve 'always on top' whereas the average native executable that does often goes to *extreme* lengths to maintain the always on top status. – Andrew Thompson Feb 03 '14 at 14:41
  • `java.security.AccessControlException: access denied ("java.awt.AWTPermission" "setWindowAlwaysOnTop")` Oh, and of course, it will also require an `all-permissions` applet even to enter the competition for always on top. I don't know about you, but I wouldn't be clicking OK when prompted to provide full access to the PC, just to get a frame that floats on top of the web page. ;) – Andrew Thompson Feb 03 '14 at 14:51
  • I think you also need to set the applet focus to false if possible. – Omoro Feb 03 '14 at 14:58
4

The obvious solution is not to open the frame using an applet. Instead, launch the frame from a link using Java Web Start.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433