-
Java Beans
Hai all,
I have just started to learn Java beans . Actually I am not able to understand its basics. I searched in net and found many tutorials but not able to capture the starting point. I got the following code from net and when I tried to run it it showed an information box "The bean does not have a main method". When I added a main() methode to it it is running but not executing the paint method so nothing is displayed in the output! Can anyone help me?
import java.awt.*;
import java.io.Serializable;
public class SimpleBean extends Canvas
implements Serializable{
private Color color = Color.green;
//property getter method
public Color getColor(){
return color;
}
//property setter method. Sets new SimpleBean
//color and repaints.
public void setColor(Color newColor){
color = newColor;
repaint();
}
public void paint(Graphics g) {
g.setColor(color);
g.fillRect(20, 5, 20, 30);
}
//Constructor sets inherited properties
public SimpleBean(){
setSize(60,40);
setBackground(Color.red);
}
}
Also if anyone knows a site for learning Java beans programming in simple steps please let me know.
Thanks in advance,
Arya.
-
hello,
Javabean classes are not meant to have main method inside them. A proper Javabean class should be implementing Serializable, which is meant to break up data chunks into smaller pieces for transmission purposes.
Also, all the variables are defined private variables. You can only have access to them using setters and getters. Setters are to initialize or assign values to variables. A typical setter should look like this:
private String name;
public void setName(String name){
this.name = name;
}
A getter is to return the value of variable to the caller. For example:
public String getName() {
return name;
}
the main purpose to have Javabeans is for encapsulation purposes / information hiding. You can go serach why encapsulation is important 
And btw to use a Javabean class you need to call it from another class which is inside the same package with the Javabean class. Just create an object of type Javabean then you can call the setters and getters by using the object created.
Last edited by viviensiu; 12-13-2005 at 03:52 AM.
-
Thanks for your guidance. Actually now only I understood the real meaning of "reusable component". This been I can call as and when needed. Now I got a grip and I will go for it. Thank u again.
Regards,
Arya.
Similar Threads
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
-
Replies: 2
Last Post: 04-08-2000, 08:13 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks