how to hide the drop down values based on another drop down values?
In Java Swing:
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DynamicCombobox {
public static void main(final String args[])throws Exception {
JFrame frame = new JFrame();
frame.setLayout(null);
JLabel lab1=new JLabel("Course");
final JComboBox combo1=new JComboBox();
combo1.addItem("--Select--");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
final Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from course");
while(rs.next()){
combo1.addItem(rs.getString("courseName"));
}
final JLabel lab2=new JLabel("Semester");
final JComboBox combo2=new JComboBox();
combo1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
if (combo1.getItemCount() > 0){
combo2.removeAllItems();
}
lab2.setVisible(true);
combo2.setVisible(true);
ItemSelectable is = (ItemSelectable)e.getSource();
String str=selectedString(is);
ResultSet rs1 = st.executeQuery("Select semester from course c,semester s where c.courseName='"+str+"' and c.courseid=s.courseid");
while(rs1.next()){
combo2.addItem(Integer.toString(rs1.getInt("semester")));
}
}
catch(Exception exception){}
}
});
lab1.setBounds(10,10,100,20);
combo1.setBounds(120,10,100,20);
lab2.setBounds(10,40,100,20);
combo2.setBounds(120,40,100,20);
lab2.setVisible(false);
combo2.setVisible(false);
frame.add(lab1);
frame.add(combo1);
frame.add(lab2);
frame.add(combo2);
frame.setSize(300,150);
frame.setVisible(true);
}
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
}
In HTML and JavaScript:
<html>
<h2>ComboBox</h2>
<script language="javascript">
var arr = new Array();
arr[0] = new Array("-select-");
arr[1] = new Array("Maharashtra","Karnataka","Kerela","Rajashthan");
arr[2] = new Array("Texas","New York","Florida","California");
function change(combo1)
{
document.getElementById("sel").style.visibility="visible";
var comboValue = combo1.value;
document.forms["form"].elements["combo2"].options.length=0;
for (var i=0;i<arr[comboValue].length;i++)
{
var option = document.createElement("option");
option.setAttribute('value',i+1);
option.innerHTML = arr[comboValue][i];
document.forms["form"].elements["combo2"].appendChild(option);
}
}
</script>
<form name="form" method="post">
<select name="combo1" onchange="change(this);">
<option value="0">-Select-</option>
<option value="1">India</option>
<option value="2">USA</option>
</option>
</select><br />
<select id="sel" name="combo2" style="visibility:hidden;">
</select>
</form>
</html>
For java swing, we have used following database tables:
CREATE TABLE `course` (
`courseid` bigint(255) NOT NULL auto_increment,
`courseName` varchar(255) default NULL,
PRIMARY KEY (`courseid`)
)
CREATE TABLE `semester` (
`semesterid` bigint(255) NOT NULL auto_increment,
`courseid` int(255) default NULL,
`semester` int(255) default NULL,
PRIMARY KEY (`semesterid`)
)
In jsp and Ajax:
1)country.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script language="javascript" type="text/javascript">
var xmlHttp
var xmlHttp
function showState(str){
document.getElementById("st").style.visibility="visible";
if (typeof XMLHttpRequest != "undefined"){
xmlHttp= new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp==null){
alert("Browser does not support XMLHTTP Request")
return;
}
var url="state.jsp";
url +="?count=" +str;
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("state").innerHTML=xmlHttp.responseText
}
}
</script>
</head>
<body>
<select name='country' onchange="showState(this.value)">
<option value="none">Select</option>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from country");
while(rs.next()){
%>
<option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
<br>
<div id='state'>
<select name='state'id='st' style="visibility:hidden" >
<option value='-1'></option>
</select>
</div>
</body>
</html>
2)state.jsp:
<%@page import="java.sql.*"%>
<%
String country=request.getParameter("count");
String buffer="<select name='state' ><option value='-1'>Select</option>";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from state where countryid='"+country+"' ");
while(rs.next()){
buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>";
}
buffer=buffer+"</select>";
response.getWriter().println(buffer);
}
catch(Exception e){
System.out.println(e);
}
%>
For the above code, we have created two database tables:
CREATE TABLE `country` (
`countryid` bigint(255) NOT NULL auto_increment,
`countryname` varchar(255) default NULL,
PRIMARY KEY (`countryid`));
CREATE TABLE `state` (
`stateid` bigint(255) NOT NULL auto_increment,
`countryid` int(255) default NULL,
`state` varchar(255) default NULL,
PRIMARY KEY (`stateid`));