File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Combobox Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Combobox Problem" Watch "Combobox Problem" New topic

Combobox Problem

Ben Reeves

Joined: Jan 23, 2006
Posts: 5
I have looked through some examples of trying to populate one combobox depending on another but im still not sure. Im using struts,jsp,java for my project. I have the first combobox populated but now i have to populate the second combobox depending on what i have selected in the first one. I am taking the fields from a database. The following is the jsp code i have, Can someone please help me?

<TD>Origin: <html:select property="origin">
<% ArrayList list = new ArrayList();
DatabaseOperations databaseOperations = new DatabaseOperations();
list = databaseOperations.getOrigins(); %>
for(int i=0; i < list.size(); i++)
<html ption value='<%=list.get(i).toString()%>'>
</html ption>
<% } %>
<TD>Destination: <html:select property="destination">

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are three ways that I know of to handle the problem of the contents of combo box B being dependent on what the user selects in combo box A:

1- If there are only a few different sets of possible options for combo box B, store each set of options as a javaScript array. Then, when the user makes a selection on combo box A, use the onChange method to call a javaScript function to swap out the options.

2- If there are a lot of different sets of possible options, or if the lists are long, Use a server-side approach: Use the onChange event from combo box A to submit the form. The Action class then reads the value from Combo box a and prepares the list of options for combo box B. It then redisplays the page with the focus on combo box B.

3- Use AJAX to make a server-side call to get the options and refresh them without refreshing the entire page. You'll have to download one of the AJAX open source projects (Such as DWR) and study up on AJAX in order to use this option.
[ February 02, 2006: Message edited by: Merrill Higginson ]

Consultant, Sima Solutions
I agree. Here's the link:
subject: Combobox Problem
It's not a secret anymore!