Java for Newbies - part 1

Introduction

Hello, and welcome to my Java for Newbies series. I will follow the same approach writing this series as I do with my Mach-II for Newbies series. For you not familiar with it, the approach is this:

I write as i learn and learn as I write.

This means that I have no Java experience on my back. I just document my own learning journey so that (hopefully) helping others going the same path.

However I hope some of the more Java savvy people out there will help me out on the road. I will have questions, and I will probably get lost at some time.

My first personal goal is to be able to make simple desktop applications. Like a password generator, a simple address directory and such. So I will pretty much jump into Swing (user interface) development, instead of the typical faceless console approach most tutorials out there take.

I'm using Eclipse for my development, and will write how I'm doing things in that environment.

Preparing.

In Eclipse, go to File > New > Project. In the New Project pop-up you'll now be asked to select a wizard. Select the "Java Project" wizard. In the first step enter the project name "JavaNewbie". Under Contents keep "Create new project in workspace" selected, and under Project layout keep "Use project folder as root for sources and class files" selected. Click "Finish". I get a box saying that "The created projects appears to be 5.0, but the project compliance is not yet set to 5.0. Blah, blah...." I don't know (yet) what this means, so I'll just keep the default answer and click "Yes".

In the Package Explorer you will now have your new project. Right-click the project name and choose New > Class. In the New Java Class box fill in the name "FirstJava". In the top you'll get a warning saying "The use of the default package is discouraged", but I'm not sure what that means either, so I just click "Finish".

You will now see the code, looking like this:

public class FirstJava {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 }
}

To make sure things are working change the code into this:

public class FirstJava {
 public static void main(String[] args) {
  System.out.println("Hello World");
 }
}

Save, and click the "Run" button. You should now get a "Run" dialouge box. You might have to click the "New" button. There's a lot of stuff here. Just be sure that the Name and Project is "JavaNewbie" and Main class is "FirstJava". In the Console part at the bottom of the Eclipse workspace you should now see the "Hello World" output.

If to compare this with a ColdFusion cfc, the "public class FirstJava" is can roughly be translated into <cfcomponent displayname="FirstJava"> and "public static void main(String[] args)" can roughly be translated to <cffunction name="main" access="public" returntype="void">

Creating a GUI

We need to import the main Swing package so at the very top put this line:

import javax.swing.*;

Now we will make a function for creating and showing our very first GUI. Put the following above the main function.

private static void createAndShowGUI() {

}

Now within the braces of our new function we will put some lines:

//Create and set up the window.
JFrame frame = new JFrame("My first swing");

//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);

//Display the window.
frame.pack();
frame.setVisible(true);

And last change change the

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

for this ones

//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
 public void run() {
         createAndShowGUI();
        }
});

Ok, what happens here is that we create a top-level container as an instance of the class JFrame. In this case a frame is a window. Applications with GUI's would use at least one frame.

We then add a lable initiating an instance of the class JLabel - and then call a function for adding it to our frame.

The frame.pack() and frame.setVisisble() functions finalize the code to set up and show the frame/window.

The last piece of code I found provided as a recomended "use as-is" code for calling the window creating function. We don't need to understand it, just to know that it ensures that we don't have a thread-safety problem.

The final code should look like this:

import javax.swing.*;

public class FirstJava {
 
 private static void createAndShowGUI() {

        //Create and set up the window.
        JFrame frame = new JFrame("My first swing");

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
 public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Try to run it now. You should get a window popping up looking like this:

Resizing it slightly to see all text like this:

Comments
You're up to the same thing I am.. I've been learning Java myself, but have been focusing on comparing the differences between CFML and Java with particular concepts (database access, array of "structures", etc).

http://dangermoose.blogspot.com/
# Posted By Darryl Lyons | 5/13/05 10:24 AM
Thank you Darryl. I'll for sure take a closer look at what you have done once I get a bit further in my Java.
# Posted By Trond Ulseth | 5/14/05 12:58 PM
please keep this up! I am your follower.

http://kaplan.ilteris.free.fr/blog/
# Posted By ilteris | 5/17/05 5:15 AM
I, myself also have no serious development experience with Java except for some integration stuff. However, I think the best, and the easiest way to learn J2EE application development is to work with AppFuse:- https://appfuse.dev.java.net/
# Posted By Vui Lo | 5/17/05 10:03 AM
For those of you that might have subscribed, I just wanted to notify that part 2 is now posted.
# Posted By Trond Ulseth | 5/18/05 11:16 PM