Getting started with Java
Written by Ian Elliot   
Monday, 27 September 2010
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:

projects 

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

command.

newfile

 

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.

Adding a button

designer

 

If you click Design you can use the Palette (top right) to place user interface object 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.

button1

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

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 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. 

If you want to see what your user interface looks like when the program is run you can click the Preview icon.

previewicon

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. 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. 

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) {
JOptionPane.showMessageDialog(
      this,
      "Hello Swing World",
      "Hello",
      JOptionPane.INFORMATION_MESSAGE,
      null
);
}

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.

runicon

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. 

run

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



If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter

 

<ASIN:0072263148>



Last Updated ( Wednesday, 07 November 2012 )
 
 

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