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

Publicado el: 01 January 1970
en el canal de: Career & Tech HQ
4,587
37

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

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.

So, the class JOptionPane is part of the package javax.swing and this class uses two predefined methods: showInputDialog() and showMessageDialog

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()

String str = JOptionPane.showInputDialog(“How are you ?”);

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
String str = JOptionPane.showInputDialog(“How are you ?”);
System.out.println(str);
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


En esta página del sitio puede ver el video en línea Java GUI Tutorial - using Dialog Box to Input in Java using JOptionPane.showInputDialog() method de Duración hora minuto segunda en buena calidad , que subió el usuario Career & Tech HQ 01 January 1970, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 4,587 veces y le gustó 37 a los espectadores. Disfruta viendo!