• 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

Please help me

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following applet called ButtonApplet contains three buttons and three labels. Actually it should display when the user clicks a button, the associated label should reverse its text. Clicking on a button that's associated with a changed label should reverse the text
again, back to its original form.
but the following applet is not changing the label back to orignal form
import java.awt.*;
import java.applet.*;
public class ButtonApplet extends Applet
{
Button button1;
Label label1;
Button button2;
Label label2;
Button button3;
Label label3;
public void init()
{
button1 = new Button("Button1");
label1 = new Label("Label1", Label.CENTER);
button2 = new Button("Button2");
label2 = new Label("Label2", Label.CENTER);
button3 = new Button("Button3");
label3 = new Label("Label3", Label.CENTER);
add(button1);
add(label1);
add(button2);
add(label2);
add(button3);
add(label3);
}
public boolean action(Event event, Object arg)
{
if (event.target instanceof Button)
ChangeButtons(arg);
return true;
}
public void ChangeButtons(Object label)
{
if (label == "Button1")
label1.setText("1lebaL");
else
label1.setText("Label1");
if (label == "Button2")
label2.setText("2lebaL");
else
label2.setText("Label2");
if (label == "Button3")
label3.setText("3lebaL");
else
label3.setText("Label3");
}
}
Please help..............
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Kajol
I do not understand why u r using the method public void action(). Why don't you add the same actionlistener to the three buttons whose job is the same. It would be better if you use the java 2.0 event delegation model. Secondly you compared the button as if(lanel=="Button1"). Don't put the Button1 within quotes coz that means it is a string.
I wrote a code for you. I have not run it, but I am sure it will work fine
[code]
public class ButtonApplet extends Applet implements ActionListener{
Button button1;
Label label1;
Button button2;
Label label2;
Button button3;
Label label3;
public void init()
{
setLayout(new GridLayout(2,3));
add(button1=new Button("button1"));
add(button2=new Button("button2"));
add(button3=new Button("button3"));
add(label1=new Label("Label1"));
add(label2=new Label("Label2"));
add(label3=new Label("Label3"));
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Button b=(Button)ae.getSource();
if(b==button1)
{
if(label1.getText().equals("Label1"))
label1.setText("1lebaL");
else label1.setText("Label1");
}
//similarly write the code for the two other buttons
}
}
[code]
hope this helps you
bye
Tanveer
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// button test program : taken from Javaranch tanveer:15/12/00
// and modified
// using switch & case instead of a group of nested if statements
import java.awt.*;
public class ButtonApplet extends Applet implements ActionListener{
Button button1,button2,button3;
Label label1,label2,label3;
public void init()
{
setLayout(new GridLayout(2,3));
add(button1=new Button("button1"));
add(button2=new Button("button2"));
add(button3=new Button("button3"));
add(label1=new Label("Label1"));
add(label2=new Label("Label2"));
add(label3=new Label("Label3"));
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Button b=(Button)ae.getSource();
switch (b)
case button1:
if(label1.getText().equals("Label1"))
label1.setText("1lebaL");
else label1.setText("Label1");
break;
case button2:
if(label2.getText().equals("Label2"))
label1.setText("2lebaL");
else label1.setText("Label2");
break;
case button3:
if(label3.getText().equals("Label3"))
label1.setText("3lebaL");
else label1.setText("Label3");
break;
default :
break;
}
}
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian ,
Thanks a lot....for ur help....
I need some explanation....
public void actionPerformed(ActionEvent ae)
{
Button b=(Button)ae.getSource();
switch (b)
case button1:
if(label1.getText().equals("Label1"))
label1.setText("1lebaL");
else label1.setText("Label1");
break;
case button2:
if(label2.getText().equals("Label2"))
label1.setText("2lebaL");
else label1.setText("Label2");
break;
case button3:
if(label3.getText().equals("Label3"))
label1.setText("3lebaL");
else label1.setText("Label3");
break;
default :
break;
}
}
I couldnt get this function......can u explain it.....to me...
basically i couldnt get these two expressions below...
public void actionPerformed(ActionEvent ae)
{
Button b=(Button)ae.getSource();
I am sorry its my begining in JAVA......so please reply.......
Kajol
 
Kajol Shroff
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I think u all guys are pretty busy.....
Nobody is replying to my query......
I tried running both the above programs.......but it gives me an error that interface ActionListener not found.......
I havent changed the above code which Brian has pasted...
Can any body tell me the reason for this....
I really want the reply prompt.......as my exams are comming sooner.....
Kajol
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
for your last question-add import statement.
import java.awt.event.ActionListener;
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i make my applets signed..?
thanks with regards.
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Kajol
For the actionPerformed method, I suggest you read the event handling concept in java.
Tanveer
hi Brian
You can't put a button inside a switch. Only int and int compatibles(char,byte,short) can be put in a switch
Tanveer
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic