1 2 Previous Next 15 Replies Latest reply: Aug 20, 2008 12:19 AM by 807575 RSS

    Poping Up a Confirmation Dialog

    807575
      I would like to popup a confirmation dialog when a user clicks a 'cancel' button in my page. If the user clicks 'NO' in the dialog, data is not cleared and navigation is canceled.

      I tried this ...
       public String cancelBtn_action() {
              
              int i = JOptionPane.showConfirmDialog
                          (null, "Do you really want to clear all data and start over?",
                              "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
              if(i == JOptionPane.YES_OPTION) {
                  getSessionBean1().clearBeans();
                  return "clear";
              }
              else return "null";
              
          }
      ... but the dialog is hidden behind all the open windows on my machine (b/c the 'null' argument in showConfrimDialog, I guess).

      Can I attach this dialog to the page somehow (so it does not get hidden)? Should I even use Swing? Is there a prefered way to use popup dialogs in JSF/Creator?

      Any information would be much appreciated.
        • 1. Re: Poping Up a Confirmation Dialog
          807575
          Hi

          Since you are not modifying any data based on the response received from confirmation dailog, I would suggest to use javascript based dialogs.

          You can also use another jsp page in a popup as confirmation dialog by specifying the size of the new page.

          Thanks
          Creator Team
          • 2. Re: Poping Up a Confirmation Dialog
            807575
            Thank you for your reply.

            Pardon any redundancy, but to be sure that I am making myself clear and that I am understanding your response...

            At first 'cancel_action()' ( event handler in the page bean) looked like this:
             public String cancelBtn_action() {
                   getSessionBean1().clearBeans();
                   return "cancel";
            }
            To make this code conditional I next tried this:
            public String cancelBtn_action() {
                    
                    int i = JOptionPane.showConfirmDialog
                                (null, "Do you really want to clear all data and start over?",
                                    "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                    if(i == JOptionPane.YES_OPTION) {
                        getSessionBean1().clearBeans();
                        return "cancel";
                    }
                    else return "null";
                    
            }
            This code works 'fine', except that the dialog gets displayed behind all open windows where the user can not see it. (If there is a way to tie the dialog to the browser frame so it does not get buried (maybe there is?), this would allow all the JOptionPane stuff to be leveraged nicely with JSF).
            Since you are not modifying any data based on the
            response received from confirmation dailog
            clearBeans() clears data previously entered in several session-scoped beans (so data is (conditionaly) modifed). "cancel" and "null" are different navigation cases
            I would suggest to use javascript based dialogs.
            I like the simplicity of using javascript, but I need to set a flag (or something similar) in my page bean to conditionaly control navigation and whether to clear data stored in the session beans. This seems to require that the javascript calls a method in the page bean. Can this be done with JSF?

            >
            You can also use another jsp page in a popup as
            confirmation dialog by specifying the size of the new
            page.
            So I guess you mean a non-JSF JSP page? Since I need to finish executing the code in the event handler that was interupted by the confirmation dialog, I think that JSF navigation/lifecycle issues would be a problem. Am I correct?

            Again pardon any redundancy.

            More details on either (or any other) approach would be helpful.
            • 3. Re: Poping Up a Confirmation Dialog
              807575
              I have tried to solve this using hidden fields and javascript dialogs.

              Here is my jsp code:
              <h:inputHidden binding="#{PresenterInfo.confirmHF}" id="confirmHF" value="no"/>
              <h:commandButton action="#{PresenterInfo.cancelBtn_action}" binding="#{PresenterInfo.cancelBtn}" id="cancelBtn" immediate="true"
                                      onclick="confirmClear()" style="left: 216px; top: 672px; position: absolute" tabindex="12" title="Clear All Data and Start Over" value="Start Over"/>
              Here is my javascript code:
              function confirmClear() {
              
                   alert('in confirmClear');
                       var yon = window.confirm("Clear all data and start over?")
                   alert('Currently value is: ' + document.forms[0].elements['form1:confirmHF'].value);
                       if(yon) {
                     alert('Should say yes');
                      document.forms[0].elements['form1:confirmHF'].value='yes';
                     alert(document.forms[0].elements['form1:confirmHF'].value);
                       }
                       else {
                     alert('Should say no');
                      document.forms[0].elements['form1:confirmHF'].value='no';
                     alert(document.forms[0].elements['form1:confirmHF'].value);                        
                       }
              
              }
              Here is my new Java page bean event handler:
              public String cancelBtn_action() {
                      
                      String s = (getConfirmHF().getValue()).toString();
              
                      if (s.equals("yes")) {
                          getSessionBean1().clearBeans();
                          return "cancel";
                      }
                      else return "null";
              
                  }
              This does not work . The 'alerts' in the js show that the hidden field's value is correctly set by the js dialog. However, in cancelBtn_action(),
              getConfirmHF().getValue()).toString() evaluates to 'no' the default set in the jsp tag.

              It appears that the JSF component (confirmHF) associated with the html hidden field are out ot synch.

              Any ideas about this or another approach to this confirmation dialog problem would be appreciated.
              • 4. Re: Poping Up a Confirmation Dialog
                807575
                An easy way to add a confirmation dialog to a button in the IDE is as follows:

                a. Add the following code to the JavaScript > onClick property of the Button component:

                return confirm('Are You Sure You Want To Delete?')

                b. If the user clicks Yes, the action handler is called. If the user clicks No, the page is not even submitted. It is as though the button had never been clicked.

                HTH, jim
                • 5. Re: Poping Up a Confirmation Dialog
                  807575
                  Thanks Jim, I'll try it!
                  • 6. Re: Poping Up a Confirmation Dialog
                    807575
                    Do not work for me, the action is not called.

                    Ciao.
                    • 7. Re: Poping Up a Confirmation Dialog
                      807575
                      I had exactly the same problem, but for me the javascript solution works fine (Thanks JIM !!!).

                      fbiaggi, maybe you forget the ';' at the end of the javasript ....
                      • 8. Re: Poping Up a Confirmation Dialog
                        807575
                        Hi,
                        maybe, I tried on an ImageHiperlink and do not work, the action is not called.Thanks.
                        • 9. Re: Poping Up a Confirmation Dialog
                          807575
                          You can try using the below example to add confirm prompt to wrap around the original onclick function.
                          <html>
                          <head>
                          <script>
                          function addConfirmPrompt(c,msg){
                          
                           var tempFunc = c.onclick;
                           var newFunc = function(){ if(confirm(msg)) return tempFunc(); }
                           c.onclick = newFunc;
                          
                          }
                          
                          function init(){
                              addConfirmPrompt(document.getElementById('c'),'Are you sure you want to delete this item?'); 
                          }
                          
                          </script>
                          <head>
                          
                          <body onload="init();">
                          
                             <a id="c" href="#" onclick="alert('ok1'); alert('ok2');">click here</a>
                          
                          </body>
                          </html>
                          • 10. Re: Poping Up a Confirmation Dialog
                            807575
                            Oops post wrongly.
                            • 11. Re: Poping Up a Confirmation Dialog
                              807575
                              Same here... I could not call action handler when I used image hyperlink. After I changed to button, it works file. Thanks JimBisso.

                              By the way, it is possible to change title of the popup windows?
                              if so, can anyone show me how to do that?
                              Thanks.
                              • 12. Re: Poping Up a Confirmation Dialog
                                807575
                                This works for me.
                                Thanks very much
                                        HtmlCommandButton l = (HtmlCommandButton)app.createComponent(
                                                                              HtmlCommandButton.COMPONENT_TYPE);
                                        l.setValue("Cancel");
                                        l.setImage("/images/trash.gif");
                                        l.setOnclick("return confirm('Are You Sure You Want To Delete?');");
                                • 13. Re: Poping Up a Confirmation Dialog
                                  807575
                                  Try this:
                                  onClick="if (!confirm('Are you sure you want to delete the opportunity?')) {return false;}"
                                  This works for me.

                                  Haroon
                                  • 14. Re: Poping Up a Confirmation Dialog
                                    807575
                                    I got it!

                                    My problem is I�m trying to give some feedback to the user. I have a delete button that can erase either zero, one or several records, thus the script didn�t really work for me.

                                    Here is how I made the pop up dialog to appear over the browser�s window
                                    int i = JOptionPane.showConfirmDialog
                                                        (new javax.swing.JOptionPane(), "You are about to delete "+ (String) qty +" record(s)\nProceed?",
                                                            "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                                    As gmcauley says, the problem appears to be the null argument in the function call.

                                    Please reply if someone believe this approach could cause problems.
                                    1 2 Previous Next