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 ]
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 ]
Joined: Mar 05, 2002
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).
Joined: Feb 08, 2005
Thank you very much for your time.... ill try this now
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.
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.
Joined: Feb 08, 2005
Whay i can see my text
Not quite getting how to use setSize(); or setPreferedSize();