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);
}
}
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;
import
import
import
import
javax.swing.Icon;
javax.swing.ImageIcon;
javax.swing.JFrame;
javax.swing.JOptionPane;
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;
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;
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;
}
}
Much more than documents.
Discover everything Scribd has to offer, including books and audiobooks from major publishers.
Cancel anytime.