Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

keyboard usage of a JOptionPane

 
Andrew Collins
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
I have this JOptionPane :

When I press the tab button on this dialog the focus nicely switches between someTextField, button1 and button2.
Now here's my problem, even when button2 has the focus, it's allways button1 that gets "pressed" when I hit the return key.
Does anyone of you know how to solve this ?
Thanks
A
[ September 04, 2002: Message edited by: Andrew Collins ]
 
John Smith
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which Look And Feel are you using? I seem to recall a "feature" of the Java Look And Feel that caused the return key to always press the default button in an option pane, and not the one that currently has the focus. I think the Windows Look And Feel has the behavior you are expecting. Also, this might have been fixed in 1.4, but that's just a vague guess...
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code u have sent is working fine with me. The button which has the focus is the one which is hit. I'm using JDK 1.3.1. Are you using JDK 1.4.0 by any chance?
 
Andrew Collins
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Thanks for the quick response.
I'm using JDK1.3 and the default Java L&F.
Switching to JDK1.4 doesn't solve the problem, explicitely setting the Windows L&F or the Motif L&F does !!
Major drawback : both look like sh*t.
Can you guys help me to solve this one using the Java L&F, please ?
Thanks !
A
 
John Smith
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were you, I'd either subclass JOptionPane or make a new class, call it JMessageBox or whatever, that has the new functionality you want. I actually never use JOptionPane's anymore specifically because of these types of problems (I like to switch button focus by pressing the left or right arrow keys, and JOptionPane's don't have the ability to display or use mnemonics in the buttons, as far as I know). So, I implemented my own class called JMessageBox which is actually very simple.
A JOptionPane is basically a simple JDialog with a couple buttons (of course, some have list boxes/drop-down boxes so that would make it slightly more complicated, but you may not need that functionality). Easy enough to code your own!
If this solution isn't practical for you, perhaps you could attach a key listener to the JOptionPane and listen for the return key. Or you could try subclassing the Java Look and Feel JOptionPane's UI component (see this page for more information) and make it have the "feel" you need (you may just be able to copy the code from the Windows or Motif L&F, since they already have the "feel" you like, just not the "look").
Hope this helps!
Randy
 
Andrew Collins
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randy,
Thanks for your most useful information !
Unfortunally, overriding the feel of a JOptionPanel is much more complicated than the example provided in the Swing tutorial you provided a link to. I've spend the whole day trying to figure out wich method I "simply" have to override to get that Windows feel and a Java's look....but it's too complicated...
It can't be that hard I think but I'm just not experienced enough to find my way in the Swing jungle....
Could ( any of ) you give me a hint where (in which class for example) I should look, please ?
 
John Smith
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
Try putting this in your code before you create the JOptionPane:

This causes all JButton's that are created after this point to use the Windows Button UI. When I did this (in the Java L&F), the buttons operated in the way you and I are expecting. However, the buttons look a little different now, so I would compare the classes:
javax.swing.plaf.metal.MetalButtonUI
and
com.sun.java.swing.plaf.windows.WindowsButtonUI
Remember that the Java SDK ships with the source code for all the class files (in the file src.zip) so it's easy to look at the source for any class.
I briefly glanced at the two classes above, and didn't see anything glaringly obvious, but I saw a class called javax.swing.plaf.basic.BasicButtonListener that might be interesting. Anyway, see if you can figure out why the WindowsButtonUI works the way you want it to and why MetalButtonUI does not, then either subclass one of them or make your own ButtonUI class and install it using the following line somewhere in your code (before any JButtons have been created):

Also, you can complain to Sun about why they closed this bug here. You'll need a login name and password to view it (it's free).
Hope this helps!
Randy
[ September 06, 2002: Message edited by: Randy Gordon ]
 
Andrew Collins
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy,
Thanks, pal !
Your hints have been most helpful !
I finally managed to implement the desired behavior :


and

Overriding the createButtonListener method resulted in the correct behaviour.
Thanks Again. I would never found this one without your help
 
John Smith
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic