2

I have successfully created a Java class that provides database connectivity and returns a connection object. I need to use this connection object to

  1. Retrieve information from the database (which I already know how to do)
  2. 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?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
rambokayambo
  • 341
  • 4
  • 10
  • 27
  • not too much details, making hard to answer it. maybe try to post your code? – chenyi1976 Jun 08 '12 at 05:30
  • well..i created a class (.java) with one method. I need to call this method in a form. Where do i instantiate(bring this class to life through an object) so that i may utilize this method in the code behind a button controll – rambokayambo Jun 08 '12 at 13:40

1 Answers1

1

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 JFrame is BorderLayout, so f.add(new NewJPanel()) has the effect of adding a panel to the CENTER. You can add other panels, perhaps ones having different layouts, to other areas.

Addendum: Note also that the generated ActionListener typically calls a method having a signature such as this, where xxx is the name of the generated control:

private void xxxActionPerformed(java.awt.event.ActionEvent evt) {}

These methods are inside the class but outside the generated code fold, so they may be edited as required.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045