Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

North Panel Not Displaying....

 
Alan Hollis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to call super.paint() in you paint method. If you add a label upon the canvas it should show.
 
Alan Hollis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm dont understand....

Super.paint() causes could not find paint(), if added to paint meathod in mySecondCanvas
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,
super.paint(g);

You should pass the graphics object.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
Alan Hollis
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your time.... ill try this now
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic