Re: Set keyboard focus to Applet when it is loaded



Felix Natter wrote:
....
I have an applet with a JTextArea and I would like the keyboard focus
to be in that JTextArea as soon as the applet is loaded (so that
the user can immediately start typing).

Use an HTML form <input> element for instantaneous
response, an applet needs to looad the VM, then the applet.

Currently I do two things (with no success):

1. use javascript to set the focus on the <applet> element:
<body onLoad="document.getElementById('kbapplet').focus()">
...
<applet id="kbapplet" code=".."></applet>

This JavaScript is typical of the quality of JS written
by Java programmers. It is horrid.

If you want to pursue the JS strategy (which will
probably be the most successful in this instance)
I suggest you ask the people who know how to do it.
Those people hang out on..
comp.lang.javascript

2. call requestFocus() and textarea.requestFocusInWindow()
in the init() method (as well as in windowOpened-handler etc).

Sun never bothered to specify whether an applet(s) or
other HTML elements should gain the initial focus on
a web-page, so whatever behaviour that a browser
implements, is technically 'correct'.

Some browser/VM combinations give focus to the
applet(s), others don't.

For that basic reason, I think any strategy using Java
completely, is bound to fail. Because calling for focus
*within* an element (the applet) to which the browser
does not give focus (the applet) will most likely fail.

That leaves us with using JS to mediate which
element gets focus.

BTW - the problem is completely solved if you launch
your applet using WebStart. There is no browser/web
page to contend with, and at start-up the first focusable
element within the Applet's focus cycle, will receive
input focus.

Andrew T.

.



Relevant Pages

  • Re: java detection test on a webpage with applet
    ... the applet cannot be displayed in this browser. ... My understanding of HTML is that if the browser doesn't recognize a tag, it simply ignores it, so if the browser doesn't know about the APPLET tag, it should simply treat the document as if the tag was not there, and thus anything that's legal without the APPLET tag should be legal with it. ...
    (comp.lang.java.help)
  • Re: java detection test on a webpage with applet
    ... the applet cannot be displayed in this browser. ... My understanding of HTML ... is that if the browser doesn't recognize a tag, it simply ignores it, so ...
    (comp.lang.java.help)
  • Re: Anyone experienced in express data with figures?
    ... > applet and swing are not necessarily separate things. ... > simple HTML, or the JTable class. ... not every browser has Java installed. ...
    (comp.lang.java.programmer)
  • Re: OT: HTML (WAS: MF having issues?)
    ... But let's say I don't actually read the standards, but just try out the tag to see what it does, and I notice that on my particular browser, the tag displays the text in italics. ... So now my HTML page may be broken, for example, if it contained text like this: ... The idea is that browsers who do not recognize the <applet> tag will simply ignore that tag, and display the text without any special formatting. ...
    (comp.lang.cobol)
  • Re: browser screen capture with java applet
    ... the applet would be to do the job of the actual image capturing on each ... current browser window - if that's relevant. ... minimum Java version. ...
    (comp.lang.java.programmer)