Java GUI Tutorial - using Dialog Box to Input in Java using JOptionPane.showInputDialog() method

preview_player
Показать описание

In today’s video, we will talk about how to use Graphical User Interface or Dialog Boxes to gather input and output results in a Java program.

Actually, Java library provides a predefined class that allows us to use Graphical User Interface in order to input values and output results. That predefined class is called JOptionPane.

The method showInputDialog() is used when we want to allow the user to input a string value from the keyboard
Meanwhile, the method showMessageDialog is used to output or display a result in a dialog box

In this video, we will have a look at how to use the method showInputDialog() in a program

So here is the syntax of the method showInputDialog()

Now, let me explain this syntax
str : is a string variable because the showInputDialog() method returns a string. That means that all the values inputted through this method are considered as strings
“How are you ?” in between the brackets is a string that will appear in the dialog box and that you can also use to inform the user about what to type in

If you run this small progam, a dialog box will pop up on the screen
And as you can see the string “How are you” is showing in the dialog box, giving the user an idea of what to type in the text field.

I will type in a value “I am fine” in the text field and click on OK, the dialog box disappears and as I said before, the value entered is returned as a string and assigned to the variable str.

To prove that the value has been assigned, I will do
And run
The result in the console is : I am fine

So, guys that’s how you can input data in your program using dialog a box or graphical User Interface

Thanks for viewing, I hope this video was informative and do not forget to subscribe

#codingriver
#java
#programming
Рекомендации по теме
Комментарии
Автор

Thanks sir, Needed to know for an exam today!

tmroshiii