Java GUI Tutorial - using Dialog Box to Input in Java using JOptionPane.showInputDialog() method
Coding River
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 ... https://www.youtube.com/watch?v=-iT5D7CJ3HM
8354219 Bytes