Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Canvas -> Swing, paintComponent() not working

Status
Not open for further replies.

NeedMiscHelp

Programmer
Dec 12, 2002
7
US
Everyone says to use JComponent or JPanel instead of Canvas. I override paintComponent() with the code that was formerly in paint(), but nothing shows up. I just get a gray panel. I put in a System.out.println() statement in the paintComponent() method and make sure it only prints out at the most 5 times (based on a global variable). I see that nothing even gets printed??!! Then my computer crashes... soooo what else am I supposed to besides simply override paintComponent()?
 
You may need to turn any layout managers you have on off.

I'm not sure if this will help, but last time I got paintComponent to work I have the line
Code:
setLayout(null)
in my constructor.

Lets us know how it goes. ----------------------------------------
There are no onions, only magic
----------------------------------------
 
I found the solution to my problem- the Canvas used to be in a JPanel itself, now I was adding a JPanel to a JPanel to a JScrollPane. So now I just directly add the first JPanel to JScrollPane. Alternatively, I think one could use setOpaque(false) on the container JPanel if more stuff needed to be added. That layout tip kind of help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top