Here is a mini-tutorial for creating a Popup window for the user to lookup
values. One scenario that you might use this for is when the page visitor needs
more information then can be displayed in a drop-down list.
Note: The original post of this blog entry had code that did not work in Internet Explorer. Escritorioooo posted a solution to the forum and I updated the code using Escritorioooo's solution. Thanks Escritorioooo!
- Create a project or open an existing one.
- Add a page named
Popup.
- Add a Table component to the page.
- Drag VIR > Tables > State from the Servers window and drop it on the
page.
- Right-click the Table component and choose Table Layout from the pop-up
menu.
- In the column for the STATE.STATEID database field, set the Component Type
to Hyperlink, and click OK.
- Select the Hyperlink component so that its properties appear in the Properties
window.
- Set the value for the
onClick property to doSave('#{currentRow.value['STATE.STATEID']}')
(I got the value to pass to doSave() from the Value Expression in the Table
Layout dialog).
- Click JSP in the editing toolbar to view the JavaServer Pages script.
- Add the following code shown in bold just under the opening tag for
ui:body:
| |
<ui:body binding="#{Popup.body1}" id="body1" style="-rave-layout: grid">
<script><![CDATA[
function doSave(val) {
window.opener.setVal(val);
window.close();
}]]></script>
|
The Popup page is now ready for use by another page.
- Create a new page called
Form1. Make it be the start page.
- Drop a Hyperlink component on the page and set the
text property
to State Code.
- Set the
url property to /faces/Popup.jsp.
- Set the
onClick property to doPopup("form1:textField1")
Note: Here you are passing the id of the field to fill with the selected value.
Determining the id can be tricky. For example, if you set the label property
of the Text Field component, the id changes to form1:textField1_field.
To determine what to use for the id, right-click in the page and choose View
in Browser. When the page displays in the browser, choose View Source from
the appropriate browser menu and look for the id setting for the component's
<input> tag.
- Click the ... button for the target property to open the target property
editor.
- In the property editor, click New and set both the Display and Value to
popup. Click in some field and check that both values stuck.
Then click OK.
Later you add script to open a window named popup, and this setting causes
the Popup.jsp page to display in that window.
- If the
target property value is still blank, select popup from
the combobox for that property.
- Drag a Text Field component and drop it under the Hyperlink component. By
default, the component's id should be textField1. If not, change the id passed
to doPopup() in step 14 to match. Use the View in Browser trick mentioned
in Step 14 to determine the value to pass.
- Click JSP in the editing toolbar to view the JavaServer Pages script.
- Add the following code shown in bold just under the opening tag for
ui:body:
| |
<ui:body binding="#{Popup.body1}" id="body1" style="-rave-layout: grid">
<script><![CDATA[
function doPopup(destination) {
popup = window.open("", "popup",
"height=300,width=200,toolbar=no,menubar=no,scrollbars=yes");
destinationElement=document.getElementById(destination);
popup.focus();
}
function setVal(val){
destinationElement.value=val;
}]]></script>
|
This script adds an instance field name openerDestinationElement
to the popup window. The script for the Popup page uses openerDestinationElement
to get to the Text Field component on the Form1 page.
- Run the project. Click State Code to open the popup window. Click a state
code to select the code, close the popup window, and populate the text field.
Posted by willy on August 30, 2006 at 06:21 PM PDT #
Posted by Marcos Rojo on September 06, 2006 at 04:02 AM PDT #
Posted by diva#2 on September 07, 2006 at 01:17 PM PDT #
Posted by diva#2 on September 18, 2006 at 08:55 AM PDT #
Posted by Pierre on September 24, 2006 at 01:21 AM PDT #
Posted by diva#2 on September 24, 2006 at 11:14 AM PDT #
Posted by mickgiu on September 27, 2006 at 07:09 AM PDT #
Posted by diva#2 on September 27, 2006 at 09:42 AM PDT #
Posted by mickgiu on September 28, 2006 at 11:39 PM PDT #
Posted by mickgiu on October 09, 2006 at 08:25 AM PDT #
Posted by diva#2 on October 09, 2006 at 08:33 AM PDT #
Posted by diva#2 on October 09, 2006 at 08:36 AM PDT #
Posted by mickgiu on October 09, 2006 at 09:28 AM PDT #
Posted by Hiten on October 31, 2006 at 03:12 PM PST #
Posted by 59.92.48.122 on November 15, 2006 at 08:27 PM PST #
Hi Divas! Thanks for your examples !
I have a question : I can't pass parameter to popup window using SessionBean ???
Thanks
Posted by Nghialt on October 09, 2007 at 03:00 AM PDT #
Hello Nghialt,
I am not a JavaScript expert so I am not a good person to ask that question. What I do know is that the JavaScript is exercised on the client side. That is, when the pop-up appears, it is done without sending anything to the server and without exercising any server-side code, such as code to save a value to a session bean. You might want to send this question to a JavaScript/JSF mailing list.
Posted by Diva #2 on October 09, 2007 at 10:17 AM PDT #
Thanks for reply. nghialt
Posted by Nghialt on October 09, 2007 at 06:52 PM PDT #
Nghialt,
I just remembered that you can use Ajax or Dynafaces to store the value in the session bean without submitting the form. We did a hands on lab about using Ajax in Visual Web apps at JavaOne 2006 -- http://developers.sun.com/learning/javaoneonline/j1lab.jsp?lab=LAB-5655&yr=2006&track=8. There is a tutorial about Dynafaces at http://nbstaging.czech/kb/55/vwp-instcurrencytrader.html.
Posted by Diva #2 on October 10, 2007 at 09:24 AM PDT #
Hi Mr.Divas,
Sir, i wish to dynamically make url which is a query string, with the selected submit values from multiple select list box without any involvement of java action listener interference. Like http:// ...jsp/?key=value&a=1&b=2... , where 1, 2 ...are all the values selected from multi select box. Can i do this ? if so how ? If I cannot its fine! any help on this issue is extreamely useful and I ove my humble gratitude all those who can add a bead to this thread!
Posted by shambhu on October 29, 2007 at 12:41 PM PDT #
Hello Shambhu,
I am not understanding what you mean by "without any involvement of java action listener interference". Do you mean that you do not want the user to have to submit the form (and thus you cannot do any server side processing)?
We, the tutorial divas, can help with JavaServer Faces and Visual Web questions, but we do not know very much about JavaScript. I learned jsut enough JavaScript to figure out how to make this popup work, but no more. If you want a JavaScript answer, then I am sorry we cannot help you. I suggest that you go to a JavaScript forum with this question.
You might want to investigate dynamic faces. See http://www.netbeans.org/kb/55/vwp-instcurrencytrader.html.
Another option is to use a jMaki widget. See http://blogs.sun.com/divas/entry/using_jmaki_9_3_with and http://www.jmaki.com/.
Posted by Diva #2 on October 29, 2007 at 04:25 PM PDT #
Hi
this example dont work in netbeans 6
please help me .
Posted by saeed on January 01, 2008 at 03:12 AM PST #
Posted by Insider Scoop From the Tutorial Divas on January 03, 2008 at 12:09 PM PST #
Saeed,
I just posted a NetBeans IDE version. See http://blogs.sun.com/divas/entry/adding_a_popup_window_to1
Posted by Diva #2 on January 03, 2008 at 12:16 PM PST #
Dear Diva:
I am learning some tutorials from your blog, but I don't have a State Table under the VIR database in my Glassfish Server. Where can I find or download the State table please?
Best regards,
Wai-chi
Posted by Wai-chi Lau on January 07, 2008 at 08:48 AM PST #
Hello,
The VIR database is includes with Sun Java Studio Creator, and with the Web and Java EE NetBeans IDE 6.0 or the Full NetBeans IDE 6.0.
If you don't have either of those versions, here are some suggestions:
To quickly try out this tutorial, use any database table and replace STATE.STATEID with the table name and column name.
Or, you can add a table to an existing database. Call the table STATE and add a STATEID column and a STATENAME column.
Posted by Diva #2 on January 07, 2008 at 09:26 AM PST #