Home Java Example Java Swing Message Box Java

Related Tutorials


 
 

Share on Google+Share on Google+

Message Box Java

Advertisement
Here we will discuss how to create a message box in Java with an example. This message box will display information is a message you want to display to a user after a particular task.

In this section we will create a message box in Java using a Java API class. A certain message, warning or information can be displayed in this message box. Generally the message box is used to display error message. JOptionPane class is used to create a message box. We can customize icon, component of the dialog box and where the box must appear on screen.

JOptionPane icon is used to create the icon you want to display. Though there are four standard JOptionPane icon, once can also customize them.

JOptionPane() creates a JOptionPane with the specified buttons, icons, message and title.

Following code creates a Message box and displays the message "This is a Message Box.":

JOptionPane.showMessageDialog(frame, "This is a Message Box.");

showxxxdialog methods are as follows:

  • showMessageDiaolog: displays one button dialog method
  • showOptionDialog: displays a variety of button with button text and contain a standard text message.
  • showConfirmDialog: asks for confirmation of something with Yes/No.
  • showInputDialog: displays a message box that accepts input from user from text field or combo box.
  • showMessageDialog: displays a message with one button which is labeled "OK". You can specify the message, title, and icon according to your wish.
  • showOptionDialog: displays a Message with the specified buttons, icons, message and title. Using this once can change the text that appears on the buttons of the message Box.
Object[] options = {"Yes", "No", "Ask later"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like to save it",null, JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);

Here is an example of displaying a message box using swing. First create a swing application then a button is created. When you click on the button a message box will pop up and display the specified message.

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

public class MessageBox {
JFrame frame;

public static void main(String[] args) {
MessageBox db = new MessageBox();
}

public MessageBox() {
frame = new JFrame("Show Message Box");
JButton button = new JButton("Click");
button.setAlignmentX((float) 100.00);

button.addActionListener(new MyAction());
frame.add(button);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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

Resource:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 7, 2013

Related Tutorials

Discuss: Message Box Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com