JOptionPane Example Taking User Input

JOptionPane Example Taking User Input

JOptionPane Example: It is easy to take input using Swing. showInputDialog() method of JOptionPane displays a dialog box in which the user can enter data. The method reads the data and returns the data as a string.
JOptionPane Example with user input of two numbers

JOptionPane Example
Output screen on JOptionPane Example

String str1 = JOptionPane.showInputDialog(“Enter First Number”);

The return type of showInputDialog() method is a string. It is required to parse (using wrapper classes) into a double value.

The same JOptionPane can be used ti display messages to user like println() method does. Refer showMessageDialog() in Swing JOptionPane – Displaying Message.

Note: Always the second parameter in the message dialog must be a string.

Total 4 programs are given on JButton:
1. JButton with Images and Labels
2. JButton Border
3. JButton Label Style
4. JButton ActionListener

2 thoughts on “JOptionPane Example Taking User Input

  1. Sayan

    JOptionPane.showMessageDialog( null, “The Product is ” + fn*sn, “Product”, JOptionPane.INFORMATION_MESSAGE)
    what is the operation of 3rd and 4th parameter of the method?

Leave a Reply

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