Saturday, August 16, 2008

Java Netbeans Tutorial

Netbeans Java Basics.

This guide is based on Netbeans 6. To use Netbeans you will have to have Java installed plus Netbeans. Any projects produced with Netbeans will run on any computer with any O/S that has java installed. This currently includes cell phones, computers, pocket pc, etc.

To install Netbeans, go to http://java.sun.com/javaee/downloads/index.jsp and choose the full install including Netbeans. This will install java and all of the basic Netbeans plugins.

After installation, start Netbeans. We will develop a basic calculator with multiple forms. This will use many of the skills necessary to develop basic applications.

When open, start a new project. Select file->new project, JAVA and Desktop Application. This will allow development of a general gui type Java Application. Give it a name, (ie FirstApp) and select finish.

A blank project will appear. This is actually a complete java application as it is. Try it. Near the top find the Green Triangle which says Run Main Project. It will compile and run a complete java application with a blank page. You will notice a standard menu and other familiar features already built into the basic java application. Close this and we will add some more to it.

On the left side of the screen is the development portion of the app. The block shown with File and Help is the main screen (called mainPanel). This is the screen that will open by default with your java application. We can add other screens (called jFrames or java Frames) to this to allow the user to have other options.

First, let's add some items to the mainPanel and see what happens. On the right side of the screen is the Palette. This is a selection of many pre-build components such as buttons, check boxes, etc to build familiar and easy to use applications. For this demo we will always use Swing components.

Now grab a Swing Controls Button and place it anywhere on the mainPanel. It is named by default jButton1. Java sees this button as having 2 parts, a variable name and a text label. First let's rename the variable name to something meaningful. Right click jon jButton1 and change the variable name to addButton. This can be anything you want but it's helpful to use something meaningful. Now java sees the variable name as addButton but the user still sees jButton1.

Let's change that and tell it to do something. Double click on jButton1. This is where we will change the text label and tell java what to do with it. A screen comes up labeled Set Action. Under Action, select Create New Action. By Action's Method will will add something that will tell us what the button does. Type openAdditionFrame. Under attributes change the Text to Add. The tool tip is helpful. This is where you add a hover-over baloon to tell the user what this button does. We'll use this. By tool tip type This button opens the addition frame.

When you click OK, it opens a new screen with lots of code. This all of the background code to make the java app worki. The curser is automatically placed near code labeled openAdditionFrame. This is the method we named earlier. We won't worry about any code now. On the upper left there are two tabs, Source and Design. Source is where you can view and edit any code with the project and Design is working on the GUI.

Go back to Design. Now lets compile and run what we have so far. Click the Green Triangle to perform this. We see our old familiar application come up with the Add button. The button does nothing yet but we can verify our application frequently by compiling and running.

Now we want to make the Add button open a separate screen (called a jFrame) where we will make a basic addition calculator.

We need to first create the second frame. While we are in Design mode on the right side of the screen select Swing Windows->Frame. Drag on top of the mainPanel. Now on the left side of the screen there are 3 tabs labeled Navigator, Projects and Inspector. Click Inspector. This will show us all of the files contained in our project. We should see FrameView, mainPanel, menuBar, and statusPanel and jFrame1. This is the second window we just added. Right click and change the variable name to addFrame.

Now we're ready to add some code. On the left side of the screen double click mainPanel. This brings our opening panel back up. Make sure to be in design mode (upper left of mainPanel). Now double click on the Add button we created.

This brings back the code editor. We will have some code like:

@Action

public void openAdditionFrame() {

}

This is called a method. A method begins with an open brace { and ends with a closed brace }. In between is we add the code to make things happen.

Now add the following code to make the method look like:

@Action

public void openAdditionFrame() {

addFrame.setBounds(400,400,400,400); //This opens the addFrame Frame to the position and size //indicated

mainPanel.setVisible(false); //This hides (not closes) the mainPanel

addFrame.setVisible(true); //This opens the addFrame Frame to the size indicated

} //above

//Anything with double slashes at the beginning are comments. Java ignores this.


Now click the Green Triangle to test. It should open the main panel. Now when you select the Add button another blank window should open. Close everything and return to the editor.

Now select the Inspector tab on the left. Again it will highlight the files included in this application. Double click on addFrame. It will bring up a blank window (frame) similar to the mainPanel except there is no menubar at the top.


From the Palette select Swing Controls->Text Field. Drag 3 of these in a culumn onto the addFrame.

The will be automatically named textField1 -3. Change the variable names. The one on the top will be no1TextField, the middle one will be no2TextField and the bottom will be no3TextField. Now you can edit the default text. In textField1 and textField2 put 1 as the default. Leave the sumTextField blank. Size each of them so they are the same. Now drag a label beside each text field. Name the top one Number 1, the middle one Number 1 and the buttom one Sum.

This will allow the user to input 2 numbers and we will calculate the sum and place that value in the bottom field.

Now we will add 3 action buttons. Drag 3 buttons over. Double click each to add a new action and change the default text on each button. Name the action and text Add, Clear and ReturntoMain.



We're ready to add some code here.

The add button is where the meat of our application lies. Double click on it to get to the code editor.

It will open up like:

@Action

public void add() {

}

Now we have to start adding some code. A few things first. All java (and C, C++ and C#) statements end with a ;. You will get an error if each statement isn't ended properly. Also regardign variables. The most common variables are String (text values), int (integer or whole numbers), and double (numbers with a decimal).

Also, java treats all values in a text field as text regardless of what you put there. If you want to value to be used with an integer or double variable it has to be converted.

In between the braces, declare 3 variables:

double no1;

double no2;

double sum;

Now when we select the Add button we want java to read the value in no1 and no2, internally add them and place the sum in sum.

To read in no1 and no2 add the following code:

no1=Double.parseDouble(no1TextField.getText());

no2=Double.parseDouble(no2TextField.getText());

This is kind of a mouthfull but here's what's happening.

First we're reading the text value (get) in no1TextField with the statement “no1TextField.getText()”

Next we are parsing (converting) it from a text value to a double value with Double.parseDouble(...)

Now we are storing the value of each of these text fields in the variables no1 and no2. So again, we read in a text value, convert it to a double and store it to no1. If we wanted a text value, such as name we wouldn't have to convert anything.

Now we will add:

sum=no1+no2;

This tells java to add the two converted values together and store them internally into the variable sum. Next we must send this internal value out to the sumTextField. First, remember though that all textField values MUST BE TEXT. So now we have to convert the double value sum to text and post it (set) to the sumTextField. Type:

sumTextField.setText(Double.toString(sum));

Here we are converting the double value sum to string with Double.toString(sum). We are then posting the string value into the sumTextField.

Both of these examples are compound statements where we are doing 2 things in1 statement (reading value and converting) or (converting and posting). This is a little confusing at first but more efficient.

Now the method should look like:

@Action

public void add() {

double no1;

double no2;

double sum;



no1=Double.parseDouble(no1TextField.getText());

no2=Double.parseDouble(no2TextField.getText());

sum=no1+no2;

sumTextField.setText(Double.toString(sum));



}

Click the Green Triangle to test it out.

Now the Add button on the mainPanel takes us to the addFrame. On the addFrame the add button works as we would expect by reading in the top 2 numbers and putting the sum in the sum box.

Now we'll work on the Clear and Return buttons.

Double click the clear button. The method will come up. We want to clear the values in the 3 boxes to start a new calculation. The method should read:

@Action

public void clear() {



no1TextField.setText("");

no2TextField.setText("");

sumTextField.setText("");

}


This simply puts a blank character “” in the textField. You could have replaced them with any number you wanted.

Now for the return button. This will close this frame and take us back to the mainPanel.

Double click on ReturntoMain and make the method say:

@Action

public void ReturntoMain() {

addFrame.setVisible(false);

mainPanel.setVisible(true);

}

This hides the current addFrame and makes the mainPanel visible again.

Now Click the Green Triangle to compile and run. All should work as expected.

One other thing. Java variable names are case sensitive, so no1 and No1 aren't the same variable.

This is a simple example but demonstrates several things to get going.

2 comments:

Nauman Bashir said...

dude there are some errors in it like:illegal expration,enum expration

Slyboy said...

how i do to show me result (double) whit 2 decimals? Ex 12.3232 show 12.32 onli. Tnx