i have a simple JPanel bean in my projects, now i want to drag my panel bean class into my jframe.
My panel bean class is like this:
public class BeanPanel extends javax.swing.JPanel {
/** Creates new form BeanPanel */
public BeanPanel () {
initComponents();
Session session=HibernateUtil.getSessionFactory().openSession();
}
This code seem to break the bean:
Session session=HibernateUtil.getSessionFactory().openSession();
When i try to drag the class into my JFrame bean i had this error message:
This component cannot be instantiated. Please make sure it is a JavaBeans Component
If i comment it all works fine.
What is the reason of this?
Thanks.
Don’t do expensive work (like opening a session) in the constructor. At best use only assignments in constructor. In your case when placing the component, NetBeans is calling its constructor. Which can’t connect, because probably the appropriate configurations aren’t loaded, or because the classpath is correct, or whatever.
create a getter and setter for
session, and use lazy initialization in the getter:That said, I think you may have problems with session handling. Perhaps you can take a look at the
getCurrentSession()method ofSessionFactory.Another thing – don’t mix database access and UI. Move the handling of data outside your Panels.