About

Kannur University btech CSE study materials, question papers, syllabus . . .

Friday, July 19, 2013

S7 IWP notes - JOptionPane

JOptionPane
JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something.
While the JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line calls to one of the static showXxxDialog methods shown below:

Method Name
Description
showConfirmDialog
Asks a confirming question, like yes/no/cancel.
showInputDialog
Prompt for some input.
showMessageDialog
Tell the user about something that has happened.
showOptionDialog
The Grand Unification of the above three.

messageType
Defines the style of the message. The Look and Feel manager may lay out the dialog differently depending on this value, and will often provide a default icon. The possible values are:
·         ERROR_MESSAGE
·         INFORMATION_MESSAGE
·         WARNING_MESSAGE
·         QUESTION_MESSAGE
·         PLAIN_MESSAGE
optionType
Defines the set of option buttons that appear at the bottom of the dialog box:
·         DEFAULT_OPTION
·         YES_NO_OPTION
·         YES_NO_CANCEL_OPTION
·         OK_CANCEL_OPTION
When one of the showXxxDialog methods returns an integer, the possible values are:
  • YES_OPTION
  • NO_OPTION
  • CANCEL_OPTION
  • OK_OPTION
  • CLOSED_OPTION

Show an error dialog that displays the message, 'alert':
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);

Show an internal information dialog with the message, 'information':
JOptionPane.showInternalMessageDialog(frame, "information",
"information", JOptionPane.INFORMATION_MESSAGE);

Show an information panel with the options yes/no and message 'choose one':
JOptionPane.showConfirmDialog(null,
"choose one", "choose one", JOptionPane.YES_NO_OPTION);

Show an internal information dialog with the options yes/no/cancel and message 'please choose one' and title information:
JOptionPane.showInternalConfirmDialog(frame,
"please choose one", "information",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to continue':
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);

Show a dialog asking the user to type in a String:
String inputValue = JOptionPane.showInputDialog("Please input a value");

Show a dialog asking the user to select a String:
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);

Constructor Summary
JOptionPane()
          Creates a
JOptionPane with a test message.
JOptionPane(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.
JOptionPane(Object message, int messageType)
          Creates an instance of
JOptionPane to display a message with the specified message type and the default options,
JOptionPane(Object message, int messageType, int optionType)
          Creates an instance of
JOptionPane to display a message with the specified message type and options.
JOptionPane(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.
JOptionPane(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.
JOptionPane(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.
Sample Progoram
import javax.swing.*;
import java.awt.event.*;
public class JOptionPaneEx extends JFrame implements ActionListener
{
 JButton b1=new JButton("Click Me");
 JOptionPaneEx()
 {
  setSize(200,200);
  getContentPane().add(b1);
  b1.addActionListener(this);
  setVisible(true);
 }
 public void actionPerformed(ActionEvent AE)
 {
  int age=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter your age"));
  JOptionPane.showConfirmDialog(null,age,"is this your real age",JOptionPane.OK_CANCEL_OPTION);
  for(int i=0;i<10;i++)
  JOptionPane.showMessageDialog(null,"Corrupted");
   }
  public static void main(String ag[])
 {
  JOptionPaneEx e1=new JOptionPaneEx();

   } }

0 comments:

Post a Comment