0

hi all, i have an applet code here..i wanna do database operations later on..but i m stuck to this basic applet function..have a look

/*
 * @(#)Appletsamp.java 1.0 11/04/09
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

import java.awt.*;
import java.applet.*;
import java.sql.*;
import java.io.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Appletsamp extends Applet //implements ActionListener
{
	public void init()
	{
	Button ok=new Button("ok");
	ok.setSize(50,50);
	ok.setLocation(200,200);
	add(ok);
	
	Button can=new Button("cancel");
	can.setSize(50,50);
	can.setLocation(200,200);
	add(can);
	
	TextField tf=new TextField();
	tf.setSize(400,600);
	tf.setLocation(300,200);
	add(tf);
	
	
	ButtonHandler bh=new ButtonHandler();
		
	ok.addActionListener(bh);
	can.addActionListener(bh);
	tf.addActionListener(bh);
	
}
	 class ButtonHandler implements ActionListener{
		
public  void actionPerformed(ActionEvent ae)
				{
								
		try{
						
					if(  (ae.getSource()).getLabel()== "ok")
					{
				System.out.println (ae.getSource()+"okokokok");
					}
				
				if(  (ae.getSource()).getLabel()=="can")
					{
				System.out.println (ae.getSource());
					}
		}
		
		catch(Exception e)
			{
			System.out.println(e+"ssss");
			}
		
		
		}
		
		
	}
//	}

	public void paint(Graphics g)
	{
		g.drawString("Welcome to Java!!", 50, 60 );
	}

}

it gives the following error..
--------------------Configuration: appletsamp - JDK version 1.6.0 <Default>------------------------------------------------
E:\appletsamp\Appletsamp.java:60: cannot find symbol
symbol : method getLabel()
location: class java.lang.Object
if( (ae.getSource()).getLabel()== "ok")
^
E:\appletsamp\Appletsamp.java:65: cannot find symbol
symbol : method getLabel()
location: class java.lang.Object
if( (ae.getSource()).getLabel()=="can")
^
2 errors
Process completed.

so guys..plz help this new bee:)..

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by JamesCherrill
1

1. To use Button's getLabel() method you must cast the Object returned by getSource() to a Button
2. You can't compare Strings with ==, use equals(String) instead, if you must.
3. If you want to know which button was pressed, test that directly

if(  (ae.getSource() == ok) ...

just remember to declare ok as

final Button ok = new Button("ok");

in your class, not in the init method.

Edited by JamesCherrill: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.