WHAT'S NEW?

JOptionPane: Understanding Message Box in Java

JOptionPane in Swing is an important component given that it notifies about some important events and other.

JOptionPane is in javax.swing package and looks something like this.
JOptionPane
 JOptionPane have a set of static methods listed below which can used as per requirement.

Confirm Dialog


public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType)


-Starts an OptionPane which is place on the specified parentComponent, holding given message and with speicified title.
Possible options for optionType are
  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION
It can used like this.

int val=JOptionPane.showConfirmDialog(null, "message", "title", JOptionPane.YES_NO_OPTION);
Confirm Dialog

Input Dialog


public static Object showInputDialog(Component parent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)

- Opens a JOptionPane placed on parent with given message and title. Type of message can be set using messageType with icon of choice. An array have to be created whose values are displayed inside the combo box and what should be the initial value can be decided through last paramter.

Possible values for messageType are
  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE
Object options[]={"Option 1","Option 2","Option 3"};
Object o=JOptionPane.showInputDialog(null,"Select a Name","Title",JOptionPane.PLAIN_MESSAGE,null,options,"Select Option");

Input Dialog

Message Dialog

public static void showMessageDialog(Component parent, Object message)

- Displays a Option Pane with  Message on the component.

JOptionPane.showMessageDialog(null, "message");

A Demo Program for using JOptionPane


import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class OptionDemo extends JFrame
{
JButton jbtnInput,jbtnMessage,jbtnSelect;
JFrame f=this;
OptionDemo()
{
super("Option Demo");
setSize(700,500);
setResizable(true);
setDefaultCloseOperation(3);
Container c=getContentPane();
setLayout(new FlowLayout());

jbtnInput=new JButton("Input");
jbtnInput.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Object options[]={"Option 1","Option 2","Option 3"};
JOptionPane.showInputDialog(f,"Select a Name","Title",JOptionPane.PLAIN_MESSAGE,null,options,"Select Option");
}
});
c.add(jbtnInput);

jbtnSelect=new JButton("Confirm");
jbtnSelect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{

JOptionPane.showConfirmDialog(f, "message", "title", JOptionPane.YES_NO_OPTION);
}
});
c.add(jbtnSelect);

jbtnMessage=new JButton("Message");
jbtnMessage.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{

JOptionPane.showMessageDialog(f, "message");
}
});
c.add(jbtnMessage);

setVisible(true);
}

public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(new com.jtattoo.plaf.hifi.HiFiLookAndFeel());
}
catch (Exception e)
{
e.printStackTrace();
}
new OptionDemo();
}
}

0 comments:

Post a Comment