I'm trying to show a user JOptionPane where they enter x and y coordinates each in their own textfield, but I don't know how to store user input as ints.

So I need to do something like:

int xCorFromUser = xCor;
int yCorFromUser = yCor;

I have this code so far:

JTextField xCor = new JTextField();
JTextField yCor = new JTextField();

Object[] fields = {"Enter X Coordinate:", xCor, "Enter Y Coordinate:", yCor};
JOptionPane.showConfirmDialog(null, fields, "Move Shape", JOptionPane.WARNING_MESSAGE);

Recommended Answers

All 7 Replies

String x= JOptionPane.showInputDialog("Please input x: ");
int xCorFromUser = Integer.parseInt(x);

This is how it looks like:

help.png

If I try your code I get an error:
int cannot be converted to java.lang.String

Start4me: if that error message is to difficult to solve, you should stop working with JOptionPane, and start at the beginning.
first learn about primitive types, then about Objects, then about the differences between the two.

commented: Rude, unhelpful -3
commented: He's right though +6

Have you tried calling getText() for your two JTextFields after the dialog has finished?

The simple pre-packaged dialogs in JOptionPane are just that - simple. The assumption for inputDialog is that there is one entry field and one value returned. For your two-value case you should create your own simple dialog and query the field contants via their getText() methods afer the user has pressed OK.

commented: Thank you very much, I appreciate your help +2

I got it to work thanks to JamesCherrill.
This code does what I need:

int theX = Integer.parseInt(xCor.getText());
int theY = Integer.parseInt(yCor.getText());

Just for the archive...

It turns out that you can put (almost) anything as the mesaage in a JOptionPane - inluding JTextFields, JPanels etc, and they are displayed and operate as you would expect. When the dialog closes you can then use the normal methods to retrieve the values from whatever controls you displayed. So you can use this to display 2 fields, or even a complex form.

This definitely works, but I haven't found any definitive doc that confirms it to be officially supported functionality.

JOptionPane is designated to returns multiple value from multiple fields two ways as is,

  • primitive array

  • (intialized) variables

_________________________________________________________________

in all cases is required to test for (easiest by using if - elseif - else)

switch (result) {
  case JOptionPane.OK_OPTION:// and returns from various JButtons
      // code
      break; 
  case JOptionPane.CANCEL_OPTION: // can be escaped by ESC key
      // code
      break; 
  case JOptionPane.EXIT_OPTION: //from X button that came from NativeOS
      // code
      break; 
}

__________________________________________________________

can't see reasons for ... :-), by default JPanel with multiple JComponents fits very well as parameter for JOptionPane

    int result = JOptionPane.showConfirmDialog (
         focus null or JComponent,  
         MyPanel, 
         "SomeDescription",
         JOptionPane.OK_CANCEL_OPTION, 
         JOptionPane.QUESTION_MESSAGE);

    public class MyPanel extends JPanel {
         //my JComponents
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.