Delphi Clinic | C++Builder Gate | Training & Consultancy | Delphi Notes Weblog | Dr.Bob's Webshop |
|
Design time or not?
Java Beans are Java components which can be used in a visual development environement like JBuilder.
We can change properties of these components from within the Object Inspector.
As long as we are using the Bean in the JBuilder IDE, we are using the Bean during design time.
Once we are done designing the application with the Bean and we are running the program with the Bean, we are using the Bean in run-time. To change the properties we must now do this programmaticly in the application, because we cannot use the JBuilder IDE.
Most of the time the Bean must behave the same during run-time and design time. But sometimes we want to have different behaviour in the JBuilder IDE than during run-time. For example when working with large database queries we don't want them to be opened in the IDE.
But how do we determine in which mode the Bean is operating: run-time or design time? Therefore we can use the java.beans.Beans.isDesignTime() method. This method will return true if the Bean is in the JBuilder IDE, else it will return false.
Let's look at this in an simple example. First the Java Bean:
public class SimpleJavaBean { public SimpleJavaBean() { } public boolean isIsBeanInDesignTime() { return java.beans.Beans.isDesignTime(); } public void setIsBeanInDesignTime(boolean newIsBeanInDesignTime) { /* no implementation */ } }
This is a very simple Java Bean with one property: isBeanInDesignTime. The get method for this property (isIsBeanInDesignTime()) returns a boolean value using the java.beans.Beans.isDesignTime() method. The set method doesn't do a thing, so we provide no implementation here.
No let's see what the value of the property is during run-time, when we execute the following test application:
public class TestSimpleJavaBean { private SimpleJavaBean bean = new SimpleJavaBean(); public TestSimpleJavaBean() { System.out.println("Design time? " + bean.isIsBeanInDesignTime()); } public static void main(String[] args) { TestSimpleJavaBean app = new TestSimpleJavaBean(); } }Output:
Design time? false
As we can see the returned value is false, because we are using the Bean at run-time.
Now when we use the SimpleJavaBean in the JBuilder IDE and look at the Object Inspector we get a different value:
We see that the property now has the value true, because we use it at design time.