Netbeans Tips and Tricks

Here are some netbeans tips and tricks for your 2nd project

Organising classes into packages

Netbeans makes it easier to organise your java classes into packages (folders). Once a project has been created, you can easily create new pages by right-clicking Source Packages in the project explorer window, then click new and select Java Package. All of your UI code (JFrames etc) should go in a UI (or similarly named) package. All of your class diagram classes (TaxiDriver, Fare, Customer etc) should go into their own package called "Domain", "Logic", or "Business". Finally you should create a package called "IO" (or "DAO" if using a database). In there goes classes that will read and write your class diagram classes to storage. The best thing to do there is to create one IO class for each of the class diagram classes that you want to store. For example, if you have a Customer class and you want to store Customers, you should create a CustomerIO class which should contain methods for reading and writing Customers from storage.

Following the above code design will get you serious marks as it makes you code very easy to read and reuse. This is an example of the n-tier architecture (google or check with Csaba for more info!)

Write code faster with netbeans

Java based IDE's like Netbeans and Eclipse use what is called a refactor to automatically modify and generate code. Three refactor features that will be really useful for you are:

  • Format and correctly indent your code automatically by hitting Alt+Shift+F
  • You can automatically generate Getter and Setter methods (and correctly encapsulate your class level attributes) by right clicking your code > select refactor > select encapsulate fields. A dialog which allows you to select which fields you want to generate getter and setters for will then show.
  • You can rename a method or a variable (and automatically rename all its usages) by right clicking and selecting rename or hitting Ctrl+R while the method or variable is highlighted.

I highlighted the above features because they have been the ones most useful to me over the years. There are many more useful tricks in netbeans so the IDE is well worth exploring!