Today I want to start with the Netbeans IDE and getting the JDK installed on your system. In short, the NetBeans IDE is going to allow you to develop all levels of Java applications much quicker than coding things by hand (which, if you're not familiar with what an IDE is, is basically what an IDE is all about!) You can obtain the latest version of NetBeans from the following link.
NetBeans IDE with JDK bundle
I would strongly recommend this method since this will get you all of the required software to get started. Later you may choose to download another one of the bundles from the Sun Microsystems website which come in various shapes and sizes. You can choose from optional add-ons for the NetBeans IDE including plugins for C++, PHP, and various other languages. A couple of the packages even include Glassfish, the Sun Application Server for J2EE application development. It's also of note that all of this software is freely available (Yep, no charge!) so go get it! Below is a link to a chart that displays various configurations of Netbeans that you can download.
NetBeans Optional Packages
If you have problems downloading or installing the NetBeans IDE and the JDK please feel free to contact me. There's also a ton of documentation on the Sun website so please remember to read the documentation.
So if you've got the IDE installed...congratulations! You're well on your way to creating Java applications. Let's go ahead and create a simple one. The following steps will lead you through creating your first Java application that simply displays a window. (NOTE: I created this tutorial with NetBeans 6.1 so there may be subtle differences in some of the steps. However, the difference shouldn't be so drastic that you can't figure it out with a little ingenuity.)
Step 1 - Creating the project
a. To create your first project start up the NetBeans IDE.
b. From the main menu select File - New Project.
c. A window will appear asking you to choose the type of project you'd like to create.
d. Under categories select Java.
e. Under projects select Java Application.
f. Click the "Next" button to continue to the next screen.
g. Since we're just trying to create a basic application at this point there's really no need to change any of the options for the project. I plan to go over some of these other options in more depth at a later date. Leave these options at their default value and click "Finish".
h. Congratulations! You just created your first java application! And what exactly will this application to at this point? Well, absolutely nothing!
Step 2 - Creating a Class for our Window
Before starting I would like to clarify exactly what I mean by "Class". When we refer to a class in Java, or pretty much any other object-oriented programming language, a class is like a blueprint from which we create objects. A good analogy is an engineer creating a blueprint for a mold in a factory. From that blueprint the engineer can use a machine (the computer in this case) to create an arbitrary number of objects that are exactly the same. I will elaborate more on classes quite a bit later in this set of tutorials but for now just think of it as a blueprint. Java comes with many pre-defined classes (blueprints) in several different collections and packages. We have the ability to reuse those "blueprints" to create our own "blueprints" that are similar to the original but vary slightly according to our needs. We use the term "inheritance" to describe the act of taking a class and creating another class from it. More on inheritance later...let's get some code out there.
a. From the "File" menu select "New File..."
b. Under categories select Java
c. Under file types select Java Class and click Next
d. We're going to give our class the name "MyWindow" so in the text edit for "Class Name" enter the text "MyWindow"
e. You can leave the rest of the fields at their default values. Click the Finish button.
f. Voila! You have your first class! (Ok, second if you include the default one built that contains the main method)
You should now see that you have two, possibly three tabbed items in the main portion of your NetBeans IDE window with the class you just created being the active one. The tabs across the top should read Main.java, MyWindow.java, and possibly Start Page. We're only interested in the ones that end in .java. You can safely close the Start Page window.
Step 3 - Adding Code to Our Class
Ok, we have a code skeleton for our class that is provided by the IDE. It looks something like this...(I've removed the comments added by the IDE)
package javaapplication8;
public class MyWindow {
}
We need to write code that tells the computer we want to use the Java "JFrame" class as the starting "blueprint" for our class...or more commonly that we want to "inherit from" the JFrame class. Change the code to look like the following...
package javaapplication8;
public class MyWindow extends JFrame {
}
You'll notice in the IDE to the left of the code you just change there is an icon that looks like light bulb with a red dot beside it. If you click that icon you'll see an option to "Add import for javax.swing.JFrame". There is also another option but we'll ignore that one for now. Select the option previous mentioned and you'll see a line appear at the top of your file that looks similar to the following...
import javax.swing.JFrame;
Without going into details, this basically tells the Java compiler that we plan to use the JFrame blueprint somewhere within this file. Without this entry, the Java compiler won't have a clue what a JFrame is or where it resides.
So now we have a blueprint that is like a JFrame (AKA a Window). By default the Windows is not going to have much size so let's give it a size. Modify you're class to look like the following...
public class MyWindow extends JFrame {
MyWindow() {
setSize(MYWIDTH, MYHEIGHT);
}
public static final int MYWIDTH = 200;
public static final int MYHEIGHT = 200;
}
Ok, there's a lot of new stuff going on here now. You can probably figure out some of it without too much difficulty. We added a couple of things to note.
1. We added a "member method" and specifically this method is called a "Constructor". It's the line that starts with "MyWindow()". Between it's brackets are instructions that will be executed for every "MyWindow" that we create. Specifically, we're setting the size of the window to "MYWIDTH" AND "MYHEIGHT" which brings us to the next addition.
2. We added two member variables to our class named MYWIDTH and MYHEIGHT. Every time we create a MyWindow it will have these two variables contained within it. The MyWindow() function can access these variables because it's in the same class as those variables. So, we use those variable to set the window's width and height both to 200.
So at this point we have a fully functioning MyWindow but trying clicking the green arrow pointing to the right at the top of the screen (the run button). Nothing happens except you get some debug output at the bottom of the screen! That's because we haven't told the main method to use the MyFrame class to create any objects from our blueprint. So, let's go do that.
Step 4 - Main and Creating an Object from Our BluePrint
Since the proper way to create and display our windows contains several items that deal with more advanced topics...I'm simply going to post how you should modify the "main" function in the Main.java file to get your application to run...
package javaapplication8;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyWindow window = new MyWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
});
}
}
Now if you click the run button (the green arrow pointing right) you should see a wonderful little 200x200 window appear with a bright, shiny red X at the top right. You can resize, move, and close the window but that's about it. Not very functional, but it's a start! I would encourage your to play around with the code and see what you can do with it. Maybe read some about JFrame's and their capabilities and properties. I would also encourage you to read instructions on the Sun website for downloading and installing the JavaDoc documentation for the J2SE API.
This concludes my basic tutorial for creating your first application with the NetBeans IDE. I hope you found it useful. Please email your comments and suggestions to me using the following link!
Email Chris!
Happy Coding!