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

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:

import 

 

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.

run

 

You only have to do this once, if you select Remember Permanently,  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. 

 

hello

 

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

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

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

 

<ASIN:0072263148>