Popup Windows Javaserver faces.

I wanted to build a popup window that I could use to bring up a print dialog in a web page. It’s no use using the JAVA print dialog because it would appear on the web server.

So:-

FORM WITH DATA —> CLICK PRINT —> Bring up Popup Print Window —> CLOSE WINDOW –> RETURN TO FORM

The Popup JSF page

1. Create the Popup window java server faces page. (just a normal jsf page)
2. Add a Button to the page to close it
3. Add a static text box, rename it to txtJAVASCRIPT, set the escape checkbox to off, add a binding attribute
4. Now, on the Popup I have a close button and the code in the close button simply sets the static text box’s javascript:

public String cmdClOSE_action() {
getSessionBean1().sPrintReportRequest="";
        txtJAVASCRIPT.setText("<script language='javascript'>window.close();</script>");
        txtJAVASCRIPT.setVisible(true); 
        return null;
}

The calling JSF page

1. Create your page that you want to call the popup box.
2. Add the popup code into the JSF for the page calling the popup (I put it into a page fragement that appears on every page):-

<script LANGUAGE="JavaScript">
       
            function popUp(URL) {
            day = new Date();
            id = day.getTime();
            eval("page" + id + " = window.open(URL, '" + id + "',
'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=382,height=286,left = 529,top = 307');");
            }
       
        </script>

Ofcourse you can change the settings as you wish (i.e. popup window size)

3. Making the popup appear.

a. Add a static text box, rename it to txtJAVASCRIPT, set the escape checkbox to off, add a binding attribute (this stores the javascript when i want the box to appear).

b. Add a button to the form you want the popup to appear on, in the button java put :-

  public String cmdPRINT_action() {
      // in this case i am printing a report so i set the session variable to tell the server the popup needs to be displayed
         getSessionBean1().sPrintReportRequest="rptzones";
// now set the txt of the static text box I set up on the same page, note its normally not visible or rendered
// in this case the popup is a jsf page called frmPRINTREPORTS.jsp
         txtJAVASCRIPT.setRendered(true);
         txtJAVASCRIPT.setText("<script language='javascript'>popUp('frmPRINTREPORTS.jsp');</script>" );
        txtJAVASCRIPT.setVisible(true);
       
       
        return null;
    }
 

 

4. Lastly in the form that called the popup I want to stop it from appearing so I have to kill the javascript.

    public void preprocess() {
       
        String sReportRequest=getSessionBean1().sPrintReportRequest;
        if (sReportRequest.equals("")) {
            txtJAVASCRIPT.setVisible(false);
            txtJAVASCRIPT.setText("");
           
        }
       
       
   

Note that the code has to run in the preprocess stage of the page, this simply makes sure that the javascript won’t run when it’s not wanted.

There you go!

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on July 17, 2008, in Howto and tagged , , , . Bookmark the permalink. 2 Comments.

  1. Really helpful artical thank you greatly, I’ve been tried to solve this problem during two days, and no it’s solved in a minute.)) Thanks and regards))

  1. Pingback: Bookmarks about Popup

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: