Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Creating a JOptionPane

A JOptionPane object represents a dialog box for several purposes: Display a message (through the use of the showMessageDialog method) Ask for user's confirmation (using the showConfirmDialog method) Obtain the user's input (using the showInputDialog method) Do the combined three above (using the showOptionDialog method) public JOptionPane() JOptionPane optionPane = new JOptionPane(); public JOptionPane(Object message) JOptionPane optionPane = new JOptionPane("Message"); public JOptionPane(Object message, int messageType) JOptionPane optionPane = new JOptionPane("Message",  JOptionPane.WARNING_MESSAGE); public JOptionPane(Object message, int messageType, int optionType) JOptionPane optionPane = new JOptionPane("Question?",  JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); public JOptionPane(Object message, int messageType, int optionType,  Icon icon) Icon printerIcon = new ImageIcon("yourFile.gif"); JOptionPane optionPane = new JOptionPane("Question?",  JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, printerIcon); public JOptionPane(Object message, int messageType, int optionType, Icon icon,  Object options[ ]) Icon greenIcon = new DiamondIcon(Color.GREEN); Icon redIcon = new DiamondIcon(Color.RED); Object optionArray[] = new Object[] { greenIcon, redIcon} ; JOptionPane optionPane = new JOptionPane("Question?",  JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, printerIcon,  optionArray); public JOptionPane(Object message, int messageType, int optionType, Icon icon, Object options[], Object initialValue) JOptionPane optionPane = new JOptionPane("Question?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, printerIcon, optionArray, redIcon); The panel presents content in four areas. All the areas are optional Icon Area: Display an Icon to indicate the type of message. The installed look and feel provides default icons for certain types of messages. You can provide your own icon. Message Area: Display a text message. Input Area: Allow users to provide a response to a message. Button Area: For getting user input. Selection of a button signals the end of the usage of the JOptionPane. Property Constants: ICON_PROPERTY INITIAL_SELECTION_VALUE_PROPERTY INITIAL_VALUE_PROPERTY INPUT_VALUE_PROPERTY MESSAGE_PROPERTY MESSAGE_TYPE_PROPERTY OPTION_TYPE_PROPERTY OPTIONS_PROPERTY SELECTION_VALUES_PROPERTY VALUE_PROPERTY WANTS_INPUT_PROPERTY