I have successfully created a Java class that provides database connectivity and returns a connection object. I need to use this connection object to
- Retrieve information from the database (which I already know how to do)
- I need to use it in the action event source code behind the button control
Unfortunately, I am using NetBeans and some auto generated code to build the GUI, and my question is simple:
When you create a class, you need to instantiate it and then call the method. Where do I instantiate this class in my form? Do I do it in the main, since it is the entry point of all programs, or do I do it behind the code for the button. I am a novice so please understand.
When I created the project, I created a main class too, and then I added a form to it. I then created a different project which is a class lib and added that to my project. How do I link the two together?
It sounds like the NetBeans GUI editor is clouding the picture. You can tame it by limiting it’s use to a single panel, as shown in this example. The default layout of
JFrameisBorderLayout, sof.add(new NewJPanel())has the effect of adding a panel to theCENTER. You can add other panels, perhaps ones having different layouts, to other areas.Addendum: Note also that the generated
ActionListenertypically calls a method having a signature such as this, wherexxxis the name of the generated control:These methods are inside the class but outside the generated code fold, so they may be edited as required.