• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setActionCommand() in Applet

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic