shrike Posted July 4, 2004 Share Posted July 4, 2004 import javax.swing.*; import java.text.SimpleDateFormat; import java.util.Date; public class ValidatingDates { public static void DateFormating( String[] cmdArgs ) throws Exception { ?SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" ); ?SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMMM, yyyy" ); ?String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); ? ?Date date = sdfInput.parse( textDate ); ?System.out.println( sdfOutput.format( date ) ); } } Can somebody get this working for me? It compiles ok, but this comes up in the Java Console (1.4.2_04) java.lang.ClassCastException at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) java.lang.ClassCastException at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Link to comment Share on other sites More sharing options...
0 andrew_simpson Posted July 4, 2004 Share Posted July 4, 2004 What's The Problem? edit didn't see the rest only the code at first Link to comment Share on other sites More sharing options...
0 shrike Posted July 4, 2004 Author Share Posted July 4, 2004 yeah... sorry about that Link to comment Share on other sites More sharing options...
0 shrike Posted July 4, 2004 Author Share Posted July 4, 2004 oh... there's more... chuck this on the end: java.lang.IllegalArgumentException: null source at java.util.EventObject.<init>(Unknown Source) at java.awt.AWTEvent.<init>(Unknown Source) at java.awt.event.ComponentEvent.<init>(Unknown Source) at java.awt.event.WindowEvent.<init>(Unknown Source) at java.awt.event.WindowEvent.<init>(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.SequencedEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) java.lang.ClassCastException at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Link to comment Share on other sites More sharing options...
0 shrike Posted July 4, 2004 Author Share Posted July 4, 2004 ...please somebody... java applets just elude me. I'm no good with them. Just in case you don't understand what it's supposed to be doing, I'll explain a little. The applet is supposed to get a date from the user, in the format dd-mm-yy. Then it uses SimpleDateFormat to output the same date in format dd month (MMMMM), year. I've also got to change it so that the years are between 01-01-1980 and 31-12-2020. I've got a vague idea on how I'm going to do that, but any help would be greatful. Link to comment Share on other sites More sharing options...
0 ramesees Posted July 4, 2004 Share Posted July 4, 2004 add the line import java.awt.*; and see if that works Link to comment Share on other sites More sharing options...
0 shrike Posted July 4, 2004 Author Share Posted July 4, 2004 that would be good... but no. Link to comment Share on other sites More sharing options...
0 shrike Posted July 4, 2004 Author Share Posted July 4, 2004 I wrote the applet as a program first, so all I really need to know is how to convert things so that it's appletable. The import java.awt.*; got rid of most of the errors, now I'm just getting this: java.lang.ClassCastException at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 5, 2004 Share Posted July 5, 2004 Probably because you're not extending applet? import java.applet.*; import javax.swing.*; import java.text.SimpleDateFormat; import java.util.Date; public class ValidatingDates extends Applet { public static void DateFormating( String[] cmdArgs ) throws Exception { SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" ); SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMMM, yyyy" ); String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); Date date = sdfInput.parse( textDate ); System.out.println( sdfOutput.format( date ) ); } } Link to comment Share on other sites More sharing options...
0 shrike Posted July 5, 2004 Author Share Posted July 5, 2004 ok... so I extended applet. Now nothing happens. I have a feeling that I'm not doing the UI part right. Can somebody help me with that? Link to comment Share on other sites More sharing options...
0 hava333 Posted July 5, 2004 Share Posted July 5, 2004 If you are just trying to print the current date and time the code below does the trick. Its javascript, not a Java Applet (just a heads up) <script language="JavaScript"> function tS(){ x=new Date(tN().getUTCFullYear(),tN().getUTCMonth(),tN().getUTCDate(),tN().getUTCHours(),tN().getUTCMinutes(),tN().getUTCSeconds()); x.setTime(x.getTime()+dS()-21600000); return x; } function tN(){ return new Date(); } function dS(){ return ((tN().getTime()>fD(0,3,1,1).getTime())&&(tN().getTime()<fD(0,9,1,-1).getTime()))?3600000:0; } function fD(d,m,h,p){ var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(tN().getUTCFullYear(),nm,1,h,0,0),dOff=0; if(p<0){ x.setTime(x.getTime()-86400000); } if(x.getDay()!=d){ dOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d); if(p<0&&dOff>0){ week-=7; } if(p>0&&dOff<0){ week+=7; } x.setTime(x.getTime()+((dOff+week)*86400000)); } return x; } function lZ(x){ return (x>9)?x:'0'+x; } function tH(x){ if(x==0){ x=12; } return (x>12)?x-=12:x; } function dT(){ if(fr==0){ fr=1; document.write(' <font face="Futura XBlk BT" ><b><span id="tP">'+eval(oT)+'</span></b></font>'); } tP.innerText=eval(oT); setTimeout('dT()',1000); } function aP(x){ return (x>11)?'PM':'AM'; } function y4(x){ return (x<500)?x+1900:x; } var mN=new Array('January','February','March','April','May','June','July','August','September','October','November','December'),fr=0,oT="mN[tS().getMonth()]+' '+tS().getDate()+','+' '+y4(tS().getYear())+' '+tH(tS().getHours())+':'+lZ(tS().getMinutes())+' '+aP(tS().getHours())"; </script> <!-- Clock Part 1 - Ends Here --> <!-- Clock Part 2 - This Starts/Displays Your Clock --> <script language="JavaScript">dT();</script> <!-- Clock Part 2 - Ends Here --><!--webbot bot="Timestamp" i-checksum="34911" endspan --> Link to comment Share on other sites More sharing options...
0 em_te Posted July 5, 2004 Share Posted July 5, 2004 The import java.awt.*; got rid of most of the errors, It sounds like there's more to the problem then you're showing us. Adding the said import statement could only have solved a compile problem but you said that it compiled fine before. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 5, 2004 Share Posted July 5, 2004 ok... so I extended applet. Now nothing happens.I have a feeling that I'm not doing the UI part right. Can somebody help me with that? Well, we'd need to see what you're trying to do for the UI first. Link to comment Share on other sites More sharing options...
0 Kestrel Posted July 6, 2004 Share Posted July 6, 2004 This is actually fairly easy to get going but you clearly aren't up to speed on applets so this link will be far more useful than simply providing working code: SUN's Applet Trail Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 I just want an applet where somebody inserts a date, in the format dd-mm-yy. Then it uses SimpleDateFormat to process it, and output onto the screen the same date, but in the format dd month, yyyy that's the bare bones basic description of this applet. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 6, 2004 Share Posted July 6, 2004 ok... so I extended applet. Now nothing happens.I have a feeling that I'm not doing the UI part right. Can somebody help me with that? Well, if that was your complete code, no wonder. You need an init() method for an Applet, this is like the regular main method in some ways. You may want to change the System.out.println to a label or something unless you plan to run it in appletviewer all the time, or they plan to have the console enabled in their browser. This works though: JAVA CODE: import java.applet.*; import javax.swing.*; import java.text.SimpleDateFormat; import java.util.Date; public class ValidatingDates extends Applet { public void init() { String s[] = {"Hello", "Bob"}; try { DateFormating(s); } catch(Exception e) { } } public static void DateFormating( String[] cmdArgs ) throws Exception { SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" ); SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMMM, yyyy" ); String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); Date date = sdfInput.parse( textDate ); System.out.println( sdfOutput.format( date ) ); } } HTML FILE: <APPLET CODE=ValidatingDates.class WIDTH=800 HEIGHT=600> </APPLET> Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 yeah... I have no idea on how to do applets, so I made the thing as a program first. Even reading all the links that people have given me, I still couln't do applets. ..how do I set the output to a label? I have a copy of NetBeans here, too. If that helps. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 6, 2004 Share Posted July 6, 2004 yeah... I have no idea on how to do applets, so I made the thing as a program first.Even reading all the links that people have given me, I still couln't do applets. ..how do I set the output to a label? I have a copy of NetBeans here, too. If that helps. It's just like a normal GUI program. You use applet.add(child);. There's also JApplet which does Swing components under javax.swing. JLabel x = new JLabel(""); this.add(x); x.setText("blahblah"); Should do it. Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 ok... I'll try and give that a go. I'm only doing Java, because it's a part of my web design course. This is just some stuff that I have to do over the holidays (ends next week). We were supposed to be learning out of Deitel & Deitel's "Java? How to Program: 5th Edition", but the school only had 1 copy, which the teacher took. And he told us that he'll order in Ed 6 when it's released... but that's useless to me, because my Java component is now over! Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 ValidatingDates.java [42:1] non-static variable this cannot be referenced from a static context this.add(output); ^ 1 error Errors compiling ValidatingDates. oh... this is what I put in: JLabel output = new JLabel(""); this.add(output); output.setText("The validated date is:\n" + sdfOutput.format(date)); Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 6, 2004 Share Posted July 6, 2004 (edited) It should actually be in your init() method. Is there any reason why you're making your DateFormating method static? Do you want it to be called outside without creating an object? It's probably better to pass the date in to the DateFormating method and return it in the new format. Edited July 6, 2004 by kjordan2001 Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 Oh. I was just reading in my own formatting. That would've stuffed it up. Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 I think I've really stuffed this up:: import java.applet.*; import javax.swing.*; import java.text.SimpleDateFormat; import java.util.Date; public class ValidatingDates extends Applet { public void init() { String s[] = {"Hello", "Bob"}; try { DateFormating(s); } catch(Exception e) { } public static void DateFormating( String[] cmdArgs ) throws Exception { SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" ); SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMMM, yyyy" ); String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); JLabel dateOutput = new JLabel(""); this.add(dateOutput); dateOutput.setText("The formatted date is:\n" + sdfOutput.format(date)); // Date date = sdfInput.parse( textDate ); // System.out.println( sdfOutput.format( date ) ); }; }; } Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 6, 2004 Share Posted July 6, 2004 import javax.swing.*; import java.text.SimpleDateFormat; import java.util.Date; public class ValidatingDates extends JApplet { public void init() { JLabel x = new JLabel(""); this.add(x); String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); try { x.setText(DateFormating(textDate)); } catch(Exception e) { x.setText("Invalid input"); } } public String DateFormating( String cmdArgs ) throws Exception //Probably don't even need this method for something so simple { SimpleDateFormat sdfInput = new SimpleDateFormat( "dd-mm-yy" ); SimpleDateFormat sdfOutput = new SimpleDateFormat ( "dd MMMMM, yyyy" ); Date date = sdfInput.parse( textDate ); return sdfOutput.format( date ); } } Link to comment Share on other sites More sharing options...
0 shrike Posted July 6, 2004 Author Share Posted July 6, 2004 cannot resolve symbol textDate. does that mean that I need to move the String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); down to the DateFormatting part? Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted July 6, 2004 Share Posted July 6, 2004 cannot resolve symbol textDate.does that mean that I need to move the String textDate = JOptionPane.showInputDialog( "Enter date in format dd-mm-yy"); down to the DateFormatting part? Or you could use cmdArgs. Link to comment Share on other sites More sharing options...
Question
shrike
Can somebody get this working for me? It compiles ok, but this comes up in the Java Console (1.4.2_04)
java.lang.ClassCastException
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.ClassCastException
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Link to comment
Share on other sites
44 answers to this question
Recommended Posts