Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
na Example of how to use the JOptionPane to make GUIs.
import javax.swing.JOptionPane;
/*
///-_-_- JOPTIONPANE TUTORIAL -_-_-\\\
Hello this is my very first programming tutorial, what is up, this is a tutorial on how to use JOptionPane!
up to now you have been using System.out.print() to output messages & you've been using java.util.Scanner
to receive input but now I'll tell you how to use JOptionPane's showMessageDialog() & showInputDialog() methods.
JOptionPane programs are little pop up windows that either have a dialog box that has a message and asks for input that is
the showInputDialog() the other is showMessageDialog, it is just a little message with a Icon if you want, both pop up window
types can have a title & both need to have null for the container usually like probably too early for you for me to talk about that.
JOptionPane Programs are a lot more easy to use and to work with than the console type programs you have made so far,
you will have to import javax.swing.JOptionPane; to use JOptionPane and it's methods.
///-_-_- JOPTIONPANE TUTORIAL -_-_-\\\
*/
public class JOpExample {
public static void main(String args[])
{
//OK first up is the showInputDialog() it gives prompt and returns user input
String input = JOptionPane.showInputDialog(null, "what up", "text box prompt");//Equivalent to java.utilScanner but shows a message, & a title
String titleLess = JOptionPane.showInputDialog(null, "no prompt!");//you don't need the prompt but you need the null at the start
//showInputDialog and showInputDialog both require the null at the beginning
if(input == null)System.exit(0);//if the input ends up being null that means you pressed cancel so adding System.exit(0); will close the program
//what ever string that is receiving input needs this afterward so that the cancel button will always work right
//and only closing the program sometimes it will close the program all the time if you add this
//if you want your input to be a number you will have to parse a String, because showInputDialog only works with Strings
//example...
int numInput = Integer.parseInt(JOptionPane.showInputDialog(null, "enter an integer", "numInput"));//I use this parse method the most
double decInput = Double.parseDouble(JOptionPane.showInputDialog(null, "enter a double", "decInput"));//I use this the 2nd most
float floInput = Float.parseFloat(JOptionPane.showInputDialog(null, "enter a float", "floInput"));//I've seen people use floats to save memory
long longInput = Long.parseLong(JOptionPane.showInputDialog(null, "enter a long", "longInput"));//I use sometimes for really big nums
byte byteInput = Byte.parseByte(JOptionPane.showInputDialog(null, "enter a byte", "byteInput"));//I haven't really used byte much
//if you parse in a non-number an exception will be thrown a NumberFormatExcption Exception
//you can use a try and catch block so that you can decide what to do when someone inputs either a non number or the wrong type
//example...
try
{
int safeInput = Integer.parseInt(JOptionPane.showInputDialog(null, "enter an integer or whatever, who cares", "safeInput"));
}
catch(NumberFormatException e)
{
//if the the inputDialog's input in this case is not a integer then a NumberFormatException will be thrown and caught by this catch
// you can have nothing, return; or System.exit(0); or main(null); or whatever
}
//OK what if you just want to out put some information? showMessageDialog();
//example...
JOptionPane.showMessageDialog(null, "I'm a message");//just a simple message
JOptionPane.showMessageDialog(null, "I'm a message", "I got a title", JOptionPane.PLAIN_MESSAGE);//message with title
//the regular icon for your Message Dialog is JOptionPane.Plain_Message but you usually don't need it
//showMessageDialog have icon messages like a warning message or a question message to indicate the type of information you are displaying
//JOptionPane.ERROR_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.QUESTION_MESSAGE JOptionPane.WARNING_MESSAGE
//icons aren't that important but they can convey a little extra information than just a PLAIN_MESSAGE can
//example...
JOptionPane.showMessageDialog(null, "I'm an error message", "", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(null, "I'm an informative message", "", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "I'm a question message", "", JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null, "I'm a warning message", "", JOptionPane.WARNING_MESSAGE);
//you don't need to import JOptionPane to use JOptionPane
//example...
javax.swing.JOptionPane.showMessageDialog(null, "I'm not imported");
javax.swing.JOptionPane.showInputDialog(null, "non-Imported input dialog");
//compact non-imported parsed input dialog
//example...
double wow;
String text;
try {wow = Double.parseDouble(text = javax.swing.JOptionPane.showInputDialog(null, "non-imported parsed input dialog", "sup"));
if(text == null) System.exit(0);}//makes cancel work
catch(NumberFormatException e){return;}//makes sure you input a number
//you can also have your class extend JOptionPane for extra compactness
//example...
new ExtendsExample();
}
static class ExtendsExample extends JOptionPane
{
public ExtendsExample()
{
//instead of JOptionPane.showInputDialog(null, "long input"); you can have
showInputDialog(null, "short input");
//instead of JOptionPane.showMessageDialog(null, "long message"); you can have
showMessageDialog(null, "short message");
//you also don't need the JOptioPane in front of the ERROR_MESSAGE part
showMessageDialog(null, "short error message", "no JOptionPane.", ERROR_MESSAGE);
//in conclusion you don't need to write out JOptionPane all the time if you are in a class that is a subclass of JOptionPane
//you can make methods so that you don't need to write out as much the methods Input() and Msg() that I'm calling are down
//below & they shorten are simply the making of JOptionPane Windows
//example...
String input = Input("tiny input");
input = Input("I got a", "message in the text box");
Msg("tiny message");
Msg("tiny message", "with a title");
//message dialog with icon
Msg("tiny plain message", "with title", 0);//plain
Msg("tiny error message", "with title", 1);//error
Msg("tiny info message", "with title", 2);//info
Msg("tiny question message", "with title", 3);//question
Msg("tiny warning message", "with title", 4);//warning
}
////short little return statements with parameters////
public String Input(String msg)
{
return showInputDialog(null, msg);
}
public String Input(String msg, String start)
{
return showInputDialog(null, msg, start);
}
public void Msg(String msg)
{
showMessageDialog(null, msg);
}
public void Msg(String msg, String title)
{
showMessageDialog(null, msg, title, PLAIN_MESSAGE);
}
public void Msg(String msg, String title, int icon)
{
if(icon == 0)showMessageDialog(null, msg, title, PLAIN_MESSAGE);
else if(icon == 1)showMessageDialog(null, msg, title, ERROR_MESSAGE);
else if(icon == 2)showMessageDialog(null, msg, title, INFORMATION_MESSAGE);
else if(icon == 3)showMessageDialog(null, msg, title, QUESTION_MESSAGE);
else if(icon == 4)showMessageDialog(null, msg, title, WARNING_MESSAGE);
}
////short little return statements with parameters////
//OK as you may notice you can have a humble JOptionPane message box written out like this:
//javax.swing.JOptionPane.showMessage(null, "sup", "title"); or...
//JOptionPane.showMessage(null, "sup", "title"); or...
//showMessage(null, "sup", "title"); or even...
//Msg("sup", "title");
}
}
//that's JOptionPane, it's not that fancy but it is definitely better than the console.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.