Adv Java - [Swing: JOptionPane Class]

♠ Posted by Unknown in at 00:04

JOptionPane Class


Windowing environments commonly contain a standard set of message boxes that allow you to quickly post information to the used or to capture information form the user. In Swing, these message boxes are contained in JOptionPane. You have many different possibilities, but the ones you’ll most commonly use are probably the message dialog and confirmation dialog, invoked using the static JOptionPane.showMessageDialog() and JOptionPane.showConfirmDialog().
The following example shows a subset of the message boxes available with JOptionPane.

Example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MessageBoxes extends JApplet implements ActionListener
{
          JButton[] jb = {new JButton("Alert"), new JButton("Yes/No"),
                                                new JButton("Color"), new JButton("3 Vals")};
          JTextField txt = new JTextField(15);
          public void init()
          {
                   Container contentPane = getContentPane();
                   contentPane.setLayout(new FlowLayout());
                   for(int i = 0; i < jb.length; i++)
                   {
                             jb[i].addActionListener(this);
                             contentPane.add(jb[i]);
                   }
                   contentPane.add(txt);
          }
          public void actionPerformed(ActionEvent ae)
          {
                   String id = ((JButton)ae.getSource()).getText();
                   if(id.equals("Alert"))
                             JOptionPane.showMessageDialog(null,"There's a bug on you!","Hey!",JOptionPane.ERROR_MESSAGE);
                   else if(id.equals("Yes/No"))
                   JOptionPane.showConfirmDialog(null,"Or No","Choose Yes",JOptionPane.YES_NO_OPTION);
                   else if(id.equals("Color"))
                             {
                                      Object [] options = {"Red", "Green"};
                                      int sel = JOptionPane.showOptionDialog(null,"Choose a Color!","Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
                                      if(sel != JOptionPane.CLOSED_OPTION)
                                                txt.setText("Color Selected : " + options[sel]);
                             }
                   else if(id.equals("Input"))
                   {
                             String val = JOptionPane.showInputDialog("How many fingers do you see?");
                             txt.setText(val);
                   }
                   else if(id.equals("3 Vals"))
                   {
                             Object [] options = {"First", "Second", "Third"};
                             Object sel = JOptionPane.showInputDialog(null,"Choose One!","Input",JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
                             if(sel != null)
Image: Swing JOptionPane Example                                                txt.setText(sel.toString());
                   }                 
          }
}

0 comments:

Post a Comment