Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Show Dialog Box in Java 
 

In this section we will use JOptionPane class to display the message Dialog box.

 

Show Dialog Box in Java - Swing Dialogs

                         

Message dialog box is used to display informative messages to the user. In this section we will use JOptionPane class to display the message Dialog box. Our program display "Click Me" button on the window and when user clicks on it program displays Message box with "OK" button and message "Roseindia.net". 

When you run the program following window will be displayed:

When you click on "Click Me" button, following Message is displayed:

Program description:

JOptionPane Class:

In non-swing application we were using System.in class for input or output some text or numeric values but now in the swing application we can use JOptionPane to show the output or show the message. This way of inputting or outputting works very efficiently in the Swing Applications. The window for showing message for input or output makes your application very innovative.

JOptionPane class is available in the javax.swing.*; package. This class provide various types of message dialog box as follows:

  • A simple message dialog box which has only one button i.e. "Ok". This type of message dialog box is used only for showing the appropriate message and user can finish the message dialog box by clicking the "Ok" button.
  • A message dialog box which has two or three buttons. You can set several values for viewing several message dialog box as follows:
    1.)    "Yes" and "No"
    2.)    "Yes", "No" and "Cancel"
    3.)    "Ok", and "Cancel"
  • A input dialog box which contains two buttons "Ok" and "Cancel".

The JOptionPane class has three methods as follows:

  • showMessageDialog(): First is the showMessageDialog() method which is used to display a simple message.
  • showInputDialog(): Second is the showInputDialog() method which is used to display a prompt for inputting. This method returns a String value which is entered by you.
  • showConfirmDialog(): And the last or third method is the showConfirmDialog() which asks the user for confirmation (Yes/No) by displaying message. This method return a numeric value either 0 or 1. If you click on the "Yes" button then the method returns 1 otherwise 0.

How program Works:

This program illustrates you how to show a message dialog box when you click on the button.

showMessageDialog():
This method is used to show a message dialog box which contains some text messages. This is being used with two arguments in the program where the first argument is the parent object in which the dialog box opens and another is the message which has to be shown.

Here is the code of the program:

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

public class ShowDialogBox{
  JFrame frame;
  public static void main(String[] args){
    ShowDialogBox db = new ShowDialogBox();
  }

  public ShowDialogBox(){
    frame = new JFrame("Show Message Dialog");
    JButton button = new JButton("Click Me");
    button.addActionListener(new MyAction());
    frame.add(button);
    frame.setSize(400400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
    public void actionPerformed(ActionEvent e){
      JOptionPane.showMessageDialog(frame,"Roseindia.net");
    }
  }
}

Download this example.

                         

» View all related tutorials
Related Tags: c ide class methods method get icon height vi width change using int id ai create js define show paint

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

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

Current Comments

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

example is good but dialog box is appeared behind the parent window first time, if i am adding these lines in my java code without button.. why it is happening?? User has to press Alt+ Tab to show the dialog box. Can anyone explain this??

Posted by Smith on Sunday, 11.16.08 @ 02:29am | #81710

If i click "compile" button then error say "frame.add(button);". So what does this mean

Posted by Anik on Saturday, 09.27.08 @ 07:49am | #80734

hi how hide the minimize and maximize buttton in the title bar of the window using java swings plz reply as soon as possible

Posted by sameena on Thursday, 02.7.08 @ 17:53pm | #47520

hi how hide the minimize and maximize buttton in the title bar of the window using java swings plz reply as soon as possible

Posted by sameena on Thursday, 02.7.08 @ 17:53pm | #47519

hi
thanks for information
this is very good
thanks again

Posted by janmejay on Wednesday, 01.30.08 @ 00:19am | #46525

sir, ilove the samples u posted..

Posted by ROmel on Wednesday, 11.28.07 @ 11:06am | #40789

hi,
instead of using myaction class we can use THIS method. ex.. button.addActionListener(this). by using this statement we can avoid the public class Myaction()..

Posted by ameer on Thursday, 08.16.07 @ 11:03am | #23488

the programe is good.but what i feel is instead of using two classes ShowDialogBox and MyAction
is it not possible to use one class and do the job

Posted by vasu on Saturday, 08.11.07 @ 12:06pm | #23157

hi,
this is very nice to step in to swing using this example
thanks...

Posted by ameer on Thursday, 08.9.07 @ 10:46am | #22998

Even beginners can also understand this tutorial... Very nice...

Posted by Srinivasan on Friday, 03.30.07 @ 19:19pm | #13013

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.