Getting to Grips With Netbeans

thrillukrainianSoftware and s/w Development

Jun 7, 2012 (5 years and 2 months ago)

287 views

Getting to Grips With Netbeans
Matt Smart
m.j.smart@cs.bham.ac.uk
www.cs.bham.ac.uk/smartmj
December 17,2010
1 Introduction
Netbeans is an integrated development environment,or IDE.Up to now,you should have been using a
plain text editor like nedit to complete your exercises.As we move into the second semester,you may
nd some of the things that an IDE can do to be quite useful.
An IDE helps you to develop programs more quickly,by combining several features:automatic com-
pilation of code,syntax highlighting,error highlighting,and the ability to run code from within the
program.We will be using Netbeans,though you may have seen others (such as Eclipse).Which you use
is really up to you,but we only oer support for Netbeans.
2 Getting Started
Let's begin.Open Netbeans by clicking on the Netbeans icon on your desktop.If it's not there,you can
nd it in the menu under Programming!Netbeans.
2.1 Projects
When you use Netbeans,your code needs to be organised into projects.A project may contain more than
one program|in fact,you might have had a single project to cover all of your exercises this semester.
This is the approach that we'll go with.The idea is that it's convenient to keep all of your exercises
together,so that you know where to look for them.
1
For this course,we'll make a single project called mscWorkshop.Each time that we write a new set
of exercises for each week,we'll make a new package to store them in (more on those later).Follow these
steps to make a new project:
1.Open Netbeans
2.You should see a\Welcome"panel.Using the menu,select File!New Project.This should open a
\New Project"window:
3.There are two parts to making a new project.First (\Choose Project"),you must select\Java"
under\Categories",and\Java Application"under\Projects".Then click the Next button.
4.Next,you'll need to lay out some specics about your project.We need to give it a name,which we've
already decided is mscWorkshop.Then,give it a location.This will be where all of your java les
and classes are stored,so choose carefully.In the picture below I've chosen/home/matt/work,
but for you it might be something like/home/msc/abc123/work.Netbeans will choose the
project folder.Now for the important bit:Make sure\Create Main Class"and\Set as
Main Project"are unticked:
2
5.Click\nish".You should now have something that looks like this:
6.Now,we need to start thinking about packages.A package is java's way of grouping code together.
Remember when you're importing code in Java,you write something like the code below?
import java.util.ArrayList;
Well,the java.util stu means that the ArrayList class is in the package java.util.Sure
enough,if you look at the top of ArrayList.java,you'll see the line
package java.util;
Packages really only exist for the programmer's benet:they don't help java to nd classes,but
they mean that you can arrange classes how you want,for your own convenience.We'll begin by
making a new package.
7.Click on the duck-head arrow next to\Source Packages".You'll see that there are no packages yet.
So,right-click on\Source Packages",and select New!Java Package.A window like this one will
appear:
3
Name the package.Since this is just an experiment,I'm calling mine testpackage.Don't change
the other options.Note that packages start with a lower-case letter.Click Finish.
8.Next,we need to put a new class into our package.This is simple too:right click on our new
package testpackage,and select New!Java Class.Call the class TestClass:
As usual,our classes have upper-case rst letters.Click Finish,and you'll note that the whole
window changes:
4
On the right hand side is the code for the class we just wrote.We can put whatever we want into
it,so let's begin!
3 Writing Classes
Netbeans is useful because it helps with a lot of common mistakes,and helps you to complete repetitive
tasks more quickly.We'll demonstrate a few of those features now.
1.Modify your TestClass so that it contains the following (which will only work if you named your
package testpackage):
package testpackage;
/
 A Test Cl ass
 @author matt
/
5
import java.util.;
public class TestClass f
private ArrayList<String> testList;
public TestClass( )f
testList = new ArrayList<String>();
g
public void addToList(String s)f
testList.add(s);
g
public String toString( )f
String ret ="The list in this class contains:\n";
for(int i = 0;i<testList.size( );i++)f
ret +="("+i+")"+testList.get(i)+"\n";
g
ret+="----";
return ret;
g
g
Note that the package statement is the rst thing in the class.Otherwise,it's just a simple class
which wraps an ArrayList of Strings,and provides a convenient way to print elements and their
indices.It doesn't have a main method.
2.Now,we want to run this code from somewhere else.So,we can make a class which contains a main
method.To do this,right-click on the package again,and choose New!Other....Select\Java Main
Class"from\File Types".Call the class TestMain.
Exercise This class just contains a single main method.Complete the method:create an instance
of TestClass,add two words to it,and then print out the list using the toString method of
TestClass.
While you're doing that,take a note of what Netbeans does.If you make a mistake,you get a red
squiggly line under the erroneous code.If you hover over the red line,Netbeans tells you what's
wrong:
6
In this case we just needed to correct nw to new,but let's try another.If we try to use a class that
isn't imported,Netbeans will oer to import the right class for us.Click on the light bulb to the
left:
3.Once you're done writing your main method,we can see if your code works.Either press Shift+F6
or right-click on the class and select Run File,in the class with the main method.You'll see that
the output of the code appears below,in the output window:
7
4 Other Nifty Tricks
Netbeans has a lot of functionality.One of the most useful things is that it can complete code for
you.Let's start by making a new class MyFrame which extends JFrame (make sure to import the right
packages).
1.Code Completion We know that there are things that need to be done before a frame will display
on the screen.We have to set the size,and make it visible.But what if we don't know the syntax
of a command?Let's say we want to set the size.Try typing the rst few letters for the command,
then pressing Ctrl+Space in the constructor of your class:
8
Netbeans knows which commands are available to JFrame.I can select the one I want,then
press Enter.The correct code will appear,with the rst parameter highlighted.After lling each
parameter in,press Tab to move to the next.
This eect is also seen when modifying objects.Try the following code.
(a) Complete your frame constructor so that the frame has a size of 300300,uses FlowLayout
and has a JButton in the center.When you write the following to set the button text:
JButton b = new JButton( );
b.
Note that Netbeans gives you a menu of commands that can be applied to b.
If you supply netbeans with the right les,it will even show you javadoc for each of the methods
you can use on your objects.See if you can nd out how to do this.
2.Field Encapsulation Create a new package,test2.Add a class AnotherClass to it,which has
an int eld and a String eld.Write a constructor which initialises these values.Next,right-click
on the class in the left-hand pane.Go to Refactor!Encapsulate Fields.You'll get a window like this:
Check all of the tick boxes next to the eld names.Click Refactor.And look!Getters and setters
have been added for all of your elds,complete with javadoc that you can ll in.
5 Putting Your Exercises Into Netbeans
You should,by this point,have two packages,testpackage and test2.Use the le manager in
Linux to nd where those packages actually are (if you followed the instructions earlier,they might be in
/home/msc/abc123/work/NetbeansProjects/mscWorkshop/.You'll note that there are several
folders:
9
The only one you need worry about is src,which contains the.java source code les.Open it,
and you'll see a folder for each package you made earlier.The names of these folders correspond to your
package names.
If you open either of them,you will just see java les.This is because Netbeans compiles your code
such that the.class les are stored in the build folder instead.
If you want to try putting your exercises into Netbeans,take a backup rst.Then,put a copy of the
folder containing one week's java les into that src directory.You may wish to rename the folder to a
sensible package name,like week7.
Go back into Netbeans,and if you did everything right,your exercise will have appeared on the left
hand pane as a package|open the package and open a le.But,oh no!A red line,with the error
\Incorrect Package".This is because Netbeans is treating that folder as a package,meaning you need to
put a package statement at the top of every le in it.Try this for yourselves now with your solution to
the Week 3 exercises.
6 More Stu
Netbeans is a very versatile and powerful tool|be careful with it.Next semester,you will nd it much
easier to know how to use Netbeans,and its integration with subversion,a group-work revision control
system.It even has tools for generating graphics using a form editor|I would advise against using those,
unless you know how to x the inecient code that Netbeans produces.Have fun!
10