Thursday, January 1, 2009

NetBeans IDE - Your First Program

This is the first post in what I plan to be a series of posts that will cover various aspects of the Java programming language from the ground up. Since I'm still learning Java I'm going to share some of the fundamentals of using the language and basically cover things that I've learned thus far. I've decided the best way to start is with the absolute basics so that anyone will be able to follow along no matter the skill level. After I've compiled many of these posts and increase my knowledge to a level that I feel is Intermediate to Advance I plan to start giving in-person and web seminars on this material. So without further delay...let's begin!

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!

Wednesday, December 31, 2008

New Java Interest

OK, call me a hypocrite or whatever you want...I'm starting to change my mind about the Java programming language. Recently I've made many comments to many people about the inefficiency and poor semantics of the Java programming language but I'm starting to retract those comments the more I learn about Java. I don't think I'm alone in being confused when I first saw code that looks like the following...

public static void main() {
EventQueue.invokeLater(new Runnable() {
void run() {
// Do some stuff here
}
});
}


Java's implementation of anonymous classes and inner-classes are pretty slick and they're starting to grow on me. I'm thinking that I'm going to make a few more basic posts when I run across some better examples. I've got a couple in mind...in particular using the ActionAdapter class when you would rather use an anonymous class for trapping events and you need more than one control to use the event handler (vs. using the Action interface directly which doesn't contain default implemetations of methods).

I'll post more later. I just wanted to get in a quicky before I forgot to post again.

Happy New Year All!

Saturday, July 12, 2008

Changing Gears

Things haven't exactly been easy lately so I haven't had very much time to program at all. My son just had a heart cath. done two days ago but it went smoothly this time so it wasn't much of a problem. We're also planning on moving within the next month so that's taking up a big of my time. To top it off work has been a bitch lately since they don't seem to act like they're much interested in keeping me on the job. In a way it's been good though because it's spurred me on to study new programming languages that are a little more in demand in the current market. The latest one is Java which I've actually programmed in before but never too the time to "really" learn it. So far I've got some opinions...

Pro's:

1. Quick and easy to get a lot done with the right tools. (Netbeans, Eclipse, etc)
2. Easy syntax
3. Well documented and popular on the web

Con's:

1. Handles ALL! variables as references which can be awkward. I'm still trying to keep myself in line with having to "clone" my mutable objects when returning them from my classes.
2. Very dirty in places. Someone at Sun definitely need to go through and clean up some classes/make more field data final, etc.
3. Lots of details to learn. There are a ton of small details and quirks about java that need to be learned. I'm wondering if this is inherent to its design?!

Anyways, I'm going to learn it along with finish the CMDEV II exam over the last part of this year. I'm thinking I'll go ahead and program my latest open-source project (Open IS Trouble Ticketing System - SourceForge) in Java instead of C++ probably using Qt Jambi. We'll have to see how it goes.

Come to think of it I also need to learn to use the MySQL JDBC driver for Java. Maybe that'll be a topic of my next blog.

I'd post another Qt research idea today but I haven't written code for my last question yet so I'll hold off. Maybe in the next day or so :P

L8r all,

Chris

Tuesday, June 24, 2008

Early Linux Software Release Opinions

I don't have a lot of time for this post because I'm on my lunch break but I had to throw this one out there. This comes from a conversation on Qt Forum that just boggled my mind. The conversation was in regards to the Monkey Studio 2.0 release that they just came out with and people are already complaining about bugs in starting the application. I have a few comments about this...

1. It would do the open-source community soooooo much good if even a small percentage of the open-source community would ask themselves one question before releasing to the public
"Is this product production quality?"
Unfortunately the answer to this question is usually no but people push their software out there anyways.

2. Another question I have is how in the world would anyone expect to market a product like Monkey Studio for Qt when the Eclipse IDE with the Qt4 plugin is everything and more that you could possibly want? I've been using it for several months now and with the latest Java I have no complaints. Even with jre6 update 6 the program was tolerable (the code completion was a bit slow). With jre6 update 10 the code completion is instantaneous on my system.

Ok, I gotta cut this short...ran out of time.

Chris

Wednesday, June 18, 2008

Sourceforge Open Source Project

Well, I've been struggling with ideas for a project to work on because, quite frankly, I'm extremely bored with what I'm working on at work. There's something about the monotony of seeing EDI all day that really doesn't do much for me.

So anyway, a while back I submitted an idea for an open source trouble ticketing system written in none other than Qt4/C++. The idea is to have a distributed client application that is cross-platform and will connect back to a MySQL database. This idea actually came from work since we didn't have a ticket system at the time...and really the one we have now is pretty "hokey!" I'm just now getting into the design stages of this project again so maybe I'll post the MySQL table creation scripts and some of my other design ideas after I get a chance to work them over.

I also had another idea that I'm just going to put out there for anyone who might be reading this and is interested in learning more about Qt4 and C++. I'm going to post a learning challenge each time I make a post for the reader to look up and/or learn about online in his/her spare time. I believe it's extemely important to constantly learn about new technologies to stay sharp and ahead of the curve. So, without further delay...

Research Challenge #1
One of the most basic and fundamental objects in the Qt library is the QApplication object. Start off by reading the basic documentation for the QApplication object. Then challenge yourself to use at least 5 member functions in some sample code (asigned from QApplication::quit() and it's constructor!) to see what you can come up with. Some ideas might be using it to retrieve a complete list of widgets in the current application or experiementing with how the system handles keyboard input. Feel free to post comments and even code if you come up with something you didn't know before.

Happy Coding!

Chris

Sunday, June 15, 2008

Introduction

Hello All!

Well, I'm new to this blogging thing but I needed a place to get my comments and ideas on programming out of my head so this is where I ended up. I'm not even sure anyone will read this thing but just in case you have that much time to waste here's a bit about me.

First and foremost, I'm dad and husband. I have two awesome children (Adrianna Lee 6 f , Carter Isaac 1 m) and a wonderful wife that somehow manages to keep them under control. She's the reason I have enough quiet time to do this in the first place.

Secondly, I'm a programmer. It's my job and quite frankly what I love to do whenever I get time away from the kids (which can be a challenge in itself!). On the job I'm a C++/Python programmer developing cross-platform EDI applications with Qt3, Qt4 and PyQT. I've recently become a huge fan of the Eclipse IDE with the latest Qt4 extensions. I'm kind of impartial to the C++ programming language and really believe that Python is for the idiots and lazy people that won't take the time to learn how a real programming language works. It makes no sense to me that you would create an entirely new language that only takes features away from another language. If you've programmed in both languages then you know what I mean.

I also have a strong background in network administration with an emphasis on Cisco equipment. My latest obsession has become database administration, specifically the MySQL database. I guess the administration of the database isn't really my interest. The thing that got my attention the most is the MySQL API. I'm also trying to learn the MySQL++ API which is really just a C++ wrapper for the old MySQL API.

Last but not least I just took the Certified MySQL Developer I test and passed with a 53 (needed 43 to pass!) I was pretty pleased with that and am hoping to take the CMDEV-II test within the next couple of months.

My other opinions include the following...
1. Microsoft Sucks! Ok, they don't suck completely. Some of their products actually have good functionality. Their entire business philosophy just completely pisses me off though. You'd think they could find something constructive to do with all that dough laying around. The way they mock the Open Source movement by pretending to give some things to the community really irritates me.
2. Open Source is fantastic! Donate all the time you can to help people and open source projects. This is for the benefit of everyone! It's also a great way to get your name out there. I know I try to help anyone out that has questions for me so please do the same for others. I try to pay a visit or two a week to http://www.qtforum.org to try to answer a few questions (username bolt18_80) so check that place out if you have qt questions.
3. Linux is the way of the future. All it's going to take is a few more empty niches in the applications community to get filled before companies stop paying Micro$uck to suck their blood. The gaming market is also lacking on Linux. It's going to take everyone to get there but I really believe it will happen (10 years maybe??)

That about it about me. I'm hoping to make a post once or twice a week pertaining to programming and projects that I'm working on. Maybe even discuss some code snippets and/or design ideas. I'm not sure if this thing allows comments from other users but if it does please do so. I'd love to swap ideas with other programmers.

L8r!

C. S.