JComboBox Display Problem

JComboBox Display Problem

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
View Answers

September 9, 2010 at 4:34 PM

Hi Friend,

Try the following code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Compare extends JFrame implements ItemListener {
JComboBox combo, lcombo;

public Compare() {
setLayout(null);
String a[] = { "Select", "Watches", "Mobiles", "shoes" };
combo = new JComboBox(a);
combo.setBounds(50, 50, 100, 20);
combo.addItemListener(this);
add(combo);

lcombo = new JComboBox();
lcombo.addItemListener(this);
lcombo.setEnabled(false);
add(lcombo);
lcombo.setBounds(50, 100, 100, 20);

setSize(300, 300);
}

public void itemStateChanged(ItemEvent e) {
String b[] = { "Titan", "HMT" };
String c[] = { "Nokia", "Sony", "Motorola", "Samsung" };
String d[] = { "Liberty", "Action", "Bata", "Campus", "Relaxo" };

if (e.getSource() == combo) {
if (combo.getSelectedItem().equals("Select")) {
lcombo.setEnabled(false);
} else if (combo.getSelectedItem().equals("Watches")) {
lcombo.setEnabled(true);
lcombo.removeAllItems();
for (int i = 0; i < b.length; i++) {
lcombo.addItem(b[i]);
}
} else if (combo.getSelectedItem().equals("Mobiles")) {
lcombo.setEnabled(true);
lcombo.removeAllItems();
for (int i = 0; i < c.length; i++) {
lcombo.removeItem(c[i]);
lcombo.addItem(c[i]);
}
} else if (combo.getSelectedItem().equals("shoes")) {
lcombo.setEnabled(true);
lcombo.removeAllItems();
for (int i = 0; i < d.length; i++) {
lcombo.addItem(d[i]);
}
}
}
}

public static void main(String args[]) {
(new Compare()).setVisible(true);
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
JComboBox Display Problem - Java Beginners
JComboBox Display Problem  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
problem with addactionlistener on JComboBox - Java Beginners
problem with addactionlistener on JComboBox  ViewElements.java:225...[] = { "A", "B", "C"}; JFrame frame = new JFrame(); JComboBox combo = new JComboBox(st); frame.add(combo); ActionListener actionListener = new
Advertisements
Display Problem
Display Problem  i am creating a small window application , i want to fetch data from ms-access(db) and want to display it on tables. what options are there to show result on table. is CSS helpfull
JComboBox
JComboBox  I have jcombobox. In which tha values are loaded from MySql Database. My problem is that i want to load content of the jtable whenever i change the selected item. Please some one help me to do this. Thank you
jcombobox
jcombobox  hi i have developed a form and i have jcombobox in which data is retrieved from ms access but the problem is that if we pressed the down key the last 5 data are not showed only the other data can be pressed
iterator display problem - Struts
iterator display problem   in action class i store database data in arrraylist but after success how can i display those data in jsp page using... friend, Code to help in solving the problem : Iterator Tag
JComboBox
have jcombobox on Jframe form of IDE. In which the values are to be loaded from MySql Database. My problem is that i want to load content of the jtable whenever i change the selected item in JComboBox. Thank you. (adsbygoogle
Problem to display checkbox item
Problem to display checkbox item  Hi, Following is my code: <...; I want to display records, when i click on submit button or any dumy button... checkboxes.ADS_TO_REPLACE_5 So, from your above code, i can display selected
Jfreechart chart display problem
Jfreechart chart display problem  Using JSP and Jfreechart displays the chart fine on an internal browser in eclipse but doesnt display it on Chrome...(request.getParameter("q")); String query="select dateof,dayinprice,company from stockprice
Integer display problem
Integer display problem  class Bean{ int n1,n2; public Bean(){ } public Bean(int n1, int n2){ this.n1=n1; this.n2=n2; } public...); } } In above program value of n1 should display 10. But it is displaying 8. what
JComboBox
JComboBox  I want to change the index of one jComboBox according to the selected index of another jComboBox. I can do it.Also the user should have the ability to select or change the index of the second combobox according
jComboBox
jComboBox  I want to change the index of one jComboBox according to the selected index of another jComboBox. I can do it.Also the user should have the ability to select or change the index of the second combobox according
Jcombobox
Jcombobox  Hii, I am doing my final year project and i am using java swing as a front end.I have used Jcombobox for displaying my on bluetooth devices and i also made a refresh button.when i click on the refresh button i
Problem with display of images in applets - Applet
Problem with display of images in applets  Hi all, When I run... in figuring out the problem....  Hi frined, import java.applet.*; import..., this); } } --------------------------------------- Display image in Java Applet
select Query result display problem
select Query result display problem  Hi, String SQL_QUERY ="from Cc"; Query query = session.createQuery(SQL_QUERY); for(Iterator it=query.iterate();it.hasNext();){ Object[] row = (Object[]) it.next
Database values in JComboBox
Database values in JComboBox In this section, you will learn how to display values in JComboBox from database. For this, we have allowed the user to enter... JFrame(); f.getContentPane().setLayout(null); final JComboBox combo = new
display records with images problem - JSP-Servlet
display records with images problem  hello, i am developing HR application whereby i want to display employee records and their pictures on web... with a unique id. Also i wrote i jsp that link to servlet in order to display
issue on jcombobox
issue on jcombobox  i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox.only few values in the combobox need this functionality. need
this code will be problem it display the error again send jsp for registration form
this code will be problem it display the error again send jsp for registration form  I AM ENTERING THE DETAILS OFTER IT DISPLAY THE ERROR PLEASE RESEND THE CODE org.apache.jasper.JasperException: java.lang.NumberFormatException
this code will be problem it display the error again send jsp for registration form
this code will be problem it display the error again send jsp for registration form  I AM ENTERING THE DETAILS OFTER IT DISPLAY THE ERROR PLEASE RESEND THE CODE org.apache.jasper.JasperException: java.lang.NumberFormatException
jComboBox with database
jComboBox with database  Hello friends, I have created three JComboBoxes. first two of them get list from string. However I select these two comboboxes, the third combobox automatically retrieve data of a particular column from
JComboBox on JRadioButton - Java Beginners
JComboBox on JRadioButton  How to add JComboBox on Jpanel ,Give Me Sample Code
JComboBox with AccessDatabase - Java Beginners
JComboBox with AccessDatabase  Hello Sir I have Created Course(CourseId,CourseName,Fees,Duration,Type) form then I want To Display Course Details on Student Admission Form when I select Course Name from JComboBox on Student
JComboBox Event Handling - Java Beginners
JComboBox Event Handling  Hi, I have problem with event handling here.There are Two JComboBox and a JTextField.When I select an item in first...,the second combo doesnot show any items in it.please help me resolve this problem
display
display  please tell me how to display the content from database.. if we click on any image using servlets/jsp...please
Changing JLabel with a jcombobox - Java Beginners
get a value from the combo,the result of which i have to display using a JLabel I... listener event of JComboBox put JLabel.setText("Put yourtext here
Help on database and JComboBox
Help on database and JComboBox  I want to select from the JComboBox and when click onto the "search" button, the selected category (example "new york") all the each new york picture and detail will be seen. But how to grab
About jcombobox - Swing AWT
About jcombobox  Hi all, I am new to this forum. Can we do auto suggest jcombobox in swing? If yes is there any jar file for that or any code... JTextField tf; private final JComboBox combo = new JComboBox(); private
Link To Database with JComboBox - Java Beginners
course names from Access Database in to JComboBox but now I want to do ,when i select Course name from Jcombobox i want to display appropriate records relate..."); final JComboBox jc=new JComboBox(); JLabel lbl2 = new JLabel("Duration"); final
Create a JComboBox Component in Java
Create a JComboBox Component in Java       In this section, you will learn about the JComboBox Component of swing in java. The JComboBox is used to display drop-down list
binding jComboBox to mysql database - Swing AWT
binding jComboBox to mysql database  I am using netbeans 6.5 How to populate jComboBox with data of specific column of mysql database table? I...; Hi friend, code to help in solving the problem : import java.awt.
How to store JComboBox item into database
How to store JComboBox item into database   import... DefaultComboBoxModel(labels); final JPanel TypeTF = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setEditable(false
Display Tag
Display Tag  Hi Here is My code, There is problem At line no.3, where i want to display two fields in one column but it gives error.. Please any one knows the solution must reply Thanks In Advance
Display Tag
Display Tag  Hi Here is My code, There is problem At line no.3, where i want to display two fields in one column but it gives error.. Please any one knows the solution must reply Thanks In Advance
navigation between panels when item is selected from jcombobox - Swing AWT
well i have a problem i have one main panel and on that there is one more panel... to approach this problem   Hi friend, For more information on JComboBox in Java visit to : http://www.roseindia.net/java/example/java/swing
easy way to make a JCombobox look like a JTextField?
easy way to make a JCombobox look like a JTextField?  Is there a good(and easy) way to make a JCombobox look like a JTextField? By this I mean there should not be a dropdown button, but when the user enters something it should
How to Add JComboBox on JPanel - Java Beginners
How to Add JComboBox on JPanel  How to Add JComboBox on JPanel D:\java>javac ViewElements.java ViewElements.java:181: expected...); JLabel lbl4=new JLabel("Branch"); final JComboBox jc=new JComboBox(); jc.addItem
problem in coding
problem in coding  i have a following code which display the contents... FileReader("myprogram1.java"); not possible to display the content...); fr.close(); possible to display the content of myprogram.txt
in php installation problem ?
in php installation problem ?   php installation in 7th step did not display y/n dialog box how to solve the problem
problem i coding
problem i coding  i have a problem in coding a combobox containing a number of *.java program on click one of them that program should compile and run and display the result in other window
php problem
php problem  Hello!! I have a problem ,i want to display the data of a single row in my database,this is the code: <?php</p> include 'connectMySql.php'; $username=$row['username']; if(! $conn ) { die('Could
Display image
Display image  How to Pass image from html to jsp and display... the html page to jsp page and display it. 1)page.html: <%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form
JComboBox Insert Edited Value Into Table
JComboBox Insert Edited Value Into Table In this section we will read about how to make JComboBox an editable and then how to insert the new edited value... in to insert an editable value of JComboBox into a table. We will use an Java editor
image display - Java Beginners
image display  i need to display all the images from the folder and i dont want give the image path.dynamically it should display. please help  Hi friend, Code to help in solving the problem var
problem in onlinetest...
problem in onlinetest...  hi, my code getting the problem is ,when user leave a question then the server puts error... pls help me
Add Items in JComboBox from Access Database - Java Beginners
Add Items in JComboBox from Access Database  Heelo Sir I want To add ittem in JComboBox which is stored in Access Database. plz Help Me Sir... records in to my JComboBox.   Hi Friend, Try the following code
how to display?
how to display?  How to write a select query for displaying data where i have author as multivalued attribute
how to display?
how to display?  How to write a select query for displaying data where i have author as multivalued attribute
how to display?
how to display?  How to write a select query for displaying data where i have author as multivalued attribute
Display data
Display data  after successful login how i can fetch my account information from sql database

Ads