Java code of JOptionPane Dialogs


import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class TestDialogue extends JFrame implements ActionListener {
    JButton button1 =null;
    JButton button2 =null;
    JButton button3 =null;

  public TestDialogue() throws HeadlessException {
    setSize(200, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    button1 = new JButton("Simple Message dialog");
    button2 = new JButton("Dialog with InputBox");
    button3 = new JButton("Yes/No dialog");

    setLayout(new FlowLayout(FlowLayout.CENTER));
    getContentPane().add(button1);
    getContentPane().add(button2);
    getContentPane().add(button3);

    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
  }

  public static void main(String[] rk) {
        try
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	}catch(Exception e)
        {}
        new TestDialogue().setVisible(true);
  }

    @Override
    public void actionPerformed(ActionEvent e) {
    String Action;
    Action = e.getActionCommand ();

        if(Action.equals("Simple Message dialog")){
            JOptionPane.showMessageDialog((Component) e.getSource(), "Hello ! Thank you!");

        }else if(Action.equals("Dialog with InputBox")){
            String name = JOptionPane.showInputDialog((Component) e.getSource(), "Hello, please Input your name.");
            if (name != null && !name.equals("")) {
                JOptionPane.showMessageDialog((Component) e.getSource(), "Hi, " + name);
            }

        }else if(Action.equals("Yes/No dialog")){

            int result = JOptionPane.showConfirmDialog((Component) e.getSource(), "Close this application?");
            if (result == JOptionPane.YES_OPTION) {
            System.exit(0);
            } else if (result == JOptionPane.NO_OPTION) {
            System.out.println("No Action performed.");

        }
       }

    }
}
Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in J2SE. Bookmark the permalink.

One Response to Java code of JOptionPane Dialogs

  1. Pingback: Pane e Pizza « GUSTO E SAPORI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s