Getting Started With Java
Written by Mike James   
Article Index
Getting Started With Java
Hello Swing
Code

Here we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

Modern Java
With NetBeans And Swing

largecover

Contents

  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events

 

This tutorial is aimed at the Java beginner and the complete beginner to programming.

If you can already program in another language and are wanting to learn Java then there are some very introductory sections that you can skip. 

If you want to know more about why NetBeans and Swing were selected then read the next section. If you simply want to get started then skip this too.

Where to start

Getting started with Java is difficult - more difficult than it needs to be. The reason is that there are so many different ways of writing a Java program.

This is confusing for the beginner.

One solution is to strip the process down to its bare essentials. This is why many introductions to Java start with programs written using NotePad or some other simple text editor and compiled using the command line.

This is at least a common baseline but it is very basic indeed. Modern Java programming isn't done from the command line and the typical Java application doesn't target the console. 

To be reasonably up-to-date we have to start as we mean to go on - using an IDE (Integrated Development Environment) and creating GUI (Graphical User Interface) applications. 

Now we hit the first snag - which IDE and which GUI Framework?

Most Java programmer have their preferred way of creating a Java program.  Eclipse and NetBeans are two very popular IDEs that Java programmers tend to swear by. I'm not going to spend time considering the merits of the two for the simple reason that one of these IDEs is much more suited to the beginner. No matter what IDE you end up using in the future NetBeans is much easier to use to get started. A simplified configuration can be downloaded and installed in one operation and it supports a drag-and-drop GUI designer without the need to find and install anything extra. In other words, it provides a modern approach to Java development in one download. 

Selecting NetBeans also solves the problem of which UI framework to use. It supports the Swing library out of the box and, as this is currently the most popular way of building a user interface, it makes sense to accept the situation. There are more modern UI Frameworks that you can use with Java - SWT, Pivot and JavaFX for example - but these have their drawbacks for the beginner. Once you have seen Swing in action then transferring your skills to another UI framework is comparatively easy. 

So starting with the most direct and easy-to-use route to modern Java application development brings us to NetBeans and Swing.

Getting Started

You first task is to set up a development environment. This is remarkably easy. First you need to install the JDK and then a suitable copy of NetBeans.

To download and install the JDK go to:

https://www.oracle.com/technetwork/java/javase/downloads/index.html

You need to select, download and run the JDK 8 - Java SE 8u221 a the time of writing. Select the platform of your choice and run the download. It is simplest to accept the defaults.  Don't be tempted to use version 9,10, 11 or 12 as these don't work with NetBeans as yet.

To download and install NetBeans go to:

http://NetBeans.org/ 

and click the download button. You will next be presented with a selection of different NetBeans. In general download the latest - 11.1 at the time of writing.

If you don't have the JDK installed then you will see an error message and an instruction to install it.

Again accept the default installation options.

If anything goes wrong your first attempt to make it work should be to uninstall and try again. 

Starting A Project

Now that you have Java and NetBeans

installed it is time to write a first program.

As with all first programs this will be as simple as possible just to make sure everything is installed and working. However, instead of creating a console based application, which is arguably the simplest possible Java program, we are going to start with the simplest Swing application.

So start NetBeans running.

To get started we first have to create a project. A project consists of all of the files and resources needed to create an application.NetBeans can create a number of different types of projects to help get you started - we need the simplest project supported. 

To create a new project we use the menu command

File, New Project 

new

 

You will see the New Project dialog box listing all of the possible initial project types - select 

Java with Maven

    Java Application

and click the Next button. Don't worry for the moment what Maven is. If this is the first time you have used NetBeans you will also have to download Java SE support - you only have to do this once.

In the next dialog box give the project the name HelloSwing. You can ignore the other entries in the dialog box - they are all useful but not when you are just getting started. In this case just click Finish and wait while NetBeans creates a project structure for you:

proj

 

 

 

<ASIN:0596009208>

<ASIN:0132354764>

<ASIN:1849511764>