Thursday, January 8, 2009

Re-using code

So now I have an applet which works (and works jolly well, though I say so myself), but the code has been assigned to the wrong package, and I'd like to start again, with everything assigned to an appropriate package, but I don't want to fiddle around with the graphics on the JFrame getting all the shapes sizes and fonts right again. So I want to reassign the classes I've already created to the right package.

Coming from a Microsoft background, I think in terms of exporting files to a package, and I look in the file menu for something resembling export or import. There is something for project groups, but nothing about assigning files to packages or importing or exporting. Next I look in tools. Maybe there is a package assignation tool? No luck there. Options? I am clutching at straws now.

So I scan through every menu, and find nothing. Maybe if I right click on the class file icon there will be a context menu item about assigning the class to a package. But no, I can only open, copy or paste. There is an idea. Let's copy it into a new project. Hmm that seemed to work. At any rate the class has attached itself to the new project tree. I think I'll do that for the other class in the original project.

And I also remember noticing in the context menu for the project there is a create new package option. I'll try that. I'll call it JHipptest. And now I'll try dragging the pasted classes in to the new package. Hmm that seemed to work. But I can't open them. I know, let's copy and paste the source code from the original project into the new package. Now let's have a look at the code, and Yes! very cleverly NetBeans has added "package JHipptest;" as a first line in the source code.

I have spent weeks and months researching this in fora, NetBeans help, and the tutorial to no avail. It reminds me of when I first used a Mac. I had to ring a technician to ask how to delete a file. There was no right mouse button, and nothing about file deletion in the menus. I felt a real fool when I was told I just had to drag the file to the Trash. Maybe NetBeans was written by an old Mac user.

In summary, it is not a disaster if you forget to assign your class to an appropriate package when you first create the source code in NetBeans. You simply add a package to your project, and then drag your source file to the package node in the project explorer pane. And you don't need to start a new project as I did - you can do it all in the original project. On the other hand, if you want to use existing code in a completely new project, it is simply a matter of copying and pasting the source files in the project explorer pane.

No comments: