JOptionPane Component in Java

This is a tutorial in which we will going to create a program that will have a JOptionPane Component in Java. A JOptionPane lets the program pop up a dialog box that will let the user informs something. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jOptionPaneComponent.java. 2. Import the javax.swing.* package to access the JOptionPane class.
  1. import javax.swing.*; //used to access the JOptionPane class
3. Initialize your variable in your Main, variable num as String.
  1. String num;
Now, we will use the showInputDialog of the JOptionPane to make the variable num as an input dialog. Have this code below:
  1. num = JOptionPane.showInputDialog("Enter an integer:");
4. To convert the variable num as a complete integer, we will use the parse method and have it initialized as a variable.
  1. int parseNum = Integer.parseInt(num);
To show the number that you have inputted, we will use the showMessageDialog method of the JOptionPane. Have this code below:
  1. JOptionPane.showMessageDialog(null,"You entered " + parseNum + ".");
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; //used to access the JOptionPane class
  2.  
  3. public class jOptionPaneComponent {
  4. public static void main(String[] args) {
  5. String num;
  6. num = JOptionPane.showInputDialog("Enter an integer:");
  7.  
  8. int parseNum = Integer.parseInt(num);
  9. JOptionPane.showMessageDialog(null,"You entered " + parseNum + ".");
  10. }
  11. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment