1

I have created some custom JPanel classes using the NetBeans GUI Builder. Next, I added them to the palette. Then I created a custom JFrame and was able to drag my JPanels onto the JFrame. This worked great while I was simply working on the GUI front end. Now I am working on the backend logic, which includes some JDBC code. I have created a BaseballCardIO interface and implemented it in BaseballCardJDBCIO to centralize all the the database stuff.

Now, one of my JPanels, AddCardsPanel, needs a reference to one of these BaseballCardIOs. I started by creating one directly in the AddCardsPanel constructor. (I know, not the best design decision anyway...) Everything was working great until I open my JFrame class in NetBeans. It started to complain about not finding the JDBC driver class.

I want to continue to use the NetBeans GUI Builder for now. I have two solutions in mind to fix my problem:

1) Tell NetBeans where to find the JDBC driver and keep the code as-is. How do I do this?

2) Modify my design so that AddCardsPanel has a constructor which takes a BaseballCardIO as a parameter. This would actually be preferrable since it makes more sense for someone else to be responsible for creating the BaseballCardIO, not AddCardsPanel. However, I still need AddCardsPanel to play nicely with NetBeans GUI Builder, which means that it needs a no-args constructor. I imagine that I could add some code which detects if AddCardsPanel is being used as a JavaBean by NetBeans then the JFrame calls the noargs constructor. Otherwise, if my application is actually running, then the JFrame calls other constructor and sends it a BaseballCardIO.

Is this a good way to go? Or does anyone have any other possible solutions?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268

1 Answers1

2
  1. Add the driver JAR to NetBeans as a library, shown here, and to your project, shown here.

  2. In Window > Services > Database > New Connections, fill out the required fields.

  3. Don't let the NetBeans GUI builder dictate your design. Isolate database access to your TableModel and other component models.

  4. Edit your question to include an sscce that shows any problems you encounter; a .form should not be required.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I already added the JAR file to my project's libraries. I didn't know about adding it separately to NetBeans. Thanks! – Code-Apprentice Aug 12 '12 at 19:59
  • Good; you might try this [approach](http://stackoverflow.com/a/2561540/230513) to taming the GUI designer. – trashgod Aug 13 '12 at 01:25
  • I revisited my design. It's pretty ad hoc anyway, since this is a small project. I decided that I want to create my `BaseballCardIO` objects in `main()` and pass them to my `JFrame` and then to the `JPanel`s that need it. I think this will make it easier in future versions to add configuration options so that the user can set up alternative database back-ends or other storage targets. – Code-Apprentice Aug 14 '12 at 00:00
  • I think that's a good plan. You'll want to build a few small components and projects to see how they can be composed into larger ones. – trashgod Aug 14 '12 at 00:42