You are on page 1of 6

import javax.swing.

JDialog;
import javax.swing.JOptionPane;
public class JOptionPaneTest3 {
public static void main(String[] args) {
JDialog.setDefaultLookAndFeelDecorated(true);
Object[] selectionValues = { "Pandas", "Dogs", "Horses" };
String initialSelection = "Dogs";
Object selection = JOptionPane.showInputDialog(null, "What are your favorite animals?",
"Zoo Quiz", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection)
;
System.out.println(selection);
}
}

With a message pop-up, there's no return value.

import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MessagePopUps {
public static void main(String[] a) {
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Printing complete");
JOptionPane.showInternalMessageDialog(parent, "Printing complete");

//
}
}

The return value signifies which button the user picked. The returned value is one of the integer constants:
YES_OPTION, NO_OPTION, or CANCEL_OPTION

import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ConfirmPopUps {
public static void main(String[] a) {
JFrame frame = new JFrame();
int result = JOptionPane.showConfirmDialog(frame, "Continue printing?");
// JOptionPane.showInternalConfirmDialog(desktop, "Continue printing?");
System.out.println(JOptionPane.CANCEL_OPTION == result);
}
}

The return value is either what the user typed in a text field (a String) or what the user picked from a list of
options (an Object).

import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class InputPopUps {
public static void main(String[] a) {
JFrame frame = new JFrame();
Object result = JOptionPane.showInputDialog(frame, "Enter printer name:");
}
}

System.out.println(result);

import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class BigValueJOptionpaneDialog {
public static void main(String[] a) {
JFrame frame = new JFrame();
String bigList[] = new String[30];
for (int i = 0; i < bigList.length; i++) {
bigList[i] = Integer.toString(i);
}
JOptionPane.showInputDialog(frame, "Pick a printer", "Input", JOptionPane.QUESTION_MESSAGE,
null, bigList, "Titan");
}
}

With the option pop-up, the return value is an int. If the button labels are manually specified with a non-null argument, th
integer represents the selected button position.

import
import
import
import

javax.swing.Icon;
javax.swing.ImageIcon;
javax.swing.JFrame;
javax.swing.JOptionPane;

public class AddingIconsToOptionPopups {

public static void main(String[] a) {


JFrame frame = new JFrame();
Icon greenIcon = new ImageIcon("yourFile.gif");
Icon redIcon = new ImageIcon("");
Object iconArray[] = { greenIcon, redIcon };

JOptionPane.showOptionDialog(frame, "Continue printing?", "Select an Option",


JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, iconArray, iconArray[1]);
}
}

import
import
import
import

javax.swing.Icon;
javax.swing.ImageIcon;
javax.swing.JFrame;
javax.swing.JOptionPane;

public class StringArrayOptionPopups {


public static void main(String[] a) {
JFrame frame = new JFrame();
Icon blueIcon = new ImageIcon("yourFile.gif");
Object stringArray[] = { "Do It", "No Way" };
JOptionPane.showOptionDialog(frame, "Continue printing?", "Select an Option",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, blueIcon, stringArray,
stringArray[0]);
}
}

The message argument not only supports displaying an array of strings, but it also can support an array of any type of
object. If the element is an Icon, the icon is placed within a JLabel,

import
import
import
import
import

javax.swing.ImageIcon;
javax.swing.JButton;
javax.swing.JDialog;
javax.swing.JOptionPane;
javax.swing.JSlider;

public class ComplexMessageDemo {


public static void main(String[] a) {

Object complexMsg[] = { "Above Message", new ImageIcon("yourFile.gif"), new JButton("Hello")


new JSlider(), new ImageIcon("yourFile.gif"), "Below Message" };

JOptionPane optionPane = new JOptionPane();


optionPane.setMessage(complexMsg);
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = optionPane.createDialog(null, "Width 100");
dialog.setVisible(true);

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import
import
import
import

javax.swing.Icon;
javax.swing.ImageIcon;
javax.swing.JButton;
javax.swing.JDialog;
javax.swing.JFrame;
javax.swing.JOptionPane;

public class AddingButtonWithActionListener {


public static void main(String[] a) {
JFrame frame = new JFrame();
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("I got an icon and a text label");

optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
Icon icon = new ImageIcon("yourFile.gif");
JButton jButton = getButton(optionPane, "OK", icon);
optionPane.setOptions(new Object[] { jButton });
JDialog dialog = optionPane.createDialog(frame, "Icon/Text Button");
dialog.setVisible(true);
}
public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) {
final JButton button = new JButton(text, icon);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// Return current text label, instead of argument to method
optionPane.setValue(button.getText());
System.out.println(button.getText());
}
};
button.addActionListener(actionListener);
return button;
}
}