• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GUI Problem

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here's the code from Head First Java, but it is not working on my Eclipse.
The output is wrong but there was no error only a warning in the code itself that says : The serialized class MyDrawPanel does not declare static final SerialVersionUID field type of long

I do not what does it mean!

Please help.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if you have gotten to the chapter in Head First that talks about Serialization of objects yet. Briefly, serialization is the writing or saving or an object to a file for example. Objects can be made to be serializable by extending the java.io.Serializable interface. Many of the Java Swing classes are serializable. When you make an object Serializable, you are suppose to put a private static final long property in the class named "SerialVersionUID". The value that you assign to this property is determined by a checksum formula. Java then uses the SerialVersionUID when deserializing an object to determine if the serialized version of the object is sufficiently compatible with the version of the class the object is trying to be serialized to. So its basically a version check.

At this point, I wouldn't worry too much about that warning; it will not affect your output at all. Later you can revisit the subject and learn more about it, including learning how you actually generate a SerialVersionUID value.

Now you mentioned the output you are getting is wrong. What output are you getting and what is expected?

[ Edit ]
Ok - I just ran the code you posted and I'm getting a blank frame. I'll take a look and let you know what I can determine.
[ July 27, 2008: Message edited by: Mark Vedder ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right click on the warning and choose to fix it. Choose a generated serialVersionUID. Presto.

While usually you want to generate a different serialVersionUID for each time you change anything in the class' signature, sometimes you want to retain compatibility with older versions. If the non-static non-transient fields themselves have not changed in name, type or order you will retain the compatibilty by using the same serialVersionUID. As said before, this will only work properly if the object can be (de)serialized in the same way.

Also something I just though about: if you do your own custom (de)serialization without using the default mechanism (i.e. you implement readObject and writeObject), you can always retain the same serialVersionUID as long as you write the same types in the same order.


You can also check your Eclipse settings and choose to ignore the missing serialVersionUID. That way the compiler will generate a new serialVersionUID each time you change the class enough.
[ July 27, 2008: Message edited by: Rob Prime ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK - I see the problem:


The drawing panel is getting added on top of the button and is therefore covering it up. If you look at the frame when it runs, you will see a small black stripe at the bottom. That's the MyDrawPanel. Change the code so the panel is added to the CENTER:


Was that just a typo on your part (which we've all done) or is it wrong in the book?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other thing I just noticed, is you need to modify your random color generation. Right now, it will always give you black, or a color with an RGB value of 0,0,0. The reason is this... Math.random() returns a double between 0 and 1. The int casting is happening before that value is getting multiplied by the 255. And based on the way int casting works, it will always cast to zero since it simply truncates. So you are always multiplying 0 and 255.

To fix, simply put the multiplication in parentheses so it happens before the casting:


Do that for all three colors.

[ July 27, 2008: Message edited by: Mark Vedder ]
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Vedder:
OK - I see the problem:


The drawing panel is getting added on top of the button and is therefore covering it up. If you look at the frame when it runs, you will see a small black stripe at the bottom. That's the MyDrawPanel. Change the code so the panel is added to the CENTER:


Was that just a typo on your part (which we've all done) or is it wrong in the book?




Hey Sir Mark,

It was a wrong typo on my part. I am so sorry , I am embarrased, I am using two books so I mixed it up. But I understand now what is serialization. My proffessor have not teach us that, but we will have GUI the next meeting. Just trying to advance study..

Thanks for the help.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to help. And no need to be embarrassed, and no need to apologize. There isn't a developer out there that hasn't made a typo at some point; and many of us make them daily ;0 The only reason I asked was that if it was wrong in the book, we'd want to file an errata report with them.

And we've all had cases where after spending hours trying to figure out why something doesn't work, upon calling someone else over to look at it, they see the problem in two seconds. That's why it is always good to ask for a second set of eyes. It's like proofreading something you wrote. Sometimes you are too close to the material to see the problems.

And kudos to you for reading ahead. You always get far more out of a lecture if you have seen the material beforehand.
[ July 28, 2008: Message edited by: Mark Vedder ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic