File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes North Panel Not Displaying.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "North Panel Not Displaying...." Watch "North Panel Not Displaying...." New topic
Author

North Panel Not Displaying....

Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
I have a problem here that i think must be simple but i cant fix. I create a frame using the following code...




I have a problem in that the middle canvas displays fine. but the north panel although i can see a space left for it doesnt seem to display n e thing... If i change it from that to display a button eg new Button, I can see the button, or i can see the label, but not a canvas so annoying!

THe code for mySecondCanvas.java is


If any one can spot why im not seeing Some Text here please, i would be really apreciative!!!

[ May 20, 2005: Message edited by: Alan Hollis ]
[ May 20, 2005: Message edited by: Alan Hollis ]
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Try to call super.paint() in you paint method. If you add a label upon the canvas it should show.
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
hmm dont understand....

Super.paint() causes could not find paint(), if added to paint meathod in mySecondCanvas
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
sorry,
super.paint(g);

You should pass the graphics object.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Moving to the Swing forum.


GenRocket - Experts at Building Test Data
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
hmmm didnt do n e thing

Compiled but still showed nothing in north panel

This is really annoying lol
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

What Manuel means, since he didn't tell you where to call that method:



The reason for this is when you override the paint method, you need to tell the component to go ahead and draw itself. When you don't call super.paint(g) the component never knows that it needs to be drawn. That is why you couldn't see it.

On a side note, class name should always use Upper Camel Case.

mySecondCanvas should be MySecondCanvas.
[ May 20, 2005: Message edited by: Gregg Bolinger ]
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
There are actually a number of problems why you can not see anything painted on your canvas. I just tried out you code an noticed that nothing gets painted whatever I try. I changed your mySecondCanvas class to extends Panel and not Canvas and voila I can see what gets painted.

Second problem is you questionDisplay panel. I advise you to just delete that code and add the mc2 panel directly NORTH in the frame. Otherwise you will have to set it's layoutmanager too.

Third problem is that you have not given a size to the mySecondCanvas class. Without given it a size the layout manager will make it so small that you cannot see the text that you have drawn on to it.

So:
- you have to remove the questionDisplay panel.
- use the setSize() method on the mySecondCanvas object and determine the height (is important for the borderlayout)
- do not extends from Canvas but from Panel (I do not really know why, it think there is some bug in Canvas maybe).
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
Thank you very much for your time.... ill try this now
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't see any reason to call super.paint() here, but I do see what's going on.

There are two problems. First of all, you're drawing the String 50 pixels down from the top of the component, which is probably larger than the reserved space. But more importantly, the mySecondCanvas object doesn't know that you're going to draw 50 pixels from the top. The BorderLayout will ask it "How tall to you want to be?" by calling the getPreferredSize() method, and it will say "0 pixels would be great, thanks" since it has nothing to display. You have to tell the mySecondCanvas class how tall it should say it wants to be by using the setPreferredSize() method. You could call this in mySecondCanvas's constructor.


[Jess in Action][AskingGoodQuestions]
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
As a reply to Ernest:

Indeed in the example code he does not have to call super.paint(g); but he mentioned that a JLabel did not get painted when he added it upon the panel. In that case he should call the super.paint(g); method.
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
Whay i can see my text


Not quite getting how to use setSize(); or setPreferedSize();

but im googling now

Thanks very much for your help !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: North Panel Not Displaying....