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


 
  
 
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
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

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

Latest Searches:
increase size of strut
logout code in jsp
calculator code usind
java strings
what is static inner c
using set in java
EncoderTest
password match jsf
Copy One Database Tabl
checkbox button enable
simple calendar
ajax
embedded java script i
how to use tab control
HSSF clone cell
Arraylist text file
What are the defined J
jsp applet netbeans
linked list
SwingWorker
launch dialog with but
charts using POI
save image
Static Fields
spring hibernate web e
how to override a vari
storing text data to a
tibco
finding superscript in
selectonelistbox examp
xls
getNumericValue
Cloneable
Photoshop Text Effects
Binome
Photoshop Photo Effect
convert linked list to
how to use different d
GWT Servlet
checkbox in servlet
Photoshop Web Layouts
adding image in j2ee
ะà¸??ะà¸?ย ะà
Java variable declarat
button launches dialog
string array add eleme
how to get the time di
Java example program t
Accessing oracle datab
Difference between req
hide file
infix
calling one jsp p
breakà¹?à¸???????à¹?à¸
how to capture multipl
LRU
hibernate secondery le
Telephony
display the sum of 20
insert fields blob
enter
how to override...ng i
Example DynaValidator
dialog
getwindowwidthandheigh
HibernatewithDAO
JConfig
SortIt
client/server computin
java drivers use to co
dojo.query
array in javascript
char at
validate using picture
jQuery To Slide Effect
calculate time
Linux Caixa MÐ????Ð???
runnable
calculator square root
decimal to aski
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 | iPhone 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.