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();
}
//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.
12-13-2005, 03:49 AM
viviensiu
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.
12-13-2005, 06:07 AM
Arya Dev
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.