Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

JOptionPane.showConfirmDialog, getting rid of hotkeys or changing them..

Thread view: 
longoja@yahoo.com - 14 Feb 2006 14:57 GMT
My application uses this method to pop up a dialog to have the User
select Yes or No.  Both the Yes and No are hotkeyed by default to use
Alt-y or Alt-n.  This was not the case when the app was running in JDK
1.3.

I would either like to get rid of the hotkeys or change them to y or n,
instead of having to use the Alt key with them.

Is this possible, or would I have to create my own dialog class?
NullBock - 14 Feb 2006 15:16 GMT
Try instantiating an instance of JOptionPane.  You can then alter the
specifics of how it's going to be shown, including the hot-keys.
Ultimately you would call JOptionPane#createDialog() to create the
actual dialog.

Walter

----
Walter Gildersleeve
Freiburg, Germany

"In science, 'fact' can only mean 'confirmed to such a degree that it
would be perverse to withhold provisional assent.' I suppose that
apples might start to rise tomorrow, but the possibility does not merit
equal time in physics classrooms." -- Stephen Jay Gould

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.
longoja@yahoo.com - 14 Feb 2006 21:06 GMT
I have created an instance of JOptionPane and used createDialog.
However, I still don't see a way to manipulate the hot-keys.
NullBock - 15 Feb 2006 05:58 GMT
Okay, so it's a bit more complex than all that...I underestimated the
work involved.  It is still probably easier than creating your own
dialog, since you'd have to do most of the following anyhow.  Here's an
example using a JOptionPane, where the "Yes" button is clicked when the
user presses 'y'.

 public static void showConfirm() {
   final JButton[] options = {
       new JButton("Yes"), new JButton("No"), new JButton("Cancel")
   };

   options[0].setActionCommand("yes");
   options[1].setActionCommand("no");
   options[2].setActionCommand("cancel");

   final JOptionPane pane = new JOptionPane("Confirm?",
     JOptionPane.QUESTION_MESSAGE, 0, null, options);

   ActionListener al = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
       pane.setValue(e.getActionCommand());
     }
   };

   options[0].addActionListener(al);
   options[1].addActionListener(al);
   options[2].addActionListener(al);

   Object key = new Object();
   pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
       .put(KeyStroke.getKeyStroke('y'), key);
   pane.getInputMap(JComponent.WHEN_FOCUSED)
       .put(KeyStroke.getKeyStroke('y'), key);
   pane.getInputMap(
       JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
       .put(KeyStroke.getKeyStroke('y'), key);
   pane.getActionMap().put(key, new AbstractAction() {
     public void actionPerformed(ActionEvent e)
     {
       options[0].doClick();
     }
   });

   pane.createDialog(null, "Huh?").show();
 }

There are probably better ways to do this, but this is what I came up
with on-the-fly.

Walter

----
Walter Gildersleeve
Freiburg, Germany

"In science, 'fact' can only mean 'confirmed to such a degree that it
would be perverse to withhold provisional assent.' I suppose that
apples might start to rise tomorrow, but the possibility does not merit
equal time in physics classrooms." -- Stephen Jay Gould

______________________________________________________
http://linkfrog.net
URL Shortening
Free and easy, small and green.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.