Saturday, July 30, 2011

How to create a dialog-based program in Java?

by Vilchor G. Perdido (eturo)


First in our code is to import the package in the Java API where the JOptionPane is. And it is found at the javax.swing package, so will create a code to import this package. It look like this...

import javax.swing.JOptionPane;
or you may write the code above like this;

import javax.swing.*; //note that the asterisk (*) indicate that all the classes and methods inside this package will be automatically used or include by the program


Second, create a code to define the class name (Note: The class name you entered a while ago is always the same the class name you will write in the code.) Below the class declaration, put a paired of open and close curly bracket where we will put the variables and methods later to be used by our program. The whole class is denoted by the light blue color in the code pad in the BlueJ text editor.

import javax.swing.JOptionPane;
public class MyFirstGUIProgram
     your code here
}

 
Third, after defining your class, the next step is to create the main method to be able to execute by the program. Whatever statements you declared inside the main method will be executed. The main method of our program will be indicated by the light yellow color the code pad window. To create the main method of our program, do this inside the class:


import javax.swing.JOptionPane; //package declaration


public class MyFirstGUIProgram //class definition

{

     public static void main(String[] args) //the main method
     {
         your code here
     }
}


Now, we are ready to create the statement that will display a dialog box with a message. To do this, we have the following syntax in creating a message dialog box.

JOptionPane.showMessageDialog(null,"your message here","text in the title bar", type of dialog box ,);

Explanation:

 


So, in our code:





import javax.swing.JOptionPane; //package declaration


public class MyFirstGUIProgram //class definition

{

  public static void main(String[] args) //the main method

  {

    //this will display a message dialog box

    JOptionPane.showMessageDialog(null,"Welcome to Java Program!", "Message",JOptioPane.INFORMATION_MESSAGE);



  }//end of main

}//end of class

Now, to compile the program, just click the Compile button at the upper left pane of the code pad window, then the status bar at the bottom will display a message whether there are errors (bugs) of the program or none. So, if there are no errors, you are ready to run the program to view the result.

In running the program, go back to the main window of BlueJ, then do right click on the class we wan to run, then select or choose "void main(String[] args)" option.

A dialog box will prompt then just click the OK button to run the program. Then immediately the output of the program will appear or display. See the image below.




There you have it. You made your first GUI-based Java program using the JOptionPane class. Congratulations! If you wan to learn more about Java programming, stay tuned in this blog site. There are a lot more tutorials I will posted soon. Happy coding guys!(et

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...