Wednesday, February 20, 2008

Need for Netbeans



Why is netbeans necessary??


"Necessity is the mother of all inventions", goes the old saying. Well my dear friend,its the same case here also. Although Java has become inseparably linked with the Internet, it is important to realise that java is first and foremost a programming language. Let us just take a ride down the history lane and look at the development of programming languages. We all know that it was C which first got the status of a "programmer's language". C was developed from 'B' and this fact is well known. However, C++ was also in the coming and it came! Why did this happen? Did the arrival of C++ prove C to be an incompetent language?

NO! As the complexity and demand grew, so did all the computer languages. So after C and C++, the stage was set for Java!! After installing jdk and jre, one can execute java applications at the command prompt. In this feature, it is important for the programmer to remember all commands and recollect them at the appropriate time. Isn't this all a little tedious? Don't we like everything at the click of a button? We always aim for more compact and steady platforms! Many paradigms of view.. god o god!

And all this brings us to confluence of IDE and java applications onto a single platform.. NETBEANS IDE!!! :D I know I seemed like it was the most obvious thing.. ha ha.. Well well,I just saved you a lot of work which would have taken you a lot of time to figure out.. All my blog readers.. consider yourselves blessed!! :D

Experiment Time



Netbeans is NOT the place where one can gain expertise in java! So if you are a novice, I say-"stick to routines". Just to make my point felt, I want to cite an example. Consider this program.



class Fun
{
public static void main(String[] args)
{
int count,i=0;
String words;
count=args.length;
System.out.println("Number of arguments= " +count);
while (i < count)
{
words=args[i];
i++;
System.out.println(i+ ":" + "Java is " +words+ "!");
}
}
}



Execute this program in jdk and at the command for compilation,give the following statement(let your filename be Fun!)

java Fun Robust Simple Secure Portable Multithread

The output of your program would be as follows:

Number of arguments=5
1: Java is Robust!
2: Java is Simple!
3: Java is Secure!
4: Java is Portable!
5: Java is Multithread!

Isn't this such a cool output!!! Now, you know the use of String[] args!!! If you were a starter, I sure can swear that you wouldn't have figured this out.. As far as my knowledge with netbeans goes, I don't think there is an option where I can provide arguments at run-time.. (If there is that option, I am yet to discover it then!!)




Observe


One of the main advantages of using IDE is that it makes life easy for a programmer.
For example,if I were to type a program which has a command to print a statement,one can observe that a window pops up as soon as "System." is typed. This window is as shown below.





This happens because the System class contains several class fields and methods and they cannot be instantiated. The facilities provided by the System class include standard input, standard output, and error output streams. By convention, this output stream is used to display information or messages that are intented to be attended by the user. "println" which is normally added at the terminal end belongs to the PrintStream. This class prints Java primitive values and object to a stream as text. Errors if present can be detected by calling the checkError() method. Additionally, this stream can be designated as "autoflush" when created so that any writes are automatically flushed to the underlying output sink when the current line is terminated. "println" just terminates the current line by inserting a line separator string(which does not mean \n).

Taking the next step


Before we proceed further,one must understand the essence of Beans. To better understand its significance,consider the following. Resistors,capacitors and inductors form the building blocks of a simple electronic circuit. All these components can be reused in complex/integrated circuits which probably yield more efficiency. This is because the functionality of every component is understood and it does not change with the system. So,every aspect of the component's behaviour can be documented. Unfortunately,until recently "write-once,run-anywhere" paradigm has not been highly successful in achieving the benefits of reusability and portability. Large applications grow high in complexity and it becomes very hard to enhance. Part of the problem is that ,until recently,there has not been a standard,portable way to write software component. This is where the role of a bean comes into play. As per standard definition, a bean is a "software component that has been designed to be reusable in a variety of environments". It may perform a simple operation such as spell check in a document or a complex one like forecasting the performance of a stock.


Netbeans is one such platform which also aids software development. In netbeans,it is important to realise that the platform recognizes and evaluates strictly in terms of a 'project'.


Time to learn:
Of the Java SE applications of netbeans,there is an application called 'Java project with existing sources wizard'. Click on File->New project. Click on java and choose the 'Java project with existing sources wizard'. Then,choose a project name and a project folder and click next. When adding the source package folder,add the folder which contains your java programs. [In a windows system,by default,all these java programs are present in C:\Program files\Java\jdk(version)\bin]Finally,click finish and your project is created.
NOTE: If you include this folder,then the whole folder becomes a project and your individual programs gain the status of a file.

If you have followed all the steps I have mentioned above,then its first part accomplished


Now,you can run any file present in your project. To open your file,click on your project folder which will open all the files tored in it. In my example,I am going to run a program which is stored as "Accurate.java" in my systen. To illustrate this,I am now going to run a file in my project. The screenshot is as shown below.







The program I have shown is a basic multithread program. Its code is



class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New thread: " + t);
t.start();
}
public void run()
{
try
{
for(int i=15;i>0;i--)
{
System.out.println(name + ":" +i);
Thread.sleep(200);
}
} catch (InterruptedException e)
{
System.out.println(name + "interrupted");
}
System.out.println(name + "exiting");
}
}
class Accurate
{
public static void main(String args[])
{
NewThread ob1= new NewThread("One");
NewThread ob2= new NewThread("Two");
try{
Thread.sleep(1000);
ob1.t.suspend();
System.out.println("Suspending thread one");
Thread.sleep(1000);
ob1.t.resume();
System.out.println("Resuming thread one");
Thread.sleep(1000);
ob2.t.suspend();
System.out.println("Suspending thread two");
Thread.sleep(1000);
ob2.t.resume();
System.out.println("Resuming thread one");
} catch (InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main thread exiting");
}
}



To run this file,we click on Build->"Complile Accurate.java" (F9). After succesful compilation,we can run the program by clicking Run->Run file->Run "Accurate.java" (or Shift+F6). After this,we can observe the output which is




New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
One:15
Two:15
One:14
Two:14
One:13
Two:13
One:12
Two:12
One:11
Two:11
Suspending thread one
Two:10
Two:9
Two:8
Two:7
Two:6
Resuming thread one
One:10
Two:5
One:9
Two:4
One:8
Two:3
One:7
Two:2
One:6
Two:1
Suspending thread two
One:5
One:4
One:3
One:2
One:1
Resuming thread one
Twoexiting
Main thread exiting
Oneexiting

Formatting

All programmers know that formatting of a code is very important in any code. This makes the program more clear,especially when the program is too lengthy. On another platforms,the programmer needs to take the extra effort to do this. However,netbeans simplifies everything for you. To make my point clear,consider the screenshot below.






I suggest you to follow the steps carefully to format your code.

1) Select your entire program with the right click of a mouse ( the picture will be as shown above).

2) Right-click and click on the option which reads "format". The shortcut is Alt+Shift+F.

Simple,isn't it?? Just by following these two simple steps,one can arrive at a correct formatted code!

Tuesday, February 19, 2008

Web Services

Web services?? Don't get confused..You heard me right! Double whammy for all you complete internet addicts! Netbeans allows access to API documentation of various web applications! Now,thats something new!! Firstly,what is API?? Firstly, API stands for Application Programming Interface and it implies to the source code interface your operating system provides when requests are made by computer programs. To notice this,switch on your internet and open netbeans 6.1. After this,you get an option called "services" at the left side of your screen on which you will have to click. After doing so,you will get a screen as shown below.




Right-click on the tab at facebook and click on "view API documentation". Why am I choosing facebook to explain my point? Well,today is the age of social networking and the chances that a person knows facebook is higher than the person knowing anything else!! So, your facebook application also makes method calls over the internet by sending HTTP requests to the facebook API server. This part of the option of netbeans is best learnt when the YOU choose to explore it! Like the tag line of netbeans 6.1 says..."the only IDE you need!!" Its open source! So guys,get started if you still haven't!!

Saturday, February 9, 2008

GUI!

A GUI is a graphical (rather than purely textual) user interface to a computer. Why is GUI necessary? Well,the term was coined because the early interactive user interfaces to the computer were not graphical but merely text oriented. This was highly cumbersome as you had to remember all the commands and the response of the computer was infamous as well. Majority of today's operating systems come with a graphical user interface. Applications typically use elements of the GUI that come with the operating system and add their own graphical user interface elements.


In netbeans,GUI design takes place in the IDE workspace. The tools present in IDE's java GUI tools include
1) GUI Builder
2) Inspector Window
3) Palette Window
4) Properties Window
5) Connection Wizard
6) Palette Manager

Instead of getting down to theoritical aspects of the GUI, I prefer to explain the method of implemeting this concept in netbeans by creating a project. Our project is to create a calculator.


i) Click on File-> New Project-> Java(in categories)-> Java Application(in projects). After doing this, click next.

ii) Give project name as Calculate and while doing so,ensure the following important points.
(a) Select "set as main project" .
(b) Deselect "create main class" if it is selected.

iii) Click Finish.

iv) In projects window, right click Calculate and select new->Jframe form.
Give
(a)class name: CalculateUI
(b) package : my.calculateThen click finish.

v) Next,we use the Palette to create to create the layout of our calculator. To use the options present in the palette,select a JPanel from the Palette and drop it onto the JFrame. In our design of the calculator, we use four jLabels, four jTextFields and eight jButtons. After dragging and dropping all of these on to the jFrame,our screen should look similar to the one shown below. If you do not clearly understand how to use GUI builder,the following webpage might come in handy. Do check this out.



Here,I have just enlarged the photo to aid better understanding.








vi) Now,we rename every display text component added on to the jFrame window. To do this, double click on every component and rename. The screenshot after doing so will be as shown below.







Just to make the photo more clearer,I have added an enlarged version of the photo. Hope it helps..!





Here it is important to observe that basic arithmetic operations can be performed on two operands wgich is indicated by 'First Number' and 'Second Number'. 'Unary field' is used when an operation is to be performed on a single number.


vii) After renaming is done,we need to add functionality to every component of the calculator. Let us start with the EXIT button. To add functionality,right click on the exit button and choose action and select action performed. The IDE will then open up the Source Code window. Now, scroll to where you implement the action you want the button to do when the button is pressed. Your source code window contains the following statement.





private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{ //TODO: Add your handling code here :
}




We add one line to exit and our final code looks as shown below. Similarly,one should follow the same procedure for adding functionality to different buttons and the codes for all buttons are as shown below.


I) EXIT Button

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
System. exit(0);
}
II) SQUARE button:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
float num,result;

num=Float.parseFloat(jTextField3.getText());
result=num*num;
jTextField4.setText(String.valueOf(result));
}


III) MULTIPLY Button:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
float x1,x2,result;

x1=Float.parseFloat(jTextField1.getText());
x2=Float.parseFloat(jTextField2.getText());
result = x1*x2;
jTextField4.setText(String.valueOf(result));
}




IV) ADD Button:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
float x1,x2,result;

x1=Float.parseFloat(jTextField1.getText());
x2=Float.parseFloat(jTextField2.getText());
result = x1+x2;

jTextField4.setText(String.valueOf(result));
}



V) DIVIDE Button:


private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
float x1,x2,result;
x1=Float.parseFloat(jTextField1.getText());
x2=Float.parseFloat(jTextField2.getText());
result = x1/x2;
jTextField4.setText(String.valueOf(result));

}




VI) RECIPROCAL Button:


private void jButton6ActionPerformed(java.awt.event.ActionEvent evt)
{
float num,result;
num=Float.parseFloat(jTextField3.getText());
result = 1/num;
jTextField4.setText(String.valueOf(result));
}


VII) SUBTRACT Button:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
float x1,x2,result;
x1=Float.parseFloat(jTextField1.getText());
x2=Float.parseFloat(jTextField2.getText());
result = (x1-x2);
jTextField4.setText(String.valueOf(result));
}




VIII) CLEAR Button:

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText(" ");
}




Its mission accomplished! Our calculator looks as shown below.






To compile build/compile the project, click on build->build main project.Then to run the main project, click on run-> run main project. When you do this, you can make calculations in this calculator! :)

Friday, February 8, 2008

Installation



I consider that by now, all my blog readers would have taken the first step atleast. The installation procedure is quite simple and it moves smoothly. And hey,you must be bored waiting for the 183MB file to download!! :) Ah, are you telling me the links for online jokes to keep you entertained now?? :D Help yourself with the www guys! (waiting waitng waiting....)

Phew! At last,eh?? I know! So, your installation is complete. If you are facing problems, I suggest you have a reading of this page:

http://www.netbeans.org/community/releases/61/install.html

I hope its mission accomplished now!


Follow Traditions



People people people! No matter how adept
you are at IDE or java,traditions come first!
Meaning we always start off with our initial
little simple java program before taking it
further.. So.. here goes my mundane routine of explaining to you as to how one can carry off their first program on netbeans.

SMILE PLEASE!

In netbeans click on file->new project. After doing this,you get a window as shown below.






Click on Java application tab and click next. You then see the java application box where you enter your project name as HelloWorld(as shown ). Its very important to observe here that the main class is named automatically..!!

If you have done this,then you are on the right track. After this,writing your one line is a piece of cake because everything else in the window is ready for you! IDE's.. bliss!!!





You get your window as shown in the right. Give your basic print statement as

System.out.println("Hello World");

After this is done,you have to build your program by pressing F11 or go to build->build main project. If you are not getting your output screen,go to window->output->output(OR ctrl+F4) Then,run your program by pressing F6 or run->run main project... :)

Congratulations! You wrote the simplest program on netbeans!!


Monday, February 4, 2008

Welcome!


Hello everyone,
This is it! My first real attempt at blogging! And I chose NETBEANS to start with. Its really exciting for me to write this blog from a perspective of exploring netbeans i.e, as someone who is trying to get a strong grip of it. So, this is where you start your journey.. your all important initial view!!