- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create input Pop-Ups (Dialog) and get input from user in Java?
Use the JOptionPane.showInputDialog() to get input from user in a dialog box like “Which sports you play the most”, “What is your name”, etc. The following is an example to create input Pop-Ups (Dialog) and get input from user −
Example
package my; import javax.swing.JOptionPane; public class SwingDemo { public static void main(String[] args) { String[] sports = { "Football", "Cricket", "Squash", "Baseball", "Fencing", "Volleyball", "Basketball" }; String res = (String) JOptionPane.showInputDialog(null, "Which sports you play the most?", "Sports", JOptionPane.PLAIN_MESSAGE, null, sports, sports[0]); switch (res) { case "Football" − System.out.println("I Love Football"); break; case "Cricket" − System.out.println("I Love Cricket"); break; case "Squash" − System.out.println("I Love Squash"); break; case "Baseball" − System.out.println("I Love Baseball"); break; case "Fencing" − System.out.println("I Love Fencing"); break; case "Volleyball" − System.out.println("I Love Volleyball"); break; case "Basketball" − System.out.println("I Love Basketball"); break; } } }
Output
Now select any of the item from above and click OK to display the selected item in the Console.
We selected “Volleyball” −
The option selected above visible in Console −
- Related Articles
- Java Program to Get Input from the User
- How to create Message Pop-Ups with Java?
- How to get Input from the User in Golang?
- Swift Program to Get Input from the User
- Haskell program to get input from the user
- Python Get a list as input from user
- Get number from user input and display in console with JavaScript
- How to input multiple values from user in one line in Java?
- Java Program to fill an array of characters from user input
- Take Matrix input from user in Python
- Taking input from the user in Tkinter
- Can we read from JOptionPane by requesting input from user in Java?
- How to input multiple values from user in one line in Python?
- How to input multiple values from user in one line in C#?
- How to create Python dictionary from JSON input?

Advertisements