aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problem with custom painting and multiple JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with custom painting and multiple JPanel" Watch "Problem with custom painting and multiple JPanel" New topic
Author

Problem with custom painting and multiple JPanel

Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
I am learning some custom painting and I encounter some problems. I want to create a frame that when I click a button, the component of the frame get redraw. It worked, however, since I add all the components onto 1 panel, I cant control its layout. So I put every component onto a separate panel, and add all these panel to one main panel, and at the end, add that main panels to the frame. Now my paintComponent(Graphics g), dont work anymore. I think since I create multiple panel, when I call repaint(), it does not know what panel to repaint any more.

Here is when I only have one panel. Custom painting work fine


Here is the code with multiple panels. Custom painting stop working

Bartek Myszkowski
Ranch Hand

Joined: Feb 03, 2009
Posts: 44
In "multiple panels" code, in class called MyPanel, You extend JPanel and override paintComponent to create custom painting but also You create mainPanel where You add other panels and You use only mainPanel which method paintComponent is not overridden! mainPanel is not object of MyPanel!

I have fixed Your code (and it works - it does custom painting) - compare it with code You have provided!!!
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
Thank you.
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
Hi Bartek Myszkowski, your help was great. So after I fix that problem, I develop a bit more and ran into another problem. Before I have a Textfield on SOUTH, a button on NORTH. Now if I add a panel that contain a image to the CENTER, now I cannot see my string that draw by my paintComponent(). I know that the Image Panel cover it, is there a way that I can draw the string on top of that Image Panel. Or, if I want to draw graphics on the top panel layer, how can I do it? Thank you. Let me know if you need me to provide the code. Here is the baisc layout
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Now if I add a panel that contain a image to the CENTER, now I cannot see my string that draw by my paintComponent().
> I know that the Image Panel cover it, is there a way that I can draw the string on top of that Image Panel.

combine the two.

on this forum's main page there is a link to the faq, which has a background image faq.
add your drawString to it's paintComponent(..)
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Cross-posted: forums.sun.com Swing Section
Thang Pham
Ranch Hand

Joined: Jan 21, 2009
Posts: 35
Thanks Michael, I solve the problem already. I need to set the opaque to false
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with custom painting and multiple JPanel