Ok ive been looking around and i have having the same problem as this fellow<Removed> im trying to use the same action listener for the buttons and the combo boxes it doesnt seem to be working. check out some code, and thanks for your help
Code:
public void actionPerformed(ActionEvent event)
{
JComboBox yearcombo = (JComboBox)event.getSource(); //i get an error here
JComboBox gendercombo = (JComboBox)event.getSource();
JComboBox statecombo = (JComboBox)event.getSource();
if (event.getSource() == clear)
{
for( int i=0; i<firsta.size(); i++)
{
System.out.println("First Name: "+firsta.get(i));
System.out.println("Last Name: "+lasta.get(i));
System.out.println("Age: "+agea.get(i));
System.out.println("Street Address: "+streeta.get(i));
System.out.println("City Name: "+citya.get(i));
// System.out.println("State Name: "+statea.get(i));
// System.out.println("Year: "+yeara.get(i));
//System.out.println("Gender: "+gendera.get(i));
}
clearall = true;
if (clearall = true)
{
street.setText(null);
first.setText(null);
last.setText(null);
age.setText(null);
city.setText(null);
// gender.setText(null);
clearall=false;
}
}
if (event.getSource() == Bok)
{
//String g = gendercombo.getSelectedItem();
//gendera.add(i,genderselect);
// String y= yearcombo.getSelectedItem();
// yeara.add(i,yearselect);
// String s= statecombo.getSelectedItem();
// statea.add(i,stateselect);
try{
// Create file
FileWriter fstream = new FileWriter("database.txt");
BufferedWriter out = new BufferedWriter(fstream);
for( int i=0; i<firsta.size(); i++)
{
out.write("First Name: "+firsta.get(i));
out.newLine();
out.write("Last Name: "+lasta.get(i));
out.newLine();
out.write("Age: "+agea.get(i));
out.newLine();
out.write("Street Address: "+streeta.get(i));
out.newLine();
out.write("City Name: "+citya.get(i));
out.newLine();
out.newLine();
out.write("State Name: "+statea.get(i));
out.write("Year: "+yeara.get(i));
out.write("Gender: "+gendera.get(i));
}
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.out.println("Error: " + e.getMessage());
}
//System.out.println(i);
//System.out.println(firsta.get(i));
clearall = false;
}
if (event.getSource() == Bcc)
{
f1.dispose();
}
if (event.getSource() == submit)
{
// Get the new item
String yearselect = (String)yearcombo.getSelectedItem();
// Get the new item
String genderselect = (String)gendercombo.getSelectedItem();
// Get the new item
String stateselect = (String)statecombo.getSelectedItem();
firststring = first.getText();
firsta.add(i,firststring);
laststring = last.getText();
lasta.add(i,laststring);
try{
agestring = age.getText();
ageint = Integer.parseInt(agestring);
}catch (Exception e){//Catch exception if any
System.out.println("Error: Numbers only....");
}
agea.add(i,ageint);
streetstring = street.getText();
streeta.add(i,streetstring);
citystring = city.getText();
citya.add(i,citystring);
yeara.add(i,yearselect);
gendera.add(i,genderselect);
statea.add(i,stateselect);
i++;
street.setText(null);
first.setText(null);
last.setText(null);
age.setText(null);
city.setText(null);
}
Comment