How to show DialogBox in java? Complete JOptionPane tutorial.

We need to show the Dialog Boxes lot of times for showing different messages. Java Swing provides JOptionPane utility.

Lets see how we can use JOptionPane class for showing different types of messages :

JOptionPane :

JOptionPane class belongs to the javax.swing.*; package. The class comes with lot of options which user can use for different purpose. It provides dialog boxes from just simple message dialog box to input dialogs.

A simple message dialog box :

It has only one button "OK" , this message dialog box can be used for showing appropriate message for user. JOptionPane provides support providing icons, specifying the dialog title and text, and even customizing the button text. We can also customize how the components inside the dialog layout and displays and also where the dialog should appear on screen.

JOptionPane provides very interesting feature of specifying the icons. We can use customized icons or we can use default used by JOptionPane which are question, information, warning, and error. Lets see how icons get showed up in Windows and Java Looks and feel.




showMessageDialog() :

We can use this method to create simple modal message dialogs. We can specify the message, icon, and title that the dialog displays. Lets see it example below :

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

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

  public DialogBoxTutorial(){
    frame = new JFrame("Simple Dialog Box Demo");
    JButton button = new JButton("Show Dialog");
    button.addActionListener(new DialogAction());
    frame.add(button);
    frame.setSize(500, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class DialogAction implements ActionListener{
    public void actionPerformed(ActionEvent e){
      JOptionPane.showMessageDialog(frame,
            "This is warning dialog demo.",
            "Warning",
        JOptionPane.WARNING_MESSAGE);
    }
  }
}


Here we are used the warning message option so output of this will be





showOptionsDialog()

JOptionPane has showOptionsDialog method which allows to specify options such as buttons, icons, message, title, and so on. The benefit of this method is we can change the text appears on buttons of JOptionPane.

Object[] options = {"Yes",
                    "No",
                    "Yes to All", "No to all"};
        int n = JOptionPane.showOptionDialog(frame,
            "Would you like to replace the demo file? ",
            "Options ",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);





showConfirmDialog()

This function used when we don't need to change the button text.

        int n = JOptionPane.showConfirmDialog(
            frame,
            "Would you like to replace the file?",
            "Confirm Dialog Options",
            JOptionPane.YES_NO_OPTION //Button appearing on JOptionpane
);
            }




showInputDialog() Example :

Unlike other dialogs this method doesn't return the int , instead it returns the Object which is normally a String. This method is used to get input from the JOptionPane.


String s = (String)JOptionPane.showInputDialog(
                    frame,
                    "String input text",
                    "Input Dialog Box",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    "Type text here");

        System.out.println("String "+s);


Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment