I am create one program that contain two combo boxes. If first combo box i am display all date. If i select one date from first combo box then the second combo box display some dates based on first combo box select date. Here is my full program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.text.*;
import javax.swing.table.*;
class DateView extends JFrame implements ActionListener,ItemListener
{
Container c;
JLabel lblstart,lblend;
JComboBox cmbstart,cmbend;
JButton btnview,btnclose,btnclear;
//JTextField txtpin;
PreparedStatement pst,pst1;
Connection con,con1;
//Statement st,st1,st2;
ResultSet rs,rs1;
ViewEntry f1;
String s,s1,end;
String[] temp,temp1;
int d1,d2,m1,m2,y1,y2;
DateView(ViewEntry f1)
{
c=getContentPane();
c.setLayout(null);
setTitle("Sort by Date");
setBounds(325,325,400,200);
setResizable(true);
Image img = Toolkit.getDefaultToolkit().getImage("logo.jpg");
setIconImage(img);
lblstart=new JLabel("Start Date");
lblend=new JLabel("End Date");
cmbstart=new JComboBox();
cmbend=new JComboBox();
btnview=new JButton("View");
btnclose=new JButton("Close");
btnclear=new JButton("Clear");
lblstart.setBounds(100,30,100,20);
cmbstart.setBounds(200,30,120,20);
lblend.setBounds(100,60,100,20);
cmbend.setBounds(200,60,120,20);
btnview.setBounds(70,100,80,20);
btnclear.setBounds(170,100,80,20);
btnclose.setBounds(270,100,80,20);
c.add(lblstart);
c.add(lblend);
c.add(cmbstart);
c.add(cmbend);
c.add(btnview);
c.add(btnclose);
c.add(btnclear);
btnview.addActionListener(this);
btnclose.addActionListener(this);
btnclear.addActionListener(this);
this.f1=f1;
f1.setEnabled(false);
cmbstart.addItem("Select");
cmbend.addItem("Select");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
close();
}
});
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:pushpa","","");
pst=con.prepareStatement("select DISTINCT acc_date from account where pin_number like ?",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
pst.setString(1,f1.txtstartdate.getText()+"%");
rs=pst.executeQuery();
while(rs.next())
{
//System.out.println(rs.getString(1));
cmbstart.addItem(""+rs.getString(1));
}
}
catch(Exception e)
{
}
cmbstart.addItemListener(this);
cmbend.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
end=cmbstart.getSelectedItem().toString();
temp = end.split("-");
d1=Integer.parseInt(temp[0].trim());
m1=Integer.parseInt(temp[1].trim());
y1=Integer.parseInt(temp[2].trim());
//System.out.println(d1);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con1=DriverManager.getConnection("jdbc:odbc:pushpa","","");
pst1=con1.prepareStatement("select distinct acc_date from account where pin_number like ?");
pst1.setString(1,f1.txtstartdate.getText()+"%");
rs1=pst1.executeQuery();
while(rs1.next())
{
s=s1=rs1.getString(1);
System.out.println(s);
temp1=s.split("-");
d2=Integer.parseInt(temp1[0].trim());
m2=Integer.parseInt(temp1[1].trim());
y2=Integer.parseInt(temp1[2].trim());
System.out.println(d2);
if((d1<=d2)&&(m1<=m2)&&(y1<=y2))
{
System.out.println(s1);
cmbend.addItem(s1);
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void actionPerformed(ActionEvent ae)
{
}
public void close()
{
f1.setEnabled(true);
f1.toFront();
setVisible(false);
dispose();
}
}
The above program run correctly. but the second combo box display date at two times.
That means
1-1-2010
2-1-2010
3-1-2010
4-1-2010
1-1-2010
2-1-2010
3-1-2010
4-1-2010
But i want only one time like this
1-1-2010
2-1-2010
3-1-2010
4-1-2010
Please help me