Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners JComboBox Display Problem
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Pushpa Sheela
JComboBox Display Problem
1 Answer(s)      4 years and 4 months ago
Posted in : Java Beginners


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

Advertisement
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
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
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
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.   import java.sql.
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 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
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
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... it contains same checkboxes. 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
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
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
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
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
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
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
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
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
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
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.
display
display  please tell me how to display the content from database.. if we click on any image using servlets/jsp...please
 

 

 

DMCA.com