File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes add a Canvas to a JPanel within a JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "add a Canvas to a JPanel within a JFrame" Watch "add a Canvas to a JPanel within a JFrame" New topic

add a Canvas to a JPanel within a JFrame


Joined: Sep 03, 2001
Posts: 5
hi there!
i extends Canvas and i draw image in it like this :

th problem is when i try to put the class instance on a JFrame and user add() , the Canvas doesnt appear ,
this is how i test it :

thanks in advance

[This message has been edited by samifox (edited September 03, 2001).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.
I am moving this thread to the AWT/Swing forum.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171

You need to do two things (I think):
- Use a layout managerin your Frame; e.g. GridLayout(1,1)
- implement getPreferredSize() in your Canvas so that the layout manager knows how big it should be.
Note this is based on AWT. I don't know if Swing works exactly the same way.

Sun Certified Programmer for the Java 2 Platform
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Remove the call to super in the paint method and the string gets drawn. I didn't see the image though.
I agree. Here's the link:
subject: add a Canvas to a JPanel within a JFrame
jQuery in Action, 3rd edition