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);

}

}

[6196 byte] By [nick211001a] at [2007-11-27 7:30:31]
# 1

did you mean you are using several button with only 1 listener? and you need to know which button being pressed?

first approach you can using 'ae.getSource()'.

public void actionPerformed(ActionEvent ae) {

Object source = ae.getSource();

if (source == numberButton) // do number btn pressed action

if (source == nameButton) // do name btn pressed action

// so on...

}

or you can assign actionCommand on each source component.

numberButton.setActionCommand("Number");

nameButton.setActionCommand("Name");

// ...

// in actionPerformed

public void actionPerformed(ActionEvent ae) {

String command = ae.getActionCommand();

if (command.equals("Number")) // do number action

if (command.equals("Name")) // do name action

// ...

}

j_shadinataa at 2007-7-12 19:10:44 > top of Java-index,Java Essentials,New To Java...
# 2

You could also have separate actionlisteners for all the buttons

nameButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

//Action here

}

});

This way you dont need to implement ActionListener interface in class Client.

johan.ga at 2007-7-12 19:10:44 > top of Java-index,Java Essentials,New To Java...
# 3

Many thanks to you both.

I now need to work out why the following server method is not returning either the staff name or email address info from a hashtable included in it.

switch(clientLine.charAt(0))

{

case'E':

{

String staffName = (String)emails.get(clientLine);

pw.println(staffName);

break;

}

nick211001a at 2007-7-12 19:10:44 > top of Java-index,Java Essentials,New To Java...