message box for error in parsing date
Dear all,
I have been trying different things with creating Message Box but it does not work
Code :
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import javax.swing.JOptionPane;
import javax.swing.UIManager; //icons?
import javax.swing.*; //http://www.roseindia.net/java/example/java/swing/ShowDialogBox.shtml
import java.awt.event.*; //http://www.roseindia.net/java/example/java/swing/ShowDialogBox.shtml
public class Person {
//...
public Person (int x, int y, Color color, String nameNow, String surnameNow, String birth, String death)
{
//...
DateFormat dfm = new SimpleDateFormat("dd MMM yyyy"); //yyyy-MM-dd
try
{ this.birth = dfm.parse("1 FEB 1969"); }
catch (Exception e)
{
JOptionPane.showMessageDialog(this,
"Eggs are not supposed to be green.",
"Inane warning",
JOptionPane.WARNING_MESSAGE);
}
}
public void draw (Graphics gDC)
{
//...
}
}
Lastly, I have tried the code from here How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) but the IDE says:
Quote:
Description Resource Path Location Type
The method showMessageDialog(Component, Object, String, int) in the type JOptionPane is not applicable for the arguments (Person, String, String, int) Person.java /Test1/src line 41 Java Problem
The above is about this:
Code :
JOptionPane.showMessageDialog(this,
"Eggs are not supposed to be green.",
"Inane warning",
JOptionPane.WARNING_MESSAGE);
I have also tried null instead of this. What can I do to create the proper message about error?
In fact, I am also curious why it causes the exception and cannot change string to date.
Regards!
Re: message box for error in parsing date
You are using invalid arguments for your showMessageDialog().
(Component, Object, String, int) are the arguments.
Code Java:
JOptionPane.showMessageDialog(this,
"Eggs are not supposed to be green.",
"Inane warning",
JOptionPane.WARNING_MESSAGE);
You use "this" as the first argument for showMessageDialog. Using "this" will call the Person object you have created. Your Person object is not a Component, so the method encounters an error. I would simply replace "this" with null for the first argument.
As to why that exception is caused, I think you have to have the try / catch combination there not because the date conversion always causes an exception, but only because it CAN cause an exception. These exceptions won't always occur, but if they do the program has to be able to catch the exception if it occurs.
Edit: I apologize, I didn't see that you had tried null already. What happens when you use null? That always works fine for me...
Hopefully this helps!