Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Show Message and Confirm Dialog Box - Swing Dialogs

                         

This section show you how to display several types of message box. There are three types of message dialog box that you can use in your swing applications, example of each type of dialog boxes are provided here. When your run the program, it will display a frame with three buttons. Once you click on the first button then the simple message box will open which holds only "Ok" button as shown below:

Image 1

If you click on the second button then the confirm dialog box will open which asks for "Ok" and "Cancel".

Image 2

If you click on the "Ok" button then a message dialog box will open with message "You clicked on "Ok" button" like this
Image 4 
otherwise message dialog box will open with text "You clicked on "Cancel" button like this

Image 6
If you click on the third button from the main window or frame then a confirm message dialog box will open with three button i.e. the "Yes", "No" and "Cancel" like the following image:

For this purposes two methods have been used:

  • showMessageDialog():
    Above method shows a simple message dialog box which holds only one button i.e. "Ok" button. This method takes four arguments in which, first is the parent object name, second is the message as string, third is the title of the message dialog box as string and the last is the type of the message dialog box.
      
  • showConfirmDialog():
    Above method asks from the user by displaying a message dialog box which contains more than one button. Depending on the parameter passed it can be "Ok" and "Cancel" or  "Yes", "No" and "Cancel". This method returns the integer value.

Here is the code of the program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ShowMessageDialog{
  JButton button;
  public static void main(String[] args){
    ShowMessageDialog md = new ShowMessageDialog();
  }

  public ShowMessageDialog(){
    JFrame frame = new JFrame("Message Dialog Box");
    button = new JButton("Show simple message dialog box");
    button.addActionListener(new MyAction());
    JPanel panel = new JPanel();
    panel.add(button);
    button = new JButton("Show \"Ok/Cancel\" message 
dialog box"
);
    button.addActionListener(new MyAction());
    panel.add(button);
    button = new JButton("Show \"Yes/No/Cancel\" dialog box");
    button.addActionListener(new MyAction());
    panel.add(button);
    frame.add(panel);
    frame.setSize(400400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
    public void actionPerformed(ActionEvent ae){
      String str = ae.getActionCommand();
      if(str.equals("Show simple message dialog box")){
        JOptionPane.showMessageDialog(null, "This is the simple message 
dialog box."
"Roseindia.net"1);
      }
      else if(str.equals("Show \"Ok/Cancel\" message dialog box")){
        if(JOptionPane.showConfirmDialog(null, "This is the \"Ok/Cancel\" 
message dialog box."
"Roseindia.net", JOptionPane.OK_CANCEL_OPTION== 0)
          JOptionPane.showMessageDialog(null, "You clicked on \"Ok\" button",
 
"Roseindia.net"1);
        else
          JOptionPane.showMessageDialog(null, "You clicked on \"Cancel\" button",
 
"Roseindia.net"1);
      }  
      else if(str.equals("Show \"Yes/No/Cancel\" dialog box")){
        JOptionPane.showConfirmDialog(null, "This is the \"Yes/No/Cancel\"
 message dialog box."
);
      }
    }
  }
}

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (post your own) View All Comments Latest 10 Comments:

is there an example for conection in a data base??

Posted by ROmel on Wednesday, 11.28.07 @ 11:59am | #40798

The Program is good


Like Visual Baisc Does Java also has MsgBox(" tile", "gggg", sss);

API

Posted by Lucky on Thursday, 06.21.07 @ 02:48am | #19820

Hello,

This is site is really good and esp. this code is really good. Short and sweet !

Sam..

Posted by Sam on Wednesday, 06.6.07 @ 17:05pm | #18398

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.