
-----------------------------------
Paul
Wed Feb 04, 2004 9:35 pm

[Tutorial] JOptionPane: easy input, output with dialog boxes
-----------------------------------
JOptionPane is a easy way to do dialog boxes, messages or inputs.
You have to import it at the beginning of the program:

import javax.swing.JOptionPane;


The arguments for JOptionPane:

for a simple message box:

JOptionPane.showMessageDialog (
   null, "Put your message here" );

to show text in different rows:

JOptionPane.showMessageDialog (
   null, "Put \nyour \nmessage \nhere" );

the \n switches to next line



Sample program:


//A sample program. Sample.java
import javax.swing.JOptionPane; // imports JOptionPane class

public class Sample {
public static void main ( String args


To show different messages with different icons, you'll need 4 arguments instead of 2.


JOptionPane.showMessageDialog (
null, "Messagehere", "Title here", JOptionPane.PLAIN_MESSAGE);


There are different kinds of icons for a dialog box, just replace the last argument:

JOptionPane.PLAIN_MESSAGE // this is a plain message
JOptionPane.INFORMATION_MESSAGE // this is a info message
JOptionPane.ERROR_MESSAGE // this is a error message
JOptionPane.WARNING_MESSAGE // this is a warning message



You can also use JOptionPane for dialog boxes for input, and assign them to variables, just put:

name=   // The variable
JOptionPane.showInputDialog ( "put your message here" );


note:  after using JOptionPane, dont forget to exit System.exit

Sample Program:

// Addition.java
// An addition program

import javax.swing.JOptionPane; //import class JOptionPane

public class Addition {
public static void main ( String args

NOTE:
JOptionPane can ONLY be used to input string, so if you want to use numbers, you have to create separate variables to change it into integers, or real numbers.

PS: I'll add more if needed, if you don't know the parse.Int part, thats ok, as long as you get the main idea of JOptionPane. These may not work 100%, feed back pls.[/b]

-----------------------------------
Tony
Wed Feb 04, 2004 11:03 pm


-----------------------------------
nice :D And this is soo much simpler then setting up input buffers and exseption handlers for basic cin>> :lol: Might be using this on CCC if they'll have any questions with manual input :D 

+50Bits

-----------------------------------
the_short1
Fri Apr 08, 2005 1:39 pm


-----------------------------------
very nice.. i already learned this. .but its a very good tut. . heres 5bits :d
