Using JOptionPane (MessageDialog)

Guest Posts Steven

I'm going to write about the MessageDialog and the InputDialog, those are very useful and they're used in lots of things.



First we're going to learn how to use the MessageDialog, to use you need to write: JOptionPane.showMessageDialog(), this funtion has different constructors:



  • showMessageDialog(Component parentComponent, Object message) : For the parentComponent you can use the JFrame you're working on, or you can write null and the message will appear at the center of the screen. And the message is going to be a String with the message you want to be displayed.


  • showMessageDialog(Component parentComponent, Object message, String title, int messageType) : In this case we have two more parameters, the title is a String that is going to be the title of your message frame. And the messageType is the type of message to be displayed: ERRORMESSAGE, INFORMATIONMESSAGE, WARNINGMESSAGE, QUESTIONMESSAGE, or PLAIN_MESSAGE.


  • showMessageDialog(Component parentComponent,Object message,String title, int messageType, Icon icon): In here the Icon parameter is an icon to be displayed in the dialog, that helps the user identify the kind of message that is being displayed.


In this example in going to use the second option, we're going to have a frame with a button and the button is clicked the message will be displayed.



Here's the code:


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class exampleJFrame extends JFrame{
private JPanel _panel; 
/*
 * buttons are global variables 
*/ 
private JButton _clickme;
 /**
 * Create the frame.
 */
public exampleJFrame() {
   setVisible(true); //we make the window visible
   setResizable(false); // if write false, you won't be able to resize the window
   setTitle("Example"); //you set the title for the window
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setLocationRelativeTo(null); // you make the window appear at the center of the screen
   setSize(294,159); // you set the size of the window
   _panel = new JPanel();
   getContentPane().add(_panel);
   _panel.setLayout(null); // you set the Layout to null = Absolute Layout
   _clickme = new JButton("Click Me!");
   _clickme.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
      //JOptionPane.showMessageDialog(null, "You clicked the button!"); This is the first option
      JOptionPane.showMessageDialog(null, "You clicked the button!", "Message", JOptionPane.PLAIN_MESSAGE);
                                 //parent        message                title       messageType
   }
});
   _clickme.setBounds(98, 55, 89, 23); // you set the size and position of the button in the JPanel
   _panel.add(_clickme); // you add the button to the panel
}
public static void main(String[] args) {
   exampleJFrame example=new exampleJFrame();
 
}



 


Here's the result:

Image



 



As can see the message is shown with the title we set.


In the next article we'll see how to use the InputDialog to get the input form the user.