Swing JOptionPane Class

Advertisements


Introduction

The class JOptionPane is a component which provides standard methods to pop up a standard dialog box for a value or informs user of something.

Class declaration

Following is the declaration for javax.swing.JOptionPane class:

public class JOptionPane
   extends JComponent
      implements Accessible

Field

Following are the fields for javax.swing.JOptionPane class:

  • static int CANCEL_OPTION --Return value from class method if CANCEL is chosen.

  • static int CLOSED_OPTION --Return value from class method if user closes window without selecting anything, more than likely this should be treated as either a CANCEL_OPTION or NO_OPTION.

  • static int DEFAULT_OPTION --Type meaning Look and Feel should not supply any options -- only use the options from the JOptionPane.

  • static int ERROR_MESSAGE --Used for error messages.

  • protected Icon icon --Icon used in pane.

  • static string ICON_PROPERTY --Bound property name for icon.

  • static int INFORMATION_MESSAGE --Used for information messages.

  • static string INITIAL_SELECTION_VALUE_PROPERTY --Bound property name for initialSelectionValue.

  • static string INITIAL_VALUE_PROPERTY --Bound property name for initialValue.

  • protected Object initialSelectionValue --Initial value to select in selectionValues.

  • protected Object initialValue --Value that should be initially selected in options.

  • static string INPUT_VALUE_PROPERTY --Bound property name for inputValue.

  • protected Object inputValue --Value the user has input.

  • protected Object message --Message to display.

  • static string MESSAGE_PROPERTY --Bound property name for message.

  • static string MESSAGE_TYPE_PROPERTY --Bound property name for type.

  • protected int messageType --Message type.

  • static int NO_OPTION --Return value from class method if NO is chosen.

  • static int OK_CANCEL_OPTION --Type used for showConfirmDialog.

  • static int OK_OPTION --Return value form class method if OK is chosen.

  • static string OPTION_TYPE_PROPERTY --Bound property name for optionType.

  • protected Object[] options --Options to display to the user.

  • static string OPTIONS_PROPERTY --Bound property name for option.

  • protected int optionType --Option type, one of DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION.

  • static int PLAIN_MESSAGE --No icon is used.

  • static int QUESTION_MESSAGE --Used for questions.

  • static string SELECTION_VALUES_PROPERTY --Bound property name for selectionValues.

  • protected Object[] selectionValues --Array of values the user can choose from.

  • static Object UNINITIALIZED_VALUE --Indicates that the user has not yet selected a value.

  • protected Object value --Currently selected value, will be a valid option, or UNINITIALIZED_VALUE or null.

  • static string VALUE_PROPERTY --Bound property name for value.

  • static string WANTS_INPUT_PROPERTY --Bound property name for wantsInput.

  • protected boolean wantsInput --If true, a UI widget will be provided to the user to get input.

  • static int WARNING_MESSAGE --Used for warning messages.

  • static int YES_NO_CANCEL_OPTION --Type used for showConfirmDialog.

  • static int YES_NO_OPTION --Type used for showConfirmDialog.

  • static int YES_OPTION --Return value from class method if YES is chosen.

Class constructors

S.N.Constructor & Description
1JOptionPane()
Creates a JOptionPane with a test message.
2JOptionPane(Object message)
Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI.
3JOptionPane(Object message, int messageType)
Creates an instance of JOptionPane to display a message with the specified message type and the default options
4JOptionPane(Object message, int messageType, int optionType)
Creates an instance of JOptionPane to display a message with the specified message type and options.
5JOptionPane(Object message, int messageType, int optionType, Icon icon)
Creates an instance of JOptionPane to display a message with the specified message type, options, and icon.
6JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options)
Creates an instance of JOptionPane to display a message with the specified message type, icon, and options.
7JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)
Creates an instance of JOptionPane to display a message with the specified message type, icon, and options, with the initially-selected option specified.

Class methods

S.N.Method & Description
1JDialog createDialog(Component parentComponent, String title)
Creates and returns a new JDialog wrapping this centered on the parentComponent in the parentComponent's frame.
2JDialog createDialog(String title)
Creates and returns a new parentless JDialog with the specified title.
3JInternalFrame createInternalFrame(Component parentComponent, String title)
Creates and returns an instance of JInternalFrame.
4AccessibleContext getAccessibleContext()
Returns the AccessibleContext associated with this JOptionPane.
5static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
Returns the specified component's desktop pane.
6static Frame getFrameForComponent(Component parentComponent)
Returns the specified component's Frame.
7Icon getIcon()
Returns the icon this pane displays.
8Object getInitialSelectionValue()
Returns the input value that is displayed as initially selected to the user.
9Object getInitialValue()
Returns the initial value.
10Object getInputValue()
Returns the value the user has input, if wantsInput is true.
11int getMaxCharactersPerLineCount()
Returns the maximum number of characters to place on a line in a message.
12Object getMessage()
Returns the message-object this pane displays.
13int getMessageType()
Returns the message type.
14Object[] getOptions()
Returns the choices the user can make.
15int getOptionType()
Returns the type of options that are displayed.
16static Frame getRootFrame()
Returns the Frame to use for the class methods in which a frame is not provided.
17Object[] getSelectionValues()
Returns the input selection values.
18OptionPaneUI getUI()
Returns the UI object which implements the L&F for this component.
19String getUIClassID()
Returns the name of the UI class that implements the L&F for this component.
20Object getValue()
Returns the value the user has selected.
21boolean getWantsInput()
Returns the value of the wantsInput property.
22protected String paramString()
Returns a string representation of this JOptionPane.
23void selectInitialValue()
Requests that the initial value be selected, which will set focus to the initial value.
24void setIcon(Icon newIcon)
Sets the icon to display.
25void setInitialSelectionValue(Object newValue)
Sets the input value that is initially displayed as selected to the user.
26void setInitialValue(Object newInitialValue)
Sets the initial value that is to be enabled -- the Component that has the focus when the pane is initially displayed.
27void setInputValue(Object newValue)
Sets the input value that was selected or input by the user.
28void setMessage(Object newMessage)
Sets the option pane's message-object.
29void setMessageType(int newType)
Sets the option pane's message type.
30void setOptions(Object[] newOptions)
Sets the options this pane displays.
31void setOptionType(int newType)
Sets the options to display.
32static voidsetRootFrame(Frame newRootFrame)
Sets the frame to use for class methods in which a frame is not provided.
33void setSelectionValues(Object[] newValues)
Sets the input selection values for a pane that provides the user with a list of items to choose from.
34void setUI(OptionPaneUI ui)
Sets the UI object which implements the L&F for this component.
35void setValue(Object newValue)
Sets the value the user has chosen.
36void setWantsInput(boolean newValue)
Sets the wantsInput property.
37static int showConfirmDialog(Component parentComponent, Object message)
Brings up a dialog with the options Yes, No and Cancel; with the title, Select an Option.
38static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType)
Brings up a dialog where the number of choices is determined by the optionType parameter.
39static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)
Brings up a dialog where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.
40static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)
Brings up a dialog with a specified icon, where the number of choices is determined by the optionType parameter.
41static String showInputDialog(Component parentComponent, Object message)
Shows a question-message dialog requesting input from the user parented to parentComponent.
42static String showInputDialog(Component parentComponent, Object message, Object initialSelectionValue)
Shows a question-message dialog requesting input from the user and parented to parentComponent.
43static String showInputDialog(Component parentComponent, Object message, String title, int messageType)
Shows a dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType.
44static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)
Prompts the user for input in a blocking dialog where the initial selection, possible selections, and all other options can be specified.
45static String showInputDialog(Object message)
Shows a question-message dialog requesting input from the user.
46static String showInputDialog(Object message, Object initialSelectionValue)
Shows a question-message dialog requesting input from the user, with the input value initialized to initialSelectionValue.
47static int showInternalConfirmDialog(Component parentComponent, Object message)
Brings up an internal dialog panel with the options Yes, No and Cancel; with the title, Select an Option.
48static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType)
Brings up a internal dialog panel where the number of choices is determined by the optionType parameter.
49static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)
Brings up an internal dialog panel where the number of choices is determined by the optionType parameter, where the messageType parameter determines the icon to display.
50static int showInternalConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)
Brings up an internal dialog panel with a specified icon, where the number of choices is determined by the optionType parameter.
51static String showInternalInputDialog(Component parentComponent, Object message)
Shows an internal question-message dialog requesting input from the user parented to parentComponent.
52static String showInternalInputDialog(Component parentComponent, Object message, String title, int messageType)
Shows an internal dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType.
53static Object showInternalInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)
Prompts the user for input in a blocking internal dialog where the initial selection, possible selections, and all other options can be specified.
54static voidshowInternalMessageDialog(Component parentComponent, Object message)
Brings up an internal confirmation dialog panel.
55static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType)
Brings up an internal dialog panel that displays a message using a default icon determined by the messageType parameter.
56static voidshowInternalMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
Brings up an internal dialog panel displaying a message, specifying all parameters.
57static voidshowMessageDialog(Component parentComponent, Object message)
Brings up an information-message dialog titled "Message".
58static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType)
Brings up a dialog that displays a message using a default icon determined by the messageType parameter.
59static voidshowMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)
Brings up a dialog displaying a message, specifying all parameters.
60static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)
Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.
61void updateUI()
Notification from the UIManager that the L&F has changed.
62static int showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)
Brings up an internal dialog panel with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.

Methods inherited

This class inherits methods from the following classes:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JOptionPane Example

Create the following java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >

SwingControlDemo.java
package com.tutorialspoint.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
    
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showDialogDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showDialogDemo(){                                       
      headerLabel.setText("Control in action: JOptionPane"); 

      JButton okButton = new JButton("OK");        
      JButton javaButton = new JButton("Yes/No");
      JButton cancelButton = new JButton("Yes/No/Cancel");

      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(
            mainFrame, "Welcome to TutorialsPoint.com");
         }          
      });

      javaButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            int output = JOptionPane.showConfirmDialog(mainFrame
               , "Click any button"
               ,"TutorialsPoint.com"
               ,JOptionPane.YES_NO_OPTION);

            if(output == JOptionPane.YES_OPTION){
               statusLabel.setText("Yes selected.");
            }else if(output == JOptionPane.NO_OPTION){
               statusLabel.setText("No selected.");
            }
         }
      });

      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {                
            int output = JOptionPane.showConfirmDialog(mainFrame
               , "Click any button"
               ,"TutorialsPoint.com"
               ,JOptionPane.YES_NO_CANCEL_OPTION,
               JOptionPane.INFORMATION_MESSAGE);

            if(output == JOptionPane.YES_OPTION){
               statusLabel.setText("Yes selected.");
            }else if(output == JOptionPane.NO_OPTION){
               statusLabel.setText("No selected.");
            }else if(output == JOptionPane.CANCEL_OPTION){
               statusLabel.setText("Cancel selected.");
            }
         }
      });

      controlPanel.add(okButton);
      controlPanel.add(javaButton);
      controlPanel.add(cancelButton);       
      mainFrame.setVisible(true);  
   }
}

Compile the program using command prompt. Go to D:/ > SWING and type the following command.

D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output

Swing JOptionPane

Advertisements
Advertisements