This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Head First Java Question - GUI program TwoButtons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Question - GUI program TwoButtons" Watch "Head First Java Question - GUI program TwoButtons" New topic
Author

Head First Java Question - GUI program TwoButtons

May Carlson
Greenhorn

Joined: Aug 11, 2010
Posts: 3
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.


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
May Carlson
Greenhorn

Joined: Aug 11, 2010
Posts: 3
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



W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10908
    
  12

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...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Aug 11, 2010
Posts: 3
Thank you! Your explanations make sense. Knowing how something works makes it a lot easier for me to remember how to code it!

May
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java Question - GUI program TwoButtons
 
Similar Threads
HFJ TwoButtons problems
TwoButtons App From "HeadFirst Java" Book
Beginners question (Head First - TwoButtons)
Simple Gui Question (Head First Java)
why does this code fail?