Hi Eugene,

To catch Enter and Escape keys on the Alert you can do the following to
extend UIAlert class:

        protected void prepareAlertDialog(final JDialog arg0) {
            KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
0);
            arg0.getRootPane().registerKeyboardAction(new ActionListener()
{
                public void actionPerformed(java.awt.event.ActionEvent e)
{
                    System.out.println("Enter pressed");
                }}, enter, JComponent.WHEN_IN_FOCUSED_WINDOW);

            
            KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
            arg0.getRootPane().registerKeyboardAction(new ActionListener()
{
                public void actionPerformed(java.awt.event.ActionEvent e)
{
                    System.out.println("Esc pressed");
                }}, esc, JComponent.WHEN_IN_FOCUSED_WINDOW);
        }


What action do you want to take on Enter key? If you would like to mean
the Enter key press == click of Ok button then then you can do something
like this:

        protected void prepareAlertDialog(final JDialog arg0) {
            JOptionPane optionPane =
(JOptionPane)arg0.getContentPane().getComponent(0);
            
            JPanel panel = (JPanel)optionPane.getComponent(1);
            JButton button = (JButton)panel.getComponent(0);
            
            if (button.getText().equals("Ok")) {
                arg0.getRootPane().setDefaultButton(button);
            }            
        } 


BTW, for faster and guaranteed response to your posts please consider
subscribing to ULC Premium Support.

Thanks and regards,

Janak

-----------------------------------------
Janak Mulani

email: [email protected]
url: http://www.canoo.com

Beyond AJAX - Java Rich Internet Applications 

http://www.canoo.com/ulc
-----------------------------------------  

> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of 
> Eugene Coelho
> Sent: Friday, February 06, 2009 1:32 PM
> To: [email protected]
> Subject: [ULC-developer] Adding a key listener to ULCAlert
> 
> Hi,
> 
> I have a scenario where i need to handle user pressing enter 
> key when a 
> ULCAlert alert is shown.
> 
> I tried the following ....  but it didnt work .. any 
> suggestions to make 
> it work ? 
> 
> ----
> 
> Client Part
> 
> 
> package com.xxxx.xxx.extension.client;
> 
> import java.awt.event.KeyEvent;
> import java.awt.event.KeyListener;
> 
> import javax.swing.JDialog;
> import javax.swing.JOptionPane;
> 
> import com.ulcjava.base.client.UIAlert;
> 
> public class UICustomAlert extends UIAlert {
> 
>   
>     public void performAction() {
>         this.fireWindowClosingULC();
>     }
>    
>     protected void postInitializeState() {
>         super.postInitializeState();
>     }
> 
>     private class HandleEnterKeyListener implements  KeyListener {
> 
>         public void keyPressed(KeyEvent e) {
>             int code = e.getKeyCode();
>             if(code == KeyEvent.VK_ESCAPE){
>             // Key pressed is the ESCAPE key. Hide this Dialog.
>                 
>             }
>             else if(code == KeyEvent.VK_ENTER){
>             //    Key pressed is the ENTER key.
>             //    Redefine performEnterAction() in subclasses 
> to respond 
> to depressing the ENTER key.
>                 performAction();
>                 
>             }
>         }
> 
>         public void keyReleased(KeyEvent arg0) {
>             
>         }
> 
>         public void keyTyped(KeyEvent arg0) {
>             
>         }
>         
>     }
> 
>     protected void prepareAlertDialog(JDialog arg0) {
>         JOptionPane optionPane =  
> (JOptionPane)arg0.getContentPane().getComponent(0);
>         KeyListener[] keyListener = optionPane.getKeyListeners();
>         for (int i=0;i<keyListener.length;i++) {
>             optionPane.removeKeyListener(keyListener[i]);
>         }
>         optionPane.addKeyListener(new HandleEnterKeyListener());
>     }
> 
> }
> 
> ----
> 
> Server Part
> 
> package com.xxxx.xxx.extension.server;
> 
> import com.ulcjava.base.application.ULCAlert;
> import com.ulcjava.base.application.ULCRootPane;
> 
> public class ULCCustomAlert extends ULCAlert {
> 
>     private static final long serialVersionUID = 1L;
> 
>     public ULCCustomAlert(ULCRootPane rootPane, String 
> loginfailed_title, String login_failed_message, String button_retry, 
> String button_quit) {
>         super(rootPane, loginfailed_title, login_failed_message, 
> button_retry, button_quit);
>     }
> 
>     protected String typeString() {
>         return "com.isfsdc.wwm.extension.client.UICustomAlert"; 
>     }
> 
>    
> 
> }
> 
>  
> _______________________________________________
> ULC-developer mailing list
> [email protected]
> http://lists.canoo.com/mailman/listinfo/ulc-developer
> 
_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to