Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Java] Changing focus with tab key

Credits go to bamccaig and ImLeftFooted for helping out!
This thread is locked; no one can reply to it. rss feed Print
[Java] Changing focus with tab key
Schyfis
Member #9,752
May 2008
avatar

Now that I've got my web application working, I need to make sure it can be used by blind people, but I'm having problems with focus traversal using the tab key.
Consider a case with two JTextFields: a username field and a password field.

When running from the desktop in a JFrame, the app properly handles focus traversal, going from the username field to the password field.

When running as an applet through a browser, the applet loses focus as soon as tab is pressed.

When running as an applet with the applet viewer, there is no focus traversal. The cursor remains in the username field.

Which obscure/arcane/mystic HTML property or Java function do I need to use to get focus traversal to work in an applet? ::)

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

ImLeftFooted
Member #3,935
October 2003
avatar

Forgive me, I only skimmed (long post :P). No idea about java, but I saw HTML in there.

tabIndex

<input type="text" tabindex="1" onfocus="this.value=this.tabIndex">
<input type="text" tabindex="2" onfocus="this.value=this.tabIndex">
<input type="text" tabindex="3" onfocus="this.value=this.tabIndex">

Code above can be tried at: tmp.html. While there use tab to go through the boxes. When focused, the box will display its tab index.

Schyfis
Member #9,752
May 2008
avatar

The trouble is that the text fields are inside the applet.
I suppose one way to get around it is to have the applet spawn a JFrame instead of adding a JPanel to itself, but I'd like to avoid that if I could.

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

bamccaig
Member #7,536
July 2006
avatar

Apparently for java.awt there is java.awt.Component::setFocusTraversalKeysEnabled(boolean). I'm not 100% sure that it would solve your problem anyway. I'm not overly familiar with Java, but I'm gathering that JFrame and JPanel suggest that you're using java.swing. Is it possible that you just have to capture key events and handle the tab key so the browser doesn't see it?

???

Schyfis
Member #9,752
May 2008
avatar

I tried the FocusTraversal thing, but it didn't have any effect. I might not have been using it on the right components.
Fortunately, I found a fairly simple solution today:

for(Component c : myJPanel){
  c.setFocusable(true);
}

It instantly fixed the problem, although why it was necessary in the first place is anyone's guess.
I also found out that the default JButton-pressing key in applets is the spacebar, not enter. (Fortunately there was a simple solution for this one as well. :D)
Why are Java applets so backwards compared to Java desktop apps? ::)

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

bamccaig
Member #7,536
July 2006
avatar

Schyfis said:

Why are Java applets so backwards compared to Java desktop apps? ::)

Probably so they behave nicer in browsers. Not all Java applets are the entire site. Sometimes they're just a small part and by default it's probably best to cooperate with the rest of the page rather than take things over. I'm sure programmers are far more likely to put effort towards making their applet work the way they want than they are towards making it cooperate. ;)

Go to: