• 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

Head First Java Question - GUI program TwoButtons

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am new here and new to Java programming. I'm working my way through the Head First Java book. I'm stuck. I created the program TwoButtons, which is presented (in piecemeal) in that book. I wrote out my code, and although it compiles and runs, it doesn't produce the drawPanel that is supposed to be in the center.

This is frustrating me. Can anyone point out what I'm not noticing? It seems that something must be wrong with my code.

Here's my code below.

Thank you in advance.


 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Case-sensitivity is very important in Java. I notice you've got a method called "PaintComponent" where you almost certainly intended to have "paintComponent". Your misnamed method won't be called, and that's probably what's going on here.
 
May Carlson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!

That fixed the problem!

I have another question, although it really is just a curiosity thing. Why are parenthesis () used after the getContentPane part of frame.getContentPane().add()

The double set of parenthesis (both after getContentPane and add) are unlike any syntax I have seen so far... I understand that it usually has to do with calling a method but it seems strange to me that it happens twice - but then again there are two periods in that statement which is unlike most of what I've seen before. I'm just wondering what the concept is behind it?

Thanks again for your quick response!

May



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

May Carlson wrote:Thank you very much!

That fixed the problem!

I have another question, although it really is just a curiosity thing. Why are parenthesis () used after the getContentPane part of frame.getContentPane().add()

The double set of parenthesis (both after getContentPane and add) are unlike any syntax I have seen so far... I understand that it usually has to do with calling a method but it seems strange to me that it happens twice - but then again there are two periods in that statement which is unlike most of what I've seen before. I'm just wondering what the concept is behind it?

Thanks again for your quick response!

May





You are calling multiple methods on the same line. You start by calling the getContentPane() method of frame, which is returning an object which has an add() method that you are calling.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some people believe that such method chaining is a bad way to write code. What happens if the call to frame.getContentPane() fails, and doesn't return an object? you'll then try and call the add() method on the NULL object.

now, that may not be horrible here, but i've seen code along the lines of


myObject.callMethodA().callMethodB().callMethodC().callMethodD().callMethodE()

now you get a null pointer exception on this line...which method caused it?

of course, the alternative is to do something like this:

TempA a = myObject.callMethodA();
TempB b = a.CallMethodB();
TempC c = b.CallMethodC();
TempD d = c.CallMethodD();
d.callMethodE();

Which I don't think is much better...
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, as Joe says, it's just a shorthand way to write something like

Container temp = frame.getContentPane();
temp.add(something);

In general, "chaining" method calls together like this is something to be avoided, as it makes stepping through your code with a debugger more difficult.
 
May Carlson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! Your explanations make sense. Knowing how something works makes it a lot easier for me to remember how to code it!

May
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic