0

Using JOptionPane for Input

It is very easy to use interactive user input windows that is called JOptionPane, in Java.

First of all, you will need to import the “JOptionPane” class before the declaration of your class. The code is as follows:

import javax.swing.JOptionPane;

Next you need to see what is the type of input you are getting from the user. If it is an interger or double, then you will need to convert it into that from string.

For e.g if I have a variable: Double variableName;

Then do as follows to get user input:

variableName=Double.parseDouble(JOptionPane.showInputDialog(null, “Enter a Number”, “My Calculator”, JOptionPane.QUESTION_MESSAGE));

Here, Double.parseDouble would convert what the user enters from String to Double. The second parameter is what description would appear in the box. The third parameter is what would appear as the window title and the last parameter is the icon that would display. In this case its a question mark.

For any queries ask #AyeshaAsghar

ayesha

Leave a Reply

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