How to get the input from joptionpane

How to get the input from joptionpane

By: chappelle Date of post: 21.07.2017

Another useful class for accepting user input, and displaying results, is the JOptionPane class.

how to get the input from joptionpane

This is located in the javax. The JOptionPane class allows you to have input boxes like this one:.

Let's adapt our code from the previous section and have some option panes. This tells java that we want to use the JOptionPane class, located in the javax. You can start a new project for this, if you prefer not to adapt your previous code. You should know how to create a new project by now.

how to get the input from joptionpane

Just remember to change the name of the Class from Main to something else. We're going to have the class name InputBoxes for ours.

Our package name will be userinput. Add the import line to your new project, and your code window should look like something like this:.

The reason for the wavy underline is that we haven't used the JOptionPane class yet. It will go away once we do. To get an input box that the user can type into, we can use the showInputDialog method of JOptionPane. We'll store the input straight into a first name variable again, just like last time. So add the following line to your main method:.

[Tutorial] JOptionPane: easy input, output with dialog boxes

In between the round brackets of showInputDialog type the message that you want displayed above the input text box. We've typed "First name".

how to get the input from joptionpane

Like all strings, it needs to go between double quotes. This time, we want showMessageDialog from the popup list. In between the round brackets we first have the word null. This is a java keyword and just means that the message box is not associated with anything else in the programme. After a comma comes the text we want to display in the message box.

The whole of your code should look like this:. As its name suggests, this ensures that the programme exits. But it also tidies up for us, removing all the created objects from memory. Now run your code. Another quick way to run your programme in NetBeans is by right-clicking anywhere inside of the coding window. From the menu that appears, select Run File.

Exercise Input boxes and Message boxes can be formatted further. Try the following for your Input boxes:. Exercise A Input boxes are not just used for text: Write a programme that prompts the user for two numbers, the breadth of a rectangle and the height of a rectangle. Use a message box to calculate the area of the rectangle. However, you'll need some extra help for this exercise. Help for the Exercise You have to use the String variable to get your numbers from the user:.

How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

However, you can't multiply two strings together. You need to convert the Strings to integers. You can convert a string to an integer like this:.

java - Read input from a pezewehemave.web.fc2.computDialog box - Stack Overflow

So you type Integer then a full stop. After the stop, type parseInt. In between the round brackets of parseInt, type the name of the string variable you're trying to convert. Exercise B The programme will crash if you enter floating point values for the breadth and height. How would you solve this? When you have solved the above exercise, do you really want Integer. What else do you think you can use?

inserted by FC2 system