Getting started with Java
Getting started with Java
Written by Ian Elliot   
Thursday, 31 March 2016
Article Index
Getting started with Java
Hello Swing


Adding a JFrame

When you click Finish an initial default project is created for you. You can see its structure in the Projects window:



The only folder that matters at this stage is the Source Packages folder. This is where all of the files containing Java that you create will be stored.

In this instance we need to create a file to start writing some Java.

Use the:

File,New File

menu command.



The dialog box that appears gives you a choice of a range of different types of file you can create. In this case we need to create a basic Swing file so select Swing GUI forms and then select JFrame Form.

A JFrame is the basic display surface that you can place buttons, textboxes and so on to create a user interface so it is often the starting point for an application. When you click the next button you are given an opportunity to give the class a name and customise it.

For the moment simply accept the defaults and click Finished.

The new JFrame file is created for you and placed in the project within the default package - more about packages later.

You will also see the designer showing you a blank grey area which represents the JFrame. You can also see two tabs - Source and Design. 

If you click the Source tab you can see the code that has been generated to create the JFrame.This code will make sense to you in a very short time working with it but for now ignore it. 

You will learn a little about Java from this example but keep in mind that its main objective is to take you though the steps of creating and running a program so that you know everything is working and what to do. 

Adding a button



If you click the Design tab you can use the Palette (top right) to place user interface objects on the JFrame.  

Scroll down in the Pallet until you find a Button -- under the Swing Controls section -- and drag-and-drop the button onto the surface of the JFrame in the designer.



You can now position and size the button by dragging it to where you want it or dragging the sizing handles.  

If you want to see what your user interface looks like, before you have added any code, when the program is run you can click the Preview icon.



To make this our first program in Java we really do have to enter some Java instructions.

Double click on the button and you will automatically generate a click event handler - i.e. a block of Java code that is obeyed in response to clicking the button.


The code is generated in among a lot of other generated code - try not to be too worried about the rest of the generated code and try not to accidentally change it. 

The default handler that NetBeans generates for you doesn't do anything at all: 

private void jButton1ActionPerformed(
             java.awt.event.ActionEvent evt) {
// TODO add your handling code here:


As you can see there is a comment line inviting you to add your own code that will be run when the button is clicked. 

Adding Some Code

To make something happen when the button is clicked we can make use of the JOptionPane object which has a range of methods that can be used to make message boxes pop up. 

There are lots of objects supplied by the Java framework and they all have names like JOptionPane that usually only make sense when you know what the object is for. 

Before we can use JOptionPane we have to tell the compiler that we are intending to use it and you need to add:

import javax.swing.JOptionPane;

to the start of the file. To do this click the Source tab - if you can't already see the code - and scroll to the very top of the file. Type or copy and paste the line in before the line starting public class NewJFrame:



With the import statement in place we can now write a line of code that makes a message box pop up.

Edit the click event handler to read

private void jButton1ActionPerformed(
                   java.awt.event.ActionEvent evt) {
      "Hello Swing World",

Don't worry too much about the actual instruction but if you read it then it should make reasonable sense.

The "Hello Swing World" appears as the message and "Hello" as the window title. The JOptionPane.INFOMATION_MESSAGE simply sets the type of pop up box. You may also notice that as you type NetBeans attempts to help you by showing you possible completions of what you are typing, by adding quotes automatically and generally trying to second guess and provide information. The easiest way to find out how all this works is to simply try it out. 

If you want to run the program as it is then click the Run icon.




The first time you run the program you will be asked to set the "Main Class" this is just the where the project starts executing code in this case set it to NewJFrame our JFrame class.



You only have to do this once but you can change a project's main class anytime you need to.

When you run the project you should see the JFrame complete with button and when you click the button the Hello Swing World message box should pop up. 




Now that you have created and run your first Swing Java application it is time to move on and learn some Java.

Which is the subject of the next part.

Modern Java
With NetBeans And Swing



  1. Getting started with Java

    In chapter 1 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.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Java Data Types - Numeric Data 
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

  9. Java Data Types - Arrays And Strings
  10. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 
  11. Advanced OOP - Type, Casting, Packages
  12. Value And Reference 
  13. Java Lambdas, SAMs And Events







or email your comment to:





Last Updated ( Thursday, 09 June 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.