• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple values in Dialog box  RSS feed

 
raj bhai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For E.G The below program ask the user to enter the value of x 1 , Y 1 and radius in separate boxes , What would look nice is if the user can Enter the Value X ,Y and radius in the same box .. Please let me know

import javax.swing.JOptionPane;

public class C3E29GeometryTwoCircles
{
public static void main(String[] args)
{
// Ask the user to enter the x1 coordinate.
String strNumber = JOptionPane.showInputDialog("Enter the value of x1 coordinate " );
double x1 = Double.parseDouble(strNumber);

strNumber = JOptionPane.showInputDialog("Enter the value of y1 coordinate " );
double y1 = Double.parseDouble(strNumber);

strNumber = JOptionPane.showInputDialog("Enter the value of radius " );
double r1 = Double.parseDouble(strNumber);

strNumber = JOptionPane.showInputDialog("Enter the value of x2 coordinate " );
double x2 = Double.parseDouble(strNumber);

strNumber = JOptionPane.showInputDialog("Enter the value of y2 coordinate " );
double y2 = Double.parseDouble(strNumber);

strNumber = JOptionPane.showInputDialog("Enter the value of radius " );
double r2 = Double.parseDouble(strNumber);

double distance = Math.sqrt((Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)));

String strOutput;
if ((distance <= Math.abs(r1 - r2)))
{

strOutput = "The circle2 is inside circle1 ";
}
else if (distance <= (r1 + r2))
{

strOutput = "circle2 overlaps circle1";
}
else
{
strOutput = "circle2 does not overlap circle1";
}
JOptionPane.showMessageDialog(null, strOutput);
}
}
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JOptionPane.showInputDialog() will always return a String.

One approach would be to ask the user to enter the values separated by a space, then split the returned String value into the three parameters you need. Of course, now you are relying on the user to provide valid data (never a good thing).

A better approach, though more work, would be to create your own userInput class that extends JPanel with three distinct JLabels and JTextFields for user input. This approach would allow you to reuse the userInput class for additional circles and allow you to filter out bad user input via the JButton's ActionListener.


Cheers!
Chris
2014-02-20_09-07-38.png
[Thumbnail for 2014-02-20_09-07-38.png]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!