• Post Reply Bookmark Topic Watch Topic
  • New Topic

Gui Problem in Head First Java Chapter 12  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

This is my first post. When running the following code from Head First Java, Chapter 12, page 371, I get a grey frame but the oval does not appear. Nothing happens when I click the button as well. I'm running Windows 7 Home Premium.

Thanks!


 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I suggest you use the @Override annotation whenever you write a method that's intended to override a method from a superclass. In this case that line of code would look like this:



Your error is in that line of code.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Steven L'Heureux
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt reply! However, when I insert the @Override annotation, I get the following compiler error:

error: method does not override or implement a method from a supertype

Any ideas?

Thanks again.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

The @Override annotation is designed to look for spelling errors, etc., because tiny misspellings like public String tostring() { return "Campbell"; } are notorious for causing errors which are difficult to find.
By the way: make the method in question protected, not public.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven L'Heureux wrote:error: method does not override or implement a method from a supertype

Any ideas?


Well, yeah. What was the name of the method you wanted to implement? What is the name of your method?
 
Steven L'Heureux
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome again

The @Override annotation is designed to look for spelling errors, etc., because tiny misspellings like public String tostring() { return "Campbell"; } are notorious for causing errors which are difficult to find.
By the way: make the method in question protected, not public.


Wow! I had no idea such a tiny spelling mistake would be so frustrating. I've always assumed the compiler would catch those. Is it because the misspelled method was never called?

Thanks Guys!
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven L'Heureux wrote:Wow! I had no idea such a tiny spelling mistake would be so frustrating. I've always assumed the compiler would catch those. Is it because the misspelled method was never called?


Yup, as far as the compiler is concerned, misspelled is just wrong. In fact it would be possible for you to declare a method "paintComponet(Graphics g)" and another method "paintComponent(Graphics g)" in the same class -- the compiler is fine with those because they have different names.

And that's really what you did. JPanel already had a "paintComponent(Graphics g)" method and you declared another method "paintComponet(Graphics g)". And when Swing decided that your panel needed to be painted, it called the "paintComponent(Graphics g)" method, and not your method which had a different name. That's why you didn't see anything being painted, because your method wasn't being called.

But so far there was nothing wrong with that. (Aside from the fact that it didn't do what you wanted, that is.) What flushed out the problem was the "@Override" annotation. When you attach that to a method, you say "This method overrides a method from the superclass." But your "paintComponet(Graphics g)" didn't override any method in JPanel, so that made the compiler flag it as an error. So @Override is a tool which helps you to flush out minor misspellings, and other similar errors, which can be hard to notice.
 
Steven L'Heureux
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good to know. Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!