ff

Recent Posts

Thursday, May 9, 2013

JOptionPane How to Use (For Beginners)


Hi Java warriors.. nice to meet you again..
Today I'm gonna show you how to use JOptoinPane Class.

Basically There are 4 main methods we use often 
1.    showMessageDialog
2.    showOptionDialog
3.    confirmDialog
4.    inputDialog

showMessageDialog() method

If you want Just a display a message as some information or as an error. such situations we can use showMessageDialog.
method Definition
public static void showMessageDialog(Component parentComponent,
                     Object message,
                     String title,
                     int messageType,
                     Icon icon)
                         throws HeadlessException
Explanation of Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used(Most of the time default frame will be your desktop , so it will load center of your Desktop. if you use 'this' as parent object which means your current Frame then JoptionPane will load center of your Frame no matter where is it.)
message - the Message ( Object) to display
title - the title string for the dialog
messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE
icon - an icon to display in the dialog that helps the user identify the kind of message that is being displayed

class TestDialog{
            public static void main(String args[]){
                        JOptionPane.showMessageDialog(this, "message goes here", "this is dialog title", JOptionPane.INFORMATION_MESSAGE, null);
//I didn't set any Icon, thats why last parameter is set as 'null'
            }
}

showOptionDialog() method

we can use showJOptionDialog method when we need customize JOptionPane.
For example Lets change the text/caption of 'OK' button
JOptionPane.showOptionDialog(null,
        "Message goes Here",
        "This is title",
        JOptionPane.OK_OPTION,
        JOptionPane.INFORMATION_MESSAGE,
        null,
        new String[]{ "my new button text"}, // this is the array
        "default");


and using same way, you can add many buttons to your OptionPane. look at following code..

                String[] buttonArray = {"Button1", "Button2", "Button3"};
                int returnValue = JOptionPane.showOptionDialog(
                                null, "Message Body goes here", "Dialog title goes here", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, buttonArray, "ddd");
                System.out.println("you selected button no"+returnValue);




if you select "Button 1" your console will display a message "you selected button no 1"
if you didnt select any button your console message will be "you selected button no -1"
this happen bcoz showOptionDialog return a integer number of your selected button
if you select first button it will return no '0' , if you select 2nd number it will return no '1' ect..
this is useful when you need to know whether user close the window or select a button

and you can also create a JOptionPane dialog with different language as following way



for do this we use a label  and button with sinhala language font and to change the  title font, I have set sinhala font to JOptionPane... check following code you will get it...

JLabel jLabel1=new JLabel();
jLabel1.setText("පණිවිඩය මෙතන ලියන්න");
jLabel1.setFont(new Font("Iskoola Pota", Font.BOLD, 14));
JOptionPane jopt = new JOptionPane();
jopt.setFont(new Font("Iskoola Pota", Font.BOLD, 14));
JButton btn1=new JButton("එකගයි");
btn1.setFont(new Font("Iskoola Pota", Font.BOLD, 14));
jopt.showOptionDialog(null,
                 jLabel1,
                "මාතෘකාව",
                 JOptionPane.OK_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new JButton[]{btn1},
               "default");




0 comments :

Post a Comment