Urgent!ActionListener not working...

i have a assign due next monday and i was wondering if anyone could help me with my action listener i don't understand why my action listener is not working

here are the example of codes for my action listener:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

class buttonPanel extends Panel implements ActionListener{

private static final int BUTTON_WIDTH = 80;

private static final int BUTTON_HEIGHT = 30;

private Button saveButton,resetButton,cancelButton;

private TextField tf0,tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9;

public buttonPanel () {

saveButton = new Button("SAVE");

saveButton.setBounds(380,20, BUTTON_WIDTH, BUTTON_HEIGHT);

saveButton.addActionListener(this);

add(saveButton);

resetButton = new Button("RESET");

resetButton.setBounds(480,20, BUTTON_WIDTH, BUTTON_HEIGHT);

resetButton.addActionListener(this);

add(resetButton);

cancelButton = new Button("CANCEL");

cancelButton.setBounds(580,20, BUTTON_WIDTH, BUTTON_HEIGHT);

cancelButton.addActionListener(this);

add(cancelButton);

}

//The implementation of ActionListener

public void actionPerformed (ActionEvent e){

if(e.getActionCommand().equals("saveButton")){

Jdbc j = new Jdbc();

j.Open("PP", "", "");

j.Update("insert into HarvestFile (TreeID, SegmentCode, DateOfHarvest, NumberOfTrees, ForestOfficer)" +

"values" + tf0.getText() + "','" + tf1.getText() + "','" + tf2.getText() + "','" +

tf3.getText() + "','" + tf4.getText() + "');");

j.Clear();

j.Close();

}

else if(e.getActionCommand().equals("cancelButton")){

HRecordFrame harvest = MainFrame.HRecord;

harvest.show();

HRecordPanel.haf.dispose();

}

else {

tf0.setText("");

tf1.setText("");

tf2.setText("");

tf3.setText("");

tf4.setText("");

tf5.setText("");

tf6.setText("");

tf7.setText("");

tf8.setText("");

tf9.setText("");

}

}

}

Thanks is advance!

harvin

[2233 byte] By [harvin] at [2007-9-26 2:09:35]
# 1
hi,this is pretty clear, isn't it? The action listener takes the action command string to decide which action to perform but there aren't any action commands assigned to the buttons.PS: use method setActionCommand(String) in class JButtonbest regards, Michael
Michael_Rudolf at 2007-6-29 8:59:27 > top of Java-index,Archived Forums,Java Programming...
# 2
> if(e.getActionCommand().equals("saveButton")){should be:if(e.getActionCommand().equals("SAVE")){> else if(e.getActionCommand().equals("cancelButton")){should be:else if(e.getActionCommand().equals("CANCEL")){
parthasarkar at 2007-6-29 8:59:27 > top of Java-index,Archived Forums,Java Programming...
# 3

If I am not mistaken, getActionCommand will return the text of the button - in your case "SAVE", "RESET", "CANCEL"

confirm this by putting a debug statement -System.out.println(e.getActionCommand()) in the first line of actionPerformed just to make sure.

A better way is to use get e.getSource() if you want to use the names of the objects as you have them now

eg

if (e.getSouce() == saveButton)

(cannot use "equals" if you use getSource

shirish_wagh at 2007-6-29 8:59:27 > top of Java-index,Archived Forums,Java Programming...
# 4

Hello,

Thanks for your repaly thus far, i've change my coding according to e.getSource()== saveButton for all the actionlisteners but my problem is i'm always getting null pointer exception for the action listener. i really don't understand what this is because as far as i know the coding looks right to me. but what i do know is that the error comes form the actionlistener part because i've tried debugging it using commands....

Thanks again

harvin

harvin at 2007-6-29 8:59:27 > top of Java-index,Archived Forums,Java Programming...