Make Java Application to Display the Message in a Dialog Box using NetBeans IDE.

Today morning I taught you how to make a complete GUI Java Application using Java, In that tutorial I taught you How to display “Hello World” in the place of theLabel using setText() method. That was one way of displaying the desired output but in Java you can display outputs in many other ways. In this post I’ll teach you how to display the output in a message box.

Have you ever formatted your USB drive? If yes then at the time the formatting is complete you see a message which is shown in a box saying “Format Completed”. You must have tried to delete files on your system? At the time of deleting you also see a window which asks for confirmation whether you want to delete the file or not, it is also one form of Dialog box. So now let us now begin with the tour to display the desired text in the Dialog Box using Java.

I assume that you know how to create a Java Application and add a frame to your application, If not then don’t worry you can navigate to the self explanatory post where I’ve already taught How to Create a Java Application and add a frame to it.

After you finish adding a Frame to your Application, Follow the steps I say to complete the purpose for what you are here.

  • Drag the button from the Palette located at the right side of the IDE to the desired position, after dragging rename the Button to the desired name. The design of the frame will look similar to the below image.


  • Double click the button the source window will appear in which you will have to write the code for action which should take place on clicking the button. The source code area of the button will be looking like the below showed Image when you double click on it.

    Now write the below code,


    JOptionPane.showMessageDialog(null,“Welcome to the world of Java”);

    After writing this you will see that the IDE will show a bulb like thing on the lines where you’ve written the code, don’t get worried if you see this.


    Just click on the bulb you will see many options coming in it but you should click on add import for javax.swing.JOptionPaneafter importing you will see that the bulb will go away and the source window will look like this

  • The tutorial doesn’t end’s here, Run the application by the shortcut keys SHIFT + F6 and Frame will appear, It will look similar to the window shown below


    And on clicking the button and you will see that a Dialog box will appear which will show the message “Welcome to the World of Java”
    See here is the output


Doesn’t it look awesome just writing one line of code to display the Dialog box with the desired text?

Here ends the tutorial but still now you aren’t free, I’ll give you some tasks to complete, Display “Techfiest a technology blog” in place of the above text in a Dialog Box.

Note

If you have any problem related to this post or any other problem related to Java then Post it out.

Article publié pour la première fois le 06/10/2013

One thought on “Make Java Application to Display the Message in a Dialog Box using NetBeans IDE.

  • October 10, 2013 at 9:53 pm
    Permalink

    how to export the input by user in an excel file?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *