File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Please help me Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Applets
Reply Bookmark "Please help me" Watch "Please help me" New topic
Author

Please help me

Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
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..............
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
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


Author of JPhotoBrush Pro (www.jphotobrushpro.com)
Brian Mahn
Greenhorn

Joined: Dec 15, 2000
Posts: 2
// 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

Joined: Dec 04, 2000
Posts: 160
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

Joined: Dec 04, 2000
Posts: 160
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
NageswaraRao Karra
Ranch Hand

Joined: Nov 12, 2000
Posts: 34
hi!
for your last question-add import statement.
import java.awt.event.ActionListener;
maateen
Greenhorn

Joined: Dec 18, 2000
Posts: 23
how can i make my applets signed..?
thanks with regards.
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me
 
Similar Threads
how to solve the error?
Focussing a TextField
MY LOTTERY CLASS
How to open a file using Java
TEXTFIELDS TO INT