Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: message box for error in parsing date

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default message box for error in parsing date

    Dear all,

    I have been trying different things with creating Message Box but it does not work

    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:

    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:

    			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!
    Last edited by johnyjj2; November 6th, 2011 at 01:37 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: message box for error in parsing date

    You are using invalid arguments for your showMessageDialog().

    (Component, Object, String, int) are the arguments.

    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!
    Last edited by snowguy13; November 27th, 2011 at 02:43 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. How to display error message box
    By jasonxman in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 21st, 2011, 03:47 PM
  2. Problem parsing WHOIS date from text and capturing fields
    By jdev28 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 16th, 2011, 04:27 PM
  3. [SOLVED] Help making an error message.
    By Lost_Secret in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 1st, 2011, 04:48 PM
  4. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  5. help with a error message
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2010, 02:56 PM