This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes setActionCommand() in Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "setActionCommand() in Applet" Watch "setActionCommand() in Applet" New topic
Author

setActionCommand() in Applet

Arun kumar Maalik
Greenhorn

Joined: Jul 04, 2006
Posts: 18
Would any body tell me that what is the function of setAction command in the following code. I got beet confuse about this command. Also i would like to know since getActionCommand is a method of Button class then How we r calling it with ActionEvent object.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class first extends Applet implements ActionListener{
Button left,right;
TextField display;
Button btn2=new Button("CANCEL");
public void init(){
left=new Button("Gauche");
add(left);
display=new TextField(5);
add(display);
right=new Button("Droit");
add(right);

left.addActionListener(this);
right.addActionListener(this);

left.setActionCommand("Left");
right.setActionCommand("right");
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
if(s.equals("left"))
display.setText("<---------");
else
if(s.equals("right"))
display.setText("-------->");
}
}

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
setActionCommand sets the string that is part of the ActionEvent when the button is clicked. You use it to determine which action should take place in your actionPerformed method. The code looks fine, i.e. you're using the two methods correctly. (Of course, you set "Left", while you check for "left", so that wouldn't work.)


Ping & DNS - my free Android networking tools app
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: setActionCommand() in Applet
 
Similar Threads
layout manager question
Password verification program
Applet Problem...
Calculator clear button not working...
NullPointerException - Using JTextField to Add Numbers