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 Contents

  1. Getting started with Java
  2. Introducing Java - Swing Objects
  3. Writing Java Code - Methods
  4. Command Line Programs
  5. The Java User Interface - More Swing
  6. Working With Class
  7. Class Inheritance
  8. Building a Java GUI - Containers

blog comments powered by Disqus 




Last Updated ( Thursday, 31 March 2016 )

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