ActionListener
How do I associate a particular ActionListener Button to an action?
I have the following code which is intended to send a string including text from a field to a server. I would appreciate any help/advice on how to link the pressed button with the action.
publicclass Clientextends Frameimplements ActionListener{
PrintStream ps =null;
PrintWriter pw =null;
BufferedReader bf=null;
Socket s =null;
Button emailButton =new Button("Find Email");//Button for finding email
Button nameButton =new Button("Find Name");//Button for finding staff name
Button numberButton =new Button("Number");//Button for number of members of staff
Button quitButton =new Button("Quit");//Button for quitting
TextField inputData =new TextField();//For communicating arguments
TextField results =new TextField();//Results text field
Label inputDataLabel =new Label("Input data");
Label resultsLabel =new Label("Results");
public Client(){
super("Presentation");
setLayout(new GridLayout(4,2));
add(emailButton);
add(nameButton);
add(numberButton);
add(quitButton);
add(inputDataLabel);
add(inputData);
add(resultsLabel);
add(results);
//Register listeners
numberButton.addActionListener(this);
nameButton.addActionListener(this);
emailButton.addActionListener(this);
quitButton.addActionListener(this);
initConnection();
[b]publicvoid actionPerformed(ActionEvent ae)
{
try
{
pw.println("E" +inputData.getText());//E and string to server
String reply = bf.readLine();//receive reply from server
results.setText(reply);//display server reply in results field
}
catch(Exception e)
{System.out.println("Problem establishing a connection 1");}[/b]
try
{
pw.println("N" +inputData.getText());
String reply = bf.readLine();
results.setText(reply);
}
catch(Exception e)
{System.out.println("Problem establishing a connection2");}
try
{
pw.println("U");
String reply = bf.readLine();
results.setText(reply);
}
catch(Exception e)
{System.out.println("Problem establishing a connection");}
try
{
pw.println("Q");
s.close();
bf.close();
pw.close();
}
catch(Exception e)
{System.out.println("Problem establishing a connection");}
}
publicvoid initConnection()
{
try
{
s =new Socket("127.0.0.1", 1200);
InputStream is = s.getInputStream();
bf =new BufferedReader(new InputStreamReader(is));
OutputStream os = s.getOutputStream();
pw =new PrintWriter(os,true);
}
catch(java.io.IOException e)
{System.out.println("Problem establishing a connection");}
}
publicstaticvoid main(String[] args)
{
Client cf =new Client();
cf.setSize(300,200);//300 pixels by 200 pixels
cf.setVisible(true);
}
}

